mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2026-01-26 03:38:11 +00:00
refactor(core): 调整eventHelper代码
This commit is contained in:
parent
5110f0cbca
commit
e6e68ae69a
@ -197,17 +197,7 @@ class App extends EventEmitter {
|
||||
app: this,
|
||||
});
|
||||
|
||||
if (this.eventHelper) {
|
||||
this.eventHelper.removeNodeEvents();
|
||||
for (const [, node] of this.page.nodes) {
|
||||
this.eventHelper.bindNodeEvents(node);
|
||||
}
|
||||
for (const [, page] of this.pageFragments) {
|
||||
for (const [, node] of page.nodes) {
|
||||
this.eventHelper.bindNodeEvents(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.eventHelper?.initEvents();
|
||||
|
||||
super.emit('page-change', this.page);
|
||||
}
|
||||
@ -254,7 +244,7 @@ class App extends EventEmitter {
|
||||
node.data?.id &&
|
||||
node.eventKeys.has(`${String(name)}_${node.data.id}`)
|
||||
) {
|
||||
return this.eventHelper?.emit(node.eventKeys.get(`${String(name)}_${node.data.id}`)!, node, ...otherArgs);
|
||||
return this.eventHelper.emit(node.eventKeys.get(`${String(name)}_${node.data.id}`)!, node, ...otherArgs);
|
||||
}
|
||||
return super.emit(name, ...args);
|
||||
}
|
||||
@ -265,8 +255,12 @@ class App extends EventEmitter {
|
||||
* @returns void
|
||||
*/
|
||||
public async runCode(codeId: Id, params: Record<string, any>, args: any[], flowState?: FlowState, node?: Node) {
|
||||
if (!codeId || isEmpty(this.codeDsl)) return;
|
||||
if (!codeId || isEmpty(this.codeDsl)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const content = this.codeDsl?.[codeId]?.content;
|
||||
|
||||
if (typeof content === 'function') {
|
||||
try {
|
||||
await content({ app: this, params, eventParams: args, flowState, node });
|
||||
|
||||
@ -82,6 +82,22 @@ export default class EventHelper extends EventEmitter {
|
||||
this.dataSourceEventList.clear();
|
||||
}
|
||||
|
||||
public initEvents() {
|
||||
this.removeNodeEvents();
|
||||
|
||||
if (this.app.page) {
|
||||
for (const [, node] of this.app.page.nodes) {
|
||||
this.bindNodeEvents(node);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [, page] of this.app.pageFragments) {
|
||||
for (const [, node] of page.nodes) {
|
||||
this.bindNodeEvents(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bindNodeEvents(node: TMagicNode) {
|
||||
node.events?.forEach((event, index) => {
|
||||
if (!event.name) {
|
||||
@ -301,11 +317,11 @@ export default class EventHelper extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
const instanceMethodPropmise = [];
|
||||
const instanceMethodPromise = [];
|
||||
for (const node of toNodes) {
|
||||
if (node.instance) {
|
||||
if (typeof node.instance[methodName] === 'function') {
|
||||
instanceMethodPropmise.push(node.instance[methodName](fromCpt, ...args));
|
||||
instanceMethodPromise.push(node.instance[methodName](fromCpt, ...args));
|
||||
}
|
||||
} else {
|
||||
node.addEventToQueue({
|
||||
@ -316,6 +332,6 @@ export default class EventHelper extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(instanceMethodPropmise);
|
||||
await Promise.all(instanceMethodPromise);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user