From 5eb3b0dcae5762ada6c2bb3e032b8acafbbb7c73 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Wed, 29 Mar 2023 16:34:20 +0800 Subject: [PATCH] =?UTF-8?q?chore(core):=20=E9=A1=B5=E9=9D=A2=E5=8F=98?= =?UTF-8?q?=E5=8C=96=E6=97=B6=E4=BB=85=E7=A7=BB=E9=99=A4=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=9A=84=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/App.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index 974e28fb..6f8e2c5d 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -68,6 +68,8 @@ class App extends EventEmitter { public eventQueueMap: Record = {}; + private eventList: { [name: string]: (fromCpt: Node, ...args: any[]) => void } = {}; + constructor(options: AppOptionsConfig) { super(); @@ -175,6 +177,7 @@ class App extends EventEmitter { this.page.destroy(); this.page = undefined; } + super.emit('page-change'); return; } @@ -189,6 +192,8 @@ class App extends EventEmitter { app: this, }); + super.emit('page-change', this.page); + if (this.platform !== 'magic') { this.bindEvents(); } @@ -223,20 +228,25 @@ class App extends EventEmitter { } public bindEvents() { + Object.entries(this.eventList).forEach(([name, handler]) => { + this.off(name, handler); + }); + + this.eventList = {}; + if (!this.page) return; - this.removeAllListeners(); - for (const [, value] of this.page.nodes) { - value.events?.forEach((event) => this.bindEvent(event, `${value.data.id}`)); - } - } + value.events?.forEach((event) => { + const eventName = `${event.name}_${value.data.id}`; + const eventHanlder = (fromCpt: Node, ...args: any[]) => { + this.eventHandler(event, fromCpt, args); + }; + this.eventList[eventName] = eventHanlder; - public bindEvent(event: EventConfig | DeprecatedEventConfig, id: string) { - const { name } = event; - this.on(`${name}_${id}`, async (fromCpt: Node, ...args) => { - await this.eventHandler(event, fromCpt, args); - }); + this.on(eventName, eventHanlder); + }); + } } public emit(name: string | symbol, node: any, ...args: any[]): boolean {