我决定重新实现我的解决方案,订阅这两个可观测数据,然后使用
filter
从
firstObservable
(已添加到缓存中)以便仅来自
secondObservable
受映射函数的影响:
const cache = {};
const firstObservable = // fetch data to be cached
const secondObservable = // fetch other data that will be decorated with cached data
// add all next events of the first observable to the cache
const waitForCache = firstObservable
.pipe(
tap({
next: data => {
cache[data.key] = data;
}
});
// use concat so the cache population is completed before dealing with the second observable
const decorated = concat(waitForCache, secondObservable)
.pipe(
filter(
data => { return /* check for some specific property that is
available in one observable, but not the other */ }
)
map(
data => {
// decorate objects with data from cache
return Object.assign({}, data, cache[data.key]);
}
));