chore(core): 页面变化时仅移除配置的事件

This commit is contained in:
roymondchen 2023-03-29 16:34:20 +08:00
parent 37045f7201
commit 5eb3b0dcae

View File

@ -68,6 +68,8 @@ class App extends EventEmitter {
public eventQueueMap: Record<string, EventCache[]> = {}; public eventQueueMap: Record<string, EventCache[]> = {};
private eventList: { [name: string]: (fromCpt: Node, ...args: any[]) => void } = {};
constructor(options: AppOptionsConfig) { constructor(options: AppOptionsConfig) {
super(); super();
@ -175,6 +177,7 @@ class App extends EventEmitter {
this.page.destroy(); this.page.destroy();
this.page = undefined; this.page = undefined;
} }
super.emit('page-change');
return; return;
} }
@ -189,6 +192,8 @@ class App extends EventEmitter {
app: this, app: this,
}); });
super.emit('page-change', this.page);
if (this.platform !== 'magic') { if (this.platform !== 'magic') {
this.bindEvents(); this.bindEvents();
} }
@ -223,20 +228,25 @@ class App extends EventEmitter {
} }
public bindEvents() { public bindEvents() {
Object.entries(this.eventList).forEach(([name, handler]) => {
this.off(name, handler);
});
this.eventList = {};
if (!this.page) return; if (!this.page) return;
this.removeAllListeners();
for (const [, value] of this.page.nodes) { 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) { this.on(eventName, eventHanlder);
const { name } = event; });
this.on(`${name}_${id}`, async (fromCpt: Node, ...args) => { }
await this.eventHandler(event, fromCpt, args);
});
} }
public emit(name: string | symbol, node: any, ...args: any[]): boolean { public emit(name: string | symbol, node: any, ...args: any[]): boolean {