From a35789c0cf1c740df9fb16bc302cf1aec50cdf2b Mon Sep 17 00:00:00 2001 From: roymondchen Date: Wed, 21 May 2025 16:24:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(data-source):=20=E7=94=B1=E4=BA=8E?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E5=BC=82=E6=AD=A5=E5=8A=A0=E8=BD=BD=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=BA=90=EF=BC=8C=E6=96=B0=E5=A2=9E=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=BA=90=E5=8A=A0=E8=BD=BD=E5=AE=8C=E6=AF=95?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data-source/src/DataSourceManager.ts | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/packages/data-source/src/DataSourceManager.ts b/packages/data-source/src/DataSourceManager.ts index 44685116..5ae1f0d6 100644 --- a/packages/data-source/src/DataSourceManager.ts +++ b/packages/data-source/src/DataSourceManager.ts @@ -89,45 +89,46 @@ class DataSourceManager extends EventEmitter { if (initialData) { this.initialData = initialData; - this.data = initialData; + this.data = { ...initialData }; } app.dsl?.dataSources?.forEach((config) => { this.addDataSource(config); }); - const dataSourceList = Array.from(this.dataSourceMap); + this.on('registered-all', () => { + const dataSourceList = Array.from(this.dataSourceMap); + if (typeof Promise.allSettled === 'function') { + Promise.allSettled>(dataSourceList.map(([, ds]) => this.init(ds))).then((values) => { + const data: DataSourceManagerData = {}; + const errors: Record = {}; - if (typeof Promise.allSettled === 'function') { - Promise.allSettled>(dataSourceList.map(([, ds]) => this.init(ds))).then((values) => { - const data: DataSourceManagerData = {}; - const errors: Record = {}; - - values.forEach((value, index) => { - const dsId = dataSourceList[index][0]; - if (value.status === 'fulfilled') { - if (this.data[dsId]) { - data[dsId] = this.data[dsId]; - } else { + values.forEach((value, index) => { + const dsId = dataSourceList[index][0]; + if (value.status === 'fulfilled') { + if (this.data[dsId]) { + data[dsId] = this.data[dsId]; + } else { + delete data[dsId]; + } + } else if (value.status === 'rejected') { delete data[dsId]; + errors[dsId] = value.reason; } - } else if (value.status === 'rejected') { - delete data[dsId]; - errors[dsId] = value.reason; - } - }); + }); - this.emit('init', data, errors); - }); - } else { - Promise.all>(dataSourceList.map(([, ds]) => this.init(ds))) - .then(() => { - this.emit('init', this.data); - }) - .catch(() => { - this.emit('init', this.data); + this.emit('init', data, errors); }); - } + } else { + Promise.all>(dataSourceList.map(([, ds]) => this.init(ds))) + .then(() => { + this.emit('init', this.data); + }) + .catch(() => { + this.emit('init', this.data); + }); + } + }); } public async init(ds: DataSource) { @@ -202,6 +203,10 @@ class DataSourceManager extends EventEmitter { this.setData(ds, changeEvent); }); + if (!this.app.dsl?.dataSources || this.dataSourceMap.size === this.app.dsl.dataSources.length) { + this.emit('registered-all'); + } + return ds; } @@ -221,20 +226,22 @@ class DataSourceManager extends EventEmitter { * @param {DataSourceSchema[]} schemas 所有数据源配置 */ public updateSchema(schemas: DataSourceSchema[]) { - schemas.forEach((schema) => { + for (const schema of schemas) { const ds = this.get(schema.id); if (!ds) { return; } this.removeDataSource(schema.id); + } + for (const schema of schemas) { this.addDataSource(cloneDeep(schema)); const newDs = this.get(schema.id); if (newDs) { this.init(newDs); } - }); + } } /**