From c7b27340901c9728195084ca88525b9102fc4855 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Mon, 26 May 2025 20:04:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(data-source):=20=E6=B2=A1=E6=9C=89=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E5=8A=A0=E8=BD=BD=E6=95=B0=E6=8D=AE=E6=BA=90=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=EF=BC=8C=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?init=E6=96=B9=E6=B3=95=E6=B2=A1=E6=9C=89=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/data-source/src/DataSourceManager.ts | 74 ++++++++++--------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/packages/data-source/src/DataSourceManager.ts b/packages/data-source/src/DataSourceManager.ts index 640366a8..bb4a47a4 100644 --- a/packages/data-source/src/DataSourceManager.ts +++ b/packages/data-source/src/DataSourceManager.ts @@ -96,39 +96,13 @@ class DataSourceManager extends EventEmitter { this.addDataSource(config); }); - 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 = {}; - - 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; - } - }); - - 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); - }); - } - }); + if (this.isAllDataSourceRegistered()) { + this.callDsInit(); + } else { + this.on('registered-all', () => { + this.callDsInit(); + }); + } } public async init(ds: DataSource) { @@ -350,6 +324,40 @@ class DataSourceManager extends EventEmitter { public offDataChange(id: string, path: string, callback: (newVal: any) => void) { return this.get(id)?.offDataChange(path, callback); } + + private callDsInit() { + 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 = {}; + + 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; + } + }); + + 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); + }); + } + } } export default DataSourceManager;