feat(core): 修改core生命周期钩子处理逻辑,执行对应的代码块

This commit is contained in:
parisma 2022-09-14 20:08:16 +08:00 committed by jia000
parent bc000e9ca6
commit 310ee32d75
3 changed files with 31 additions and 20 deletions

View File

@ -18,7 +18,7 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import type { EventItemConfig, Id, MApp } from '@tmagic/schema'; import type { CodeBlockDSL, EventItemConfig, Id, MApp } from '@tmagic/schema';
import Env from './Env'; import Env from './Env';
import { bindCommonEventListener, isCommonMethod, triggerCommonMethod } from './events'; import { bindCommonEventListener, isCommonMethod, triggerCommonMethod } from './events';
@ -44,7 +44,7 @@ interface EventCache {
class App extends EventEmitter { class App extends EventEmitter {
public env; public env;
public codeDsl: CodeBlockDSL | undefined;
public pages = new Map<Id, Page>(); public pages = new Map<Id, Page>();
public page: Page | undefined; public page: Page | undefined;
@ -140,8 +140,8 @@ class App extends EventEmitter {
* @param curPage id * @param curPage id
*/ */
public setConfig(config: MApp, curPage?: Id) { public setConfig(config: MApp, curPage?: Id) {
this.codeDsl = config.methods;
this.pages = new Map(); this.pages = new Map();
config.items?.forEach((page) => { config.items?.forEach((page) => {
this.pages.set( this.pages.set(
page.id, page.id,

View File

@ -51,7 +51,6 @@ class Node extends EventEmitter {
const { events } = options.config; const { events } = options.config;
this.data = options.config; this.data = options.config;
this.events = events; this.events = events;
this.listenLifeSafe(); this.listenLifeSafe();
this.once('destroy', () => { this.once('destroy', () => {
@ -65,15 +64,20 @@ class Node extends EventEmitter {
} }
private listenLifeSafe() { private listenLifeSafe() {
this.once('created', (instance: any) => { this.once('created', async (instance: any) => {
this.instance = instance; this.instance = instance;
if (Array.isArray(this.data.created)) {
if (typeof this.data.created === 'function') { await Promise.all(
this.data.created(this); this.data.created.map(async (codeId) => {
if (this.app?.codeDsl[codeId] && typeof this.app?.codeDsl[codeId]?.content === 'function') {
await this.app.codeDsl[codeId].content(this);
}
}),
);
} }
}); });
this.once('mounted', (instance: any) => { this.once('mounted', async (instance: any) => {
this.instance = instance; this.instance = instance;
const eventConfigQueue = this.app.eventQueueMap[instance.config.id] || []; const eventConfigQueue = this.app.eventQueueMap[instance.config.id] || [];
@ -82,8 +86,14 @@ class Node extends EventEmitter {
this.app.eventHandler(eventConfig.eventConfig, eventConfig.fromCpt, eventConfig.args); this.app.eventHandler(eventConfig.eventConfig, eventConfig.fromCpt, eventConfig.args);
} }
if (typeof this.data.mounted === 'function') { if (Array.isArray(this.data.mounted)) {
this.data.mounted(this); await Promise.all(
this.data.mounted.map(async (codeId) => {
if (this.app?.codeDsl[codeId] && typeof this.app?.codeDsl[codeId]?.content === 'function') {
await this.app.codeDsl[codeId].content(this);
}
}),
);
} }
}); });
} }

View File

@ -23,7 +23,8 @@ export default {
methods: { methods: {
l7znj1q24wilb357ay6: { l7znj1q24wilb357ay6: {
name: 'getData', name: 'getData',
content: `() => {\n console.log("this is getData function")\n}`, // eslint-disable-next-line no-eval
content: eval(`(vm) => {\n console.log("this is getData function")\n}`),
}, },
}, },
items: [ items: [
@ -50,7 +51,7 @@ export default {
fontWeight: '', fontWeight: '',
}, },
events: [], events: [],
created: '', created: ['l7znj1q24wilb357ay6'],
items: [ items: [
{ {
type: 'text', type: 'text',
@ -75,7 +76,7 @@ export default {
text: 'Tmagic editor 营销活动编辑器', text: 'Tmagic editor 营销活动编辑器',
multiple: true, multiple: true,
events: [], events: [],
created: '', created: [],
}, },
{ {
type: 'qrcode', type: 'qrcode',
@ -99,7 +100,7 @@ export default {
name: '二维码', name: '二维码',
url: 'https://github.com/Tencent/tmagic-editor', url: 'https://github.com/Tencent/tmagic-editor',
events: [], events: [],
created: '', created: [],
}, },
{ {
type: 'img', type: 'img',
@ -124,7 +125,7 @@ export default {
src: 'https://vfiles.gtimg.cn/vupload/20210811/388ed01628667545737.png', src: 'https://vfiles.gtimg.cn/vupload/20210811/388ed01628667545737.png',
url: '', url: '',
events: [], events: [],
created: '', created: [],
}, },
{ {
type: 'button', type: 'button',
@ -156,7 +157,7 @@ export default {
method: 'openOverlay', method: 'openOverlay',
}, },
], ],
created: '', created: [],
}, },
{ {
type: 'overlay', type: 'overlay',
@ -222,7 +223,7 @@ export default {
method: 'closeOverlay', method: 'closeOverlay',
}, },
], ],
created: '', created: [],
}, },
{ {
type: 'img', type: 'img',
@ -261,12 +262,12 @@ export default {
text: 'Tmagic', text: 'Tmagic',
multiple: false, multiple: false,
events: [], events: [],
created: '', created: [],
}, },
], ],
layout: 'absolute', layout: 'absolute',
events: [], events: [],
created: '', created: [],
}, },
], ],
}, },