From 310ee32d75bc9d87c8558be3c35be971716aec9a Mon Sep 17 00:00:00 2001 From: parisma Date: Wed, 14 Sep 2022 20:08:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E4=BF=AE=E6=94=B9core=E7=94=9F?= =?UTF-8?q?=E5=91=BD=E5=91=A8=E6=9C=9F=E9=92=A9=E5=AD=90=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91,=E6=89=A7=E8=A1=8C=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/App.ts | 6 +++--- packages/core/src/Node.ts | 26 ++++++++++++++++++-------- playground/src/configs/dsl.ts | 19 ++++++++++--------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index 9bd559a3..55298321 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -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(); 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, diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index 340a98c2..d3a4e5a1 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -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); + } + }), + ); } }); } diff --git a/playground/src/configs/dsl.ts b/playground/src/configs/dsl.ts index d38f1469..0483697d 100644 --- a/playground/src/configs/dsl.ts +++ b/playground/src/configs/dsl.ts @@ -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: [], }, ], },