我开发了一个 Angular 9 应用程序,但我不明白如何将shareReplay运算符与其他运算符一起使用。我做了类似以下的事情:
if (!this.cache[key]) {
this.cache[key] = this.http.get(...).pipe(
shareReplay(1),
flatMap(...),
map(...),
reduce(...)
);
}
return this.cache[key];
Run Code Online (Sandbox Code Playgroud)
此后我的应用程序的 CPU 使用率一直为 100%。当我将其更改为:
if (!this.cache[key]) {
this.cache[key] = this.http.get(...).pipe(
flatMap(...),
map(...),
reduce(...),
shareReplay(1)
);
}
return this.cache[key];
Run Code Online (Sandbox Code Playgroud)
看起来效果很好。是否有必要将shareReplay运算符作为最后一个使用?如此高的 CPU 使用率从何而来?
编辑:更详细的代码片段:
this.http.get(...).pipe(
// I would like to avoid several same http calls
shareReplay(1),
// I would like to flatten an array of objects that comes from backend
flatMap(option => option),
// I need to map all objects …Run Code Online (Sandbox Code Playgroud)