所以withLatestFrom非常接近我需要的东西,但是它有一个奇怪的行为。仅当"master“在"slave”之后发出时,它才发出一个值。如下大理石图所示:
--1----2-------2---- (source)
----a-------b------- (other1)
-------2a------2b--
我需要一个运算符,它的行为如下:
--1------2-------2---- (source)
----a---------b------- (other1)
----1a---2a------2b--
还有这个:
------1------2-------2---- (source)
--a--------------b------- (other1)
------1a-----2a------2b--
换句话说,它基本上需要在第一次发射时像CombineLatest一样工作,然后在以后像withLatestFrom一样工作。如何实现此行为?
发布于 2020-11-23 02:16:11
我不认为有特定的运算符,但请尝试下面的代码片段。
试试这个:
combineLatest(
source,
other1.pipe(
take(1), // only take one emission, the take(1) might kill this observable and stop further emissions but give it a try
)
).pipe(
withLatestFrom(other1),
).subscribe(result => {
console.log(result);
const source = result[0][0]; // I am not sure how to destructure it but I suspect
const other1 = result[1]; // result will be a 2d array in the form of
// [[source, firstEmissionOther1], other1]
console.log(source);
console.log(other1);
})
发布于 2020-11-23 12:16:47
您可以尝试使用成对运算符
combineLatest([master$, slave$])
.pipe(
startWith([null, null]),
pairwise(),
filter(([prev, curr]) => curr[0] !== prev[0]),
map(([prev, curr]) => curr)
)
.subscribe(result => console.log(JSON.stringify(result)));
在stackblitz https://stackblitz.com/edit/rxjs-4uyclu?file=index.ts上查看相同的内容
发布于 2020-11-23 15:27:41
具有不同源码流的combineLatest
。
一种方法可能是使用combineLatest
,忽略不是源自源头的排放。JS对象比较的典型注意事项确实适用于此,因此您可能需要相应地调整您的自定义比较器函数。
combineLatest(
source$,
latestFrom$
).pipe(
distinctUntilChanged((prev, curr) => prev[0] === curr[0])
)
https://stackoverflow.com/questions/64959650
复制相似问题