Skip to content

Commit

Permalink
feat: alias Pattern to Filter for better understanding in functional (#…
Browse files Browse the repository at this point in the history
…466)

* feat: alias Pattern to Filter for better understanding in functional
context

Ref: #462

* docs: update docs for using Filter instead of Pattern
  • Loading branch information
Keith-CY authored Sep 12, 2023
1 parent 7a21f23 commit b332aea
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, never>> {
constructor() {
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 10 additions & 10 deletions docs/tutorials/mvp-dapp.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, never>> {
constructor(
Expand All @@ -231,18 +231,18 @@ export class OmnilockModel extends JSONStore<Record<string, never>> {
schemaPattern?: SchemaPattern
},
) {
super(undefined, { ...params, ref: ActorReference.newWithPattern(OmniLockModel, `/${args}`) })
super(undefined, { ...params, ref: ActorReference.newWithFilter(OmniLockModel, `/${args}`) })
this.registerResourceBinding()
}
}
```

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.

Expand All @@ -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
Expand All @@ -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}/`),
},
)

Expand Down
2 changes: 2 additions & 0 deletions packages/models/src/actor/actor-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class ActorReference {
return ref
}

static newWithFilter = this.newWithPattern

#name: ActorName
#path: string
#protocol: string
Expand Down
11 changes: 11 additions & 0 deletions packages/models/src/utils/decorator/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
10 changes: 5 additions & 5 deletions packages/samples/mvp-dapp/src/actors/omnilock.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
OutPointString,
SchemaPattern,
UpdateStorageValue,
DataPattern,
LockPattern,
DataFilter,
LockFilter,
JSONStorage,
} from '@ckb-js/kuai-models'
import { BI } from '@ckb-lumos/bi'
Expand All @@ -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<Record<string, never>> {
#omnilockAddress: string
Expand All @@ -45,7 +45,7 @@ export class OmnilockModel extends JSONStore<Record<string, never>> {
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')
}
Expand Down
10 changes: 5 additions & 5 deletions packages/samples/mvp-dapp/src/actors/record.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'] } }> {
Expand All @@ -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}/`),
},
)

Expand Down

0 comments on commit b332aea

Please sign in to comment.