RxJS mergeMap 等待内部 Observables
RxJS mergeMap wait for inner Observables
我有一个解决方案,起初似乎有效,但后来我在慢速网络下尝试了它:
public getKeyFigureValuesForAllClients(keyFigurename: string) {
const keyFigureDefintion$ = this.keyFigureDefintions$.pipe(
flatMap((keyFigure) => keyFigure),
filter((x) => x.name === keyFigurename),
take(1));
const clients$ = this.dataService.getClients().pipe(
flatMap((client) => client));
const keyFigureAndClients$ = combineLatest([keyFigureDefintion$, clients$]);
return keyFigureAndClients$.pipe(
tap(x=>console.log(x)),
switchMap(([keyfigure, client]) =>
this.dataService.getDataPointsByPeriodName(client.id, this.currentPeriodName).pipe(
map((datapoints) => ({
client,
datapoints,
keyFigureCalculator: new KeyFigureCalculator(keyfigure, datapoints),
}),
)),
),
tap(x=>console.log(x)),
toArray(),
);
}
所以首先我加载我的关键人物和客户列表。然后我想得到相应的数据点和 return 一个包含我所有值的数组。
但似乎 this.dataService.getDataPointsByPeriodName()
需要更长的时间,所以我只得到第一个客户的值。如果我在 Chrome 中将网络速度更改为“在线”,一切正常(10 次尝试中有 9 次...)。
有等待接线员吗?我找到了一些关于 concatAll() 的信息,但我不知道将它放在哪里以及如何让它工作。
老实说,这有点奇怪,您将客户端流“只是”扁平化以便稍后再次使用 toArray()
。也许这种方法的某个地方是所描述行为的原因,但我不能肯定地说。请在下面找到没有 flatMap
的建议
public getKeyFigureValuesForAllClients(keyFigurename: string) {
const keyFigureDefintion$ = this.keyFigureDefintions$.pipe(
map(keyFigures => keyFigures.find(keyFigure => keyFigure.name === keyFigurename)),
)
const clients$ = this.dataService.getClients();
const keyFigureAndClients$ = forkJoin([keyFigureDefintion$, clients$]).pipe(
switchMap(([keyfigure, clients]) => {
return forkJoin(clients.map(client => this.dataService.getDataPointsByPeriodName(client.id, this.currentPeriodName).pipe(
map((datapoints) => ({
client,
datapoints,
keyFigureCalculator: new KeyFigureCalculator(keyfigure, datapoints),
}),
))));
}),
);
}
我有一个解决方案,起初似乎有效,但后来我在慢速网络下尝试了它:
public getKeyFigureValuesForAllClients(keyFigurename: string) {
const keyFigureDefintion$ = this.keyFigureDefintions$.pipe(
flatMap((keyFigure) => keyFigure),
filter((x) => x.name === keyFigurename),
take(1));
const clients$ = this.dataService.getClients().pipe(
flatMap((client) => client));
const keyFigureAndClients$ = combineLatest([keyFigureDefintion$, clients$]);
return keyFigureAndClients$.pipe(
tap(x=>console.log(x)),
switchMap(([keyfigure, client]) =>
this.dataService.getDataPointsByPeriodName(client.id, this.currentPeriodName).pipe(
map((datapoints) => ({
client,
datapoints,
keyFigureCalculator: new KeyFigureCalculator(keyfigure, datapoints),
}),
)),
),
tap(x=>console.log(x)),
toArray(),
);
}
所以首先我加载我的关键人物和客户列表。然后我想得到相应的数据点和 return 一个包含我所有值的数组。
但似乎 this.dataService.getDataPointsByPeriodName()
需要更长的时间,所以我只得到第一个客户的值。如果我在 Chrome 中将网络速度更改为“在线”,一切正常(10 次尝试中有 9 次...)。
有等待接线员吗?我找到了一些关于 concatAll() 的信息,但我不知道将它放在哪里以及如何让它工作。
老实说,这有点奇怪,您将客户端流“只是”扁平化以便稍后再次使用 toArray()
。也许这种方法的某个地方是所描述行为的原因,但我不能肯定地说。请在下面找到没有 flatMap
public getKeyFigureValuesForAllClients(keyFigurename: string) {
const keyFigureDefintion$ = this.keyFigureDefintions$.pipe(
map(keyFigures => keyFigures.find(keyFigure => keyFigure.name === keyFigurename)),
)
const clients$ = this.dataService.getClients();
const keyFigureAndClients$ = forkJoin([keyFigureDefintion$, clients$]).pipe(
switchMap(([keyfigure, clients]) => {
return forkJoin(clients.map(client => this.dataService.getDataPointsByPeriodName(client.id, this.currentPeriodName).pipe(
map((datapoints) => ({
client,
datapoints,
keyFigureCalculator: new KeyFigureCalculator(keyfigure, datapoints),
}),
))));
}),
);
}