-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize the workflow of getting an model instance #160
Comments
What I though before was to implement this in |
I think currently it's right, but In the future users will not need to call find with |
From my understanding, what I designed and implemented is like this. I think, the class Actor {
constructor(ref?: ActorRef) {
const metadata: { ref: ActorRef | undefined } = Reflect.getMetadata(ProviderKey.Actor, this.constructor)
// TODO: add explicit error message
ref = ref ? ref : metadata?.ref
if (!ref) throw new Error()
this.#ref = ref
this.receiveMail()
}
}
constructor(
ref?: ActorRef,
schemaOption?: GetStorageOption<StructSchema>,
params?: {
states?: Record<OutPointString, GetStorageStruct<StructSchema>>
chainData?: Record<OutPointString, UpdateStorageValue>
cellPattern?: CellPattern
schemaPattern?: SchemaPattern
options?: Option
},
) {
super(ref)
this.cellPattern = Reflect.getMetadata(ProviderKey.CellPattern, this.constructor, this.ref.uri) || params?.cellPattern
this.schemaPattern = Reflect.getMetadata(ProviderKey.SchemaPattern, this.constructor) || params?.schemaPattern
this.schemaOption = schemaOption
this.states = params?.states || {}
this.chainData = params?.chainData || {}
this.options = params?.options
this.#lock = Reflect.getMetadata(ProviderKey.LockPattern, this.constructor, this.ref.uri)
this.#type = Reflect.getMetadata(ProviderKey.TypePattern, this.constructor, this.ref.uri)
} For registry, I think it is customized for class Registry {
find = async <T = Actor>(ref: ActorRef, module: new (...args: Array<unknown>) => unknown): Promise<T | undefined> => {
try {
let actor = this.#container.get<T>(ref.uri)
if (!actor) {
this.#bind(module, { ref })
actor = this.#container.get<T>(ref.uri)
const resourceBindingRegister = Reflect.getMetadata(ProviderKey.ResourceBindingRegister, ref.uri)
if (resourceBindingRegister) {
await (actor as Actor).call('local://resource', resourceBindingRegister)
}
}
return actor
} catch (e) {
console.log('Registry `find` catch error', e)
return undefined
}
}
bind = (module: new (...args: Array<unknown>) => unknown): void =>
this.#bind(module, Reflect.getMetadata(ProviderKey.Actor, module))
#bind = (module: new (...args: Array<unknown>) => unknown, metadata?: Record<'ref', ActorRef>): void => {
}
} The implementation is still in test now. Please correct me if I was wrong. |
It seems that the |
The gola of this design is
So a |
Yes, but actually, not only one sub class will be declared before, like this. First, a
Then, a sub class which extended the
Finally, the registry initiated the As I could not find a way to pass metadata by both |
The // class NewStore extends OmnilockModel {}
// Reflect.defineMetadata(ProviderKey.Actor, { ref: actorRef }, NewStore)
// Reflect.defineMetadata(ProviderKey.CellPattern, createCellPattern(lock), NewStore)
// Reflect.defineMetadata(ProviderKey.LockPattern, lock, NewStore)
@Ref(`${omnilock_code_hash}/${omnilock_hash_type}/:args`)
@CellPattern({ codeHash: 'custom_code_hash', hashType: 'custom_hash_type' })
@LockPattern({ codeHash: '...', hashType: '...' })
class OmnilockModel {
constructor(@Params('lock') lock: Omit<Script, 'args'>, @Params('args') args: string) {
this.lock = { ...lock, args }
}
} And in the registry, when a request hits ref The idea was from route parameters of nestjs, ref: https://docs.nestjs.com/controllers#route-parameters |
I modify the And after implementing the CellPattern and LockPattern, I find out that it is not easy to use because it is too customization. So I think it is better for developers to define the logic in their own sub |
Now in the mvp dapp, an instance of model is getting by method defined in the controller
kuai/packages/samples/mvp-dapp/src/app.controller.ts
Lines 67 to 87 in 91a5ad3
It returns an instance if one has been registered or instantiates one if there's no live model.
This logic could be encapsulated in the registry, so users don't have to write it in different dapps repeatedly.
The text was updated successfully, but these errors were encountered: