From b332aea72950471fe73f79fadbea2e9d9ce67c0b Mon Sep 17 00:00:00 2001 From: Chen Yu Date: Tue, 12 Sep 2023 23:47:46 +0900 Subject: [PATCH] feat: alias Pattern to Filter for better understanding in functional (#466) * feat: alias Pattern to Filter for better understanding in functional context Ref: https://github.com/ckb-js/kuai/issues/462 * docs: update docs for using Filter instead of Pattern --- README.md | 12 +++++------ docs/tutorials/mvp-dapp.md | 20 +++++++++---------- packages/models/src/actor/actor-reference.ts | 2 ++ .../models/src/utils/decorator/provider.ts | 11 ++++++++++ .../mvp-dapp/src/actors/omnilock.model.ts | 10 +++++----- .../mvp-dapp/src/actors/record.model.ts | 10 +++++----- 6 files changed, 39 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index c20828f9..4ccf418a 100644 --- a/README.md +++ b/README.md @@ -403,11 +403,11 @@ For this, we provider the default `listener` and the default `data source` in `k We provider several decorators to help to register the `store` to resource binding, here is one example to use these decorators as below. ```typescript -import { ActorProvider, DataPattern, LockPattern, Omnilock } from '@ckb-js/kuai-models' +import { ActorProvider, DataFilter, LockFilter, Omnilock } from '@ckb-js/kuai-models' @ActorProvider({ ref: { name: 'omnilock', path: `/:args/` } }) -@LockPattern() -@DataPattern('0x') +@LockFilter() +@DataFilter('0x') @Omnilock() class OmnilockModel extends JSONStore> { constructor() { @@ -438,9 +438,9 @@ The Lock decorators we provided as below. `kuai` should know which cell should be connected to the `store`, filter data of them from chain. We provide several decorator for you to do this. -- `LockPattern`: Filter the lock to equal to the `lockscript` of `store` defined. -- `DataPattern`: It has one parameter means data, filter the data in cell to equal to the parameter. -- `DataPrefixPattern`: It has one parameter means data prefix, filter the data to start with the parameter. +- `LockFilter`: Filter the lock to equal to the `lockscript` of `store` defined. +- `DataFilter`: It has one parameter means data, filter the data in cell to equal to the parameter. +- `DataPrefixFilter`: It has one parameter means data prefix, filter the data to start with the parameter. ### [store](https://github.com/ckb-js/kuai/tree/develop/packages/models/src/store) diff --git a/docs/tutorials/mvp-dapp.md b/docs/tutorials/mvp-dapp.md index d64731c5..3afa1032 100644 --- a/docs/tutorials/mvp-dapp.md +++ b/docs/tutorials/mvp-dapp.md @@ -218,8 +218,8 @@ At first, the built-in model named **JSONStore** should be imported as the basic ```typescript @ActorProvider({ ref: { name: 'omnilock', path: '/:args/' } }) -@LockPattern() -@DataPattern('0x') +@LockFilter() +@DataFilter('0x') @Omnilock() export class OmnilockModel extends JSONStore> { constructor( @@ -231,7 +231,7 @@ export class OmnilockModel extends JSONStore> { schemaPattern?: SchemaPattern }, ) { - super(undefined, { ...params, ref: ActorReference.newWithPattern(OmniLockModel, `/${args}`) }) + super(undefined, { ...params, ref: ActorReference.newWithFilter(OmniLockModel, `/${args}`) }) this.registerResourceBinding() } } @@ -239,10 +239,10 @@ export class OmnilockModel extends JSONStore> { Pay attention to the decorators above **OmnilockModel** -1. [ActorProvider](https://github.com/ckb-js/kuai/blob/962d503e75dc808812ec216409877ddc8545c109g/packages/models/src/utils/decorator/provider.ts#L20) defines how the model instance should be indexed. It works with the constructor parameter **args** to construct a unique index; -2. [LockPattern](https://github.com/ckb-js/kuai/blob/962d503e75dc808812ec216409877ddc8545c109g/packages/models/src/utils/decorator/provider.ts#L82) indicates that OmnilockModel follows a pattern of lock script; -3. [DataPattern](https://github.com/ckb-js/kuai/blob/962d503e75dc808812ec216409877ddc8545c109g/packages/models/src/utils/decorator/provider.ts#L98) make sure all cells collected are plain cells; -4. [Omnilock](https://github.com/ckb-js/kuai/blob/962d503e75dc808812ec216409877ddc8545c109g/packages/models/src/utils/decorator/provider.ts#L142) injects a well-known cell pattern for **LockPattern**. +1. [ActorProvider](https://github.com/ckb-js/kuai/blob/54ada687e6f5529bc660d85f72f97c42fc441a19/packages/models/src/utils/decorator/provider.ts#L20) defines how the model instance should be indexed. It works with the constructor parameter **args** to construct a unique index; +2. [LockFilter](https://github.com/ckb-js/kuai/blob/54ada687e6f5529bc660d85f72f97c42fc441a19/packages/models/src/utils/decorator/provider.ts#L153) indicates that OmnilockModel follows a pattern of lock script; +3. [DataFilter](https://github.com/ckb-js/kuai/blob/54ada687e6f5529bc660d85f72f97c42fc441a19/packages/models/src/utils/decorator/provider.ts#L155) make sure all cells collected are plain cells; +4. [Omnilock](https://github.com/ckb-js/kuai/blob/54ada687e6f5529bc660d85f72f97c42fc441a19/packages/models/src/utils/decorator/provider.ts#L144) injects a well-known cell pattern for **LockPattern**. By prepending all these decorators, an OmnilockModel instance represents cells owned by `OmniLockAddress(args)` as an entire object during runtime. @@ -256,8 +256,8 @@ Similarly, the RecordModel can be decorated to limit its cells by `omnilock` and ```typescript @ActorProvider({ ref: { name: 'record', path: '/:codeHash/:hashType/:args/' } }) -@LockPattern() -@TypePattern() +@LockFilter() +@TypeFilter() @Lock() // arbitrary lock pattern @Type(PROFILE_TYPE_SCRIPT) // Inject PROFILE_TYPE_SCRIPT for Type Pattern. PROFILE_TYPE_SCRIPT can be imported from the facility generated by contract deployment export class RecordModel extends JSONStore<{ data: { offset: number; schema: StoreType['data'] } }> { // offset can be removed along with data prefix pattern @@ -277,7 +277,7 @@ export class RecordModel extends JSONStore<{ data: { offset: number; schema: Sto { data: { offset: (DAPP_DATA_PREFIX_LEN - 2) / 2 } }, { ...params, - ref: ActorReference.newWithPattern(RecordModel, `/${codeHash}/${hashType}/${args}/`), + ref: ActorReference.newWithFilter(RecordModel, `/${codeHash}/${hashType}/${args}/`), }, ) diff --git a/packages/models/src/actor/actor-reference.ts b/packages/models/src/actor/actor-reference.ts index 8ce9e426..5dc872d1 100644 --- a/packages/models/src/actor/actor-reference.ts +++ b/packages/models/src/actor/actor-reference.ts @@ -29,6 +29,8 @@ export class ActorReference { return ref } + static newWithFilter = this.newWithPattern + #name: ActorName #path: string #protocol: string diff --git a/packages/models/src/utils/decorator/provider.ts b/packages/models/src/utils/decorator/provider.ts index d40c9135..2aaa1481 100644 --- a/packages/models/src/utils/decorator/provider.ts +++ b/packages/models/src/utils/decorator/provider.ts @@ -144,3 +144,14 @@ export const DefaultLock = export const Omnilock = (): ClassDecorator => DefaultLock('OMNILOCK') export const Secp256k1Lock = (): ClassDecorator => DefaultLock('SECP256K1_BLAKE160') + +/* + * Alias pattern to filter for better understanding in functional context + */ +export { + Pattern as Filter, + LockPattern as LockFilter, + TypePattern as TypeFilter, + DataPattern as DataFilter, + DataPrefixPattern as DataPrefixFilter, +} diff --git a/packages/samples/mvp-dapp/src/actors/omnilock.model.ts b/packages/samples/mvp-dapp/src/actors/omnilock.model.ts index 9804f717..003967d6 100644 --- a/packages/samples/mvp-dapp/src/actors/omnilock.model.ts +++ b/packages/samples/mvp-dapp/src/actors/omnilock.model.ts @@ -15,8 +15,8 @@ import { OutPointString, SchemaPattern, UpdateStorageValue, - DataPattern, - LockPattern, + DataFilter, + LockFilter, JSONStorage, } from '@ckb-js/kuai-models' import { BI } from '@ckb-lumos/bi' @@ -29,8 +29,8 @@ import { bytes } from '@ckb-lumos/codec' * add business logic in an actor */ @ActorProvider({ ref: { name: 'omnilock', path: `/:args/` } }) -@LockPattern() -@DataPattern('0x') +@LockFilter() +@DataFilter('0x') @Omnilock() export class OmnilockModel extends JSONStore> { #omnilockAddress: string @@ -45,7 +45,7 @@ export class OmnilockModel extends JSONStore> { schemaPattern?: SchemaPattern }, ) { - super(undefined, { ...params, ref: ActorReference.newWithPattern(OmnilockModel, `/${args}/`) }) + super(undefined, { ...params, ref: ActorReference.newWithFilter(OmnilockModel, `/${args}/`) }) if (!this.lockScript) { throw new Error('lock script is required') } diff --git a/packages/samples/mvp-dapp/src/actors/record.model.ts b/packages/samples/mvp-dapp/src/actors/record.model.ts index c1117315..78c7ba05 100644 --- a/packages/samples/mvp-dapp/src/actors/record.model.ts +++ b/packages/samples/mvp-dapp/src/actors/record.model.ts @@ -16,8 +16,8 @@ import { OutPointString, SchemaPattern, UpdateStorageValue, - LockPattern, - TypePattern, + LockFilter, + TypeFilter, } from '@ckb-js/kuai-models' import { InternalServerError } from 'http-errors' import { BI } from '@ckb-lumos/bi' @@ -42,8 +42,8 @@ export type StoreType = { * add business logic in an actor */ @ActorProvider({ ref: { name: 'record', path: '/:codeHash/:hashType/:args/' } }) -@LockPattern() -@TypePattern() +@LockFilter() +@TypeFilter() @Lock() @Type(MVP_CONTRACT_TYPE_SCRIPT) export class RecordModel extends JSONStore<{ data: { offset: number; schema: StoreType['data'] } }> { @@ -63,7 +63,7 @@ export class RecordModel extends JSONStore<{ data: { offset: number; schema: Sto { data: { offset: (DAPP_DATA_PREFIX_LEN - 2) / 2 } }, { ...params, - ref: ActorReference.newWithPattern(RecordModel, `/${codeHash}/${hashType}/${args}/`), + ref: ActorReference.newWithFilter(RecordModel, `/${codeHash}/${hashType}/${args}/`), }, )