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

View File

@ -51,7 +51,6 @@ class Node extends EventEmitter {
const { events } = options.config;
this.data = options.config;
this.events = events;
this.listenLifeSafe();
this.once('destroy', () => {
@ -65,15 +64,20 @@ class Node extends EventEmitter {
}
private listenLifeSafe() {
this.once('created', (instance: any) => {
this.once('created', async (instance: any) => {
this.instance = instance;
if (typeof this.data.created === 'function') {
this.data.created(this);
if (Array.isArray(this.data.created)) {
await Promise.all(
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;
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);
}
if (typeof this.data.mounted === 'function') {
this.data.mounted(this);
if (Array.isArray(this.data.mounted)) {
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: {
l7znj1q24wilb357ay6: {
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: [
@ -50,7 +51,7 @@ export default {
fontWeight: '',
},
events: [],
created: '',
created: ['l7znj1q24wilb357ay6'],
items: [
{
type: 'text',
@ -75,7 +76,7 @@ export default {
text: 'Tmagic editor 营销活动编辑器',
multiple: true,
events: [],
created: '',
created: [],
},
{
type: 'qrcode',
@ -99,7 +100,7 @@ export default {
name: '二维码',
url: 'https://github.com/Tencent/tmagic-editor',
events: [],
created: '',
created: [],
},
{
type: 'img',
@ -124,7 +125,7 @@ export default {
src: 'https://vfiles.gtimg.cn/vupload/20210811/388ed01628667545737.png',
url: '',
events: [],
created: '',
created: [],
},
{
type: 'button',
@ -156,7 +157,7 @@ export default {
method: 'openOverlay',
},
],
created: '',
created: [],
},
{
type: 'overlay',
@ -222,7 +223,7 @@ export default {
method: 'closeOverlay',
},
],
created: '',
created: [],
},
{
type: 'img',
@ -261,12 +262,12 @@ export default {
text: 'Tmagic',
multiple: false,
events: [],
created: '',
created: [],
},
],
layout: 'absolute',
events: [],
created: '',
created: [],
},
],
},