diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index 664d9ca5..9483b96c 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -32,6 +32,10 @@ interface EventCache { args: any[]; } +interface Methods { + [key: string]: (...args: any[]) => any; +} + export interface NodeOptions { config: MNode; page?: Page; @@ -45,7 +49,7 @@ class Node extends EventEmitter { [key: string]: any; }; public events: EventConfig[] = []; - public instance?: any; + public instance?: any = {}; public page?: Page; public parent?: Node; public app: TMagicApp; @@ -69,6 +73,7 @@ class Node extends EventEmitter { const { events, style } = data; this.events = events || []; this.style = style || {}; + this.instance.config = data; this.emit('update-data', data); } @@ -76,6 +81,22 @@ class Node extends EventEmitter { this.eventQueue.push(event); } + public registerMethod(methods: Methods) { + if (!methods) { + return; + } + + if (!this.instance) { + this.instance = {}; + } + + for (const [key, fn] of Object.entries(methods)) { + if (typeof fn === 'function') { + this.instance[key] = fn; + } + } + } + public async runHookCode(hook: string, params?: Record) { if (typeof this.data[hook] === 'function') { // 兼容旧的数据格式 @@ -137,12 +158,23 @@ class Node extends EventEmitter { this.listenLifeSafe(); }); - this.instance = instance; + if (instance) { + this.registerMethod(instance); + if (instance.config) { + this.setData(instance.config); + } + } + await this.runHookCode('created'); }); this.once('mounted', async (instance: any) => { - this.instance = instance; + if (instance) { + this.registerMethod(instance); + if (instance.config) { + this.setData(instance.config); + } + } for (let eventConfig = this.eventQueue.shift(); eventConfig; eventConfig = this.eventQueue.shift()) { if (typeof instance[eventConfig.method] === 'function') {