diff --git a/packages/core/package.json b/packages/core/package.json index a0d86411..5c127444 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -40,7 +40,9 @@ }, "devDependencies": { "@types/events": "^3.0.0", + "@types/lodash-es": "^4.17.4", "@types/node": "^15.12.4", + "lodash-es": "^4.17.21", "rimraf": "^3.0.2", "typescript": "^4.7.4", "vite": "^3.1.3" diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index d73112de..622cfb4b 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -18,6 +18,8 @@ import { EventEmitter } from 'events'; +import { isEmpty } from 'lodash-es'; + import type { EventItemConfig, MComponent, MContainer, MPage } from '@tmagic/schema'; import type App from './App'; @@ -66,15 +68,7 @@ class Node extends EventEmitter { private listenLifeSafe() { this.once('created', async (instance: any) => { this.instance = instance; - if (Array.isArray(this.data.created) && this.app?.codeDsl) { - 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); - } - }), - ); - } + await this.runCodeBlock('created'); }); this.once('mounted', async (instance: any) => { @@ -86,17 +80,18 @@ class Node extends EventEmitter { this.app.eventHandler(eventConfig.eventConfig, eventConfig.fromCpt, eventConfig.args); } - 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); - } - }), - ); - } + await this.runCodeBlock('mounted'); }); } + + private async runCodeBlock(hook: string) { + if (!Array.isArray(this.data[hook]) || isEmpty(this.app?.codeDsl)) return; + for (const codeId of this.data[hook]) { + if (this.app?.codeDsl[codeId] && typeof this.app?.codeDsl[codeId]?.content === 'function') { + await this.app.codeDsl[codeId].content(this); + } + } + } } export default Node; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3823fbda..fcca687e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -136,8 +136,10 @@ importers: specifiers: '@tmagic/schema': 1.1.6 '@types/events': ^3.0.0 + '@types/lodash-es': ^4.17.4 '@types/node': ^15.12.4 events: ^3.3.0 + lodash-es: ^4.17.21 rimraf: ^3.0.2 typescript: ^4.7.4 vite: ^3.1.3 @@ -146,7 +148,9 @@ importers: events: 3.3.0 devDependencies: '@types/events': 3.0.0 + '@types/lodash-es': 4.17.6 '@types/node': 15.14.9 + lodash-es: 4.17.21 rimraf: 3.0.2 typescript: 4.7.4 vite: 3.1.3 @@ -5559,7 +5563,6 @@ packages: /lodash-es/4.17.21: resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==} - dev: false /lodash-unified/1.0.2_3ib2ivapxullxkx3xftsimdk7u: resolution: {integrity: sha512-OGbEy+1P+UT26CYi4opY4gebD8cWRDxAT6MAObIVQMiqYdxZr1g3QHWCToVsm31x2NkLS4K3+MC2qInaRMa39g==}