我有两个观察点。一个将获取初始数据,另一个将对更改做出反应并相应地应用它们:
const initial$: Observable<Report[]>;
const changes$: Observable<ObjectChangeEvent<Report>>;
一些特征是:
我想将两个可观察值合并为一个。我到目前为止最接近的是 combineLatest 运算符。但这与特征2)冲突,因为 changes $ 可能不会发出任何东西。
任何帮助将不胜感激。
答案 0 :(得分:1)
您的意思是这样吗?
this.result$ = this.changes$.pipe(
skipUntil(this.initial$),
// ...
);
或者...
const initial$: Observable<Report[]>;
const changes$: Observable<ObjectChangeEvent<Report>>;
const accumulatedChanges$: Observable<ObjectChangeEvent<Report>[]>;
this.accumulatedChanges$ = this.changes$.pipe(
scan((acc, curr) => [...acc, curr], []),
startWith([]),
); // emits [change1], [change1, change2], [change1, change2, change3]....
this.result$ = combineLatest([this.initial$, this.accumulatedChanges$]).pipe(
// apply all accumulated changes on initial
);
编辑:
OR ....
this.result$ = this.initial$.pipe(
switchMap(initial => this.changes$.pipe(
// applyChangeToCurrent is only a placeholder for your semantic to alter the current
scan((current, change) => applyChangeToCurrent(current, change), initial),
)),
);
答案 1 :(得分:0)