Skip to content

Commit

Permalink
fix(data-source): 兼容Promise.allSettled
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Jun 13, 2024
1 parent 5873842 commit 7ee7f53
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions packages/data-source/src/DataSourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,32 @@ class DataSourceManager extends EventEmitter {
});

const dataSourceList = Array.from(this.dataSourceMap);
Promise.allSettled<Record<string, any>>(dataSourceList.map(([, ds]) => this.init(ds))).then((values) => {
const data: DataSourceManagerData = {};
const errors: Record<string, Error> = {};

values.forEach((value, index) => {
const dsId = dataSourceList[index][0];
if (value.status === 'fulfilled') {
data[dsId] = this.data[dsId];
} else if (value.status === 'rejected') {
errors[dsId] = value.reason;
}
});

this.emit('init', data, errors);
});
if (typeof Promise.allSettled === 'function') {
Promise.allSettled<Record<string, any>>(dataSourceList.map(([, ds]) => this.init(ds))).then((values) => {
const data: DataSourceManagerData = {};
const errors: Record<string, Error> = {};

values.forEach((value, index) => {
const dsId = dataSourceList[index][0];
if (value.status === 'fulfilled') {
data[dsId] = this.data[dsId];
} else if (value.status === 'rejected') {
errors[dsId] = value.reason;
}
});

this.emit('init', data, errors);
});
} else {
Promise.all<Record<string, any>>(dataSourceList.map(([, ds]) => this.init(ds)))
.then(() => {
this.emit('init', this.data);
})
.catch(() => {
this.emit('init', this.data);
});
}
}

public async init(ds: DataSource) {
Expand Down

0 comments on commit 7ee7f53

Please sign in to comment.