feat(core): instance设定不再限制只有方法

This commit is contained in:
roymondchen 2025-04-28 20:11:28 +08:00
parent 6dc007388b
commit e5bcd762b2
4 changed files with 28 additions and 12 deletions

View File

@ -73,7 +73,11 @@ class Node extends EventEmitter {
const { events, style } = data; const { events, style } = data;
this.events = events || []; this.events = events || [];
this.style = style || {}; this.style = style || {};
this.instance.config = data; try {
this.instance.config = data;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e: any) {}
this.emit('update-data', data); this.emit('update-data', data);
} }
@ -81,6 +85,9 @@ class Node extends EventEmitter {
this.eventQueue.push(event); this.eventQueue.push(event);
} }
/**
* @deprecated use setInstance instead
*/
public registerMethod(methods: Methods) { public registerMethod(methods: Methods) {
if (!methods) { if (!methods) {
return; return;
@ -97,6 +104,10 @@ class Node extends EventEmitter {
} }
} }
public setInstance(instance: any) {
this.instance = instance;
}
public async runHookCode(hook: string, params?: Record<string, any>) { public async runHookCode(hook: string, params?: Record<string, any>) {
if (typeof this.data[hook] === 'function') { if (typeof this.data[hook] === 'function') {
// 兼容旧的数据格式 // 兼容旧的数据格式
@ -144,6 +155,10 @@ class Node extends EventEmitter {
} }
public destroy() { public destroy() {
this.eventQueue.length = 0;
this.instance = null;
this.events = [];
this.style = {};
this.removeAllListeners(); this.removeAllListeners();
} }
@ -159,10 +174,7 @@ class Node extends EventEmitter {
}); });
if (instance) { if (instance) {
this.registerMethod(instance); this.setInstance(instance);
if (instance.config) {
this.setData(instance.config);
}
} }
this.runHookCode('created'); this.runHookCode('created');
@ -171,10 +183,7 @@ class Node extends EventEmitter {
this.once('mounted', (instance: any) => { this.once('mounted', (instance: any) => {
const handler = async () => { const handler = async () => {
if (instance) { if (instance) {
this.registerMethod(instance); this.setInstance(instance);
if (instance.config) {
this.setData(instance.config);
}
} }
for (let eventConfig = this.eventQueue.shift(); eventConfig; eventConfig = this.eventQueue.shift()) { for (let eventConfig = this.eventQueue.shift(); eventConfig; eventConfig = this.eventQueue.shift()) {

View File

@ -106,9 +106,16 @@ class Page extends Node {
} }
public destroy(): void { public destroy(): void {
super.destroy(); this.nodes.forEach((node) => {
if (node === this) {
return;
}
node.destroy();
});
this.nodes.clear(); this.nodes.clear();
super.destroy();
} }
} }

View File

@ -1,5 +1,5 @@
{ {
"version": "0.1.0", "version": "0.1.1",
"name": "@tmagic/vue-page", "name": "@tmagic/vue-page",
"type": "module", "type": "module",
"main": "src/index.ts", "main": "src/index.ts",

View File

@ -73,7 +73,7 @@ export default defineComponent({
const node = useNode({ config: { ...config, [IS_DSL_NODE_KEY]: true } }, app); const node = useNode({ config: { ...config, [IS_DSL_NODE_KEY]: true } }, app);
if (config.id !== preConfig?.id) { if (config.id !== preConfig?.id) {
node?.registerMethod({ refresh }); node?.setInstance({ config: props.config, refresh });
node?.emit('created'); node?.emit('created');
} }
await nextTick(); await nextTick();