我有一个 RxJS Observable,它对底层数据结构发出一系列更改 - 具体来说,snapshotChanges() from an AngularFirestoreCollection .
- 我目前正在将其映射到一组纯 JavaScript 对象以供我的应用使用。
- 此数组不受任何方式保护,使用代码可能会意外修改此结构。
- 每当底层数据源发出时,都会重建整个数组,即使数组中只有一项(有时甚至没有)实际发生更改。
- 因此,所有引用每次都会发生变化,使得更改检测变得比实际需要的更加困难,并且确实减慢了我的应用的速度。
我想做的是使用 Immer维护不可变的结构,以便未更改的数据在结构上与"new"数组共享。
我无法解决的是如何将 pipe()
关闭 snapshotChanges()
observable,以便管道可以访问先前发出的不可变数据(或首次默认)除了最新的 snapshotChanges()
输出。
在代码中,我基本上已经拥有的是:
const docToObject = (doc) => { /* change document to fresh plain object every time */ };
const mappedData$ = snapshotChanges().pipe(
map(changes => changes.map(change => docToObject(change.payload.doc)),
tap(array => console.log('mutable array:', array)),
);
我本质上是在寻找这样的东西,但我不知道 XXX(...)
应该是什么:
const newImmutableObject = (changes, old) => {
// new immutable structure from old one + changes, structurally sharing as much as
// possible
};
const mappedData$ = snapshotChanges().pipe(
// ==================================================================================
XXX(...), // missing ingredient to combine snapshotChanges and previously emitted
// value, or default to []
// ==================================================================================
map(([snapshotChanges, prevImmutableOutput]) => newImmutableOutput(...)),
tap(array => console.log('IMMUTABLE ARRAY with shared structure:', array)),
);
我感觉像the expand
operator接近我需要的,但它似乎只在后续运行中传递先前发出的值,而我还需要新发出的 snapshotChanges
。
给定一个 RxJS Observable 管道,我如何操作此 Observable 的排放,同时还可以访问该管道之前的排放?
最佳答案
根据您的要求,我建议使用 scan
运算符,它可以跟踪所有以前的状态和新状态。
const newImmutableObject = (changes, old) => {
// new immutable structure from old one + changes, structurally sharing as much as
// possible
};
const mappedData$ = snapshotChanges().pipe(
scan((acc, current) => [...acc, current], []), //<-- scan is used here
map(([snapshotChanges, prevImmutableOutput]) => newImmutableOutput(...)),
tap(array => console.log('IMMUTABLE ARRAY with shared structure:', array)),
);
关于angular - 如何让 RxJS observable 管道访问原始 observable 的发射和管道之前的发射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53235649/