计算 Observable 完成之前发出的值的数量?
Counting the number of values emitted before the Observable completes?
正在尝试验证可观察对象在完成之前是否发出了一定数量的事件。这是伪代码:
o.pipe(count).subscribe(count=>
expect(count).toEqual(4));
想法?
count
运算符的工作方式如下:
Counts the number of emissions on the source and emits that number when the source completes (source)
所以你可以这样使用它:
obs.pipe(count()).subscribe(totalEmissions => expect(totalEmissions).toEqual(4))
请注意,您无法真正衡量在 原始可观察对象完成之前发生了多少事件,因为如果它没有完成,那么您就没有完成计数!
但是,您可以使用 tap
:
记下每次发射的 "index"
let count = 0
obs.pipe(tap(() => console.log("emitted! Index: " + count++))).subscribe(obsValue => {/*...*/})
我不确定哪个是你的用例,但你可以这样做。
正在尝试验证可观察对象在完成之前是否发出了一定数量的事件。这是伪代码:
o.pipe(count).subscribe(count=>
expect(count).toEqual(4));
想法?
count
运算符的工作方式如下:
Counts the number of emissions on the source and emits that number when the source completes (source)
所以你可以这样使用它:
obs.pipe(count()).subscribe(totalEmissions => expect(totalEmissions).toEqual(4))
请注意,您无法真正衡量在 原始可观察对象完成之前发生了多少事件,因为如果它没有完成,那么您就没有完成计数!
但是,您可以使用 tap
:
let count = 0
obs.pipe(tap(() => console.log("emitted! Index: " + count++))).subscribe(obsValue => {/*...*/})
我不确定哪个是你的用例,但你可以这样做。