基于另一个Observable的过去值过滤Observable



所以我有两个可观察器click$focus$。只有在过去500毫秒内没有任何焦点事件的情况下,我才希望点击进入

我试过滑雪。但我在想办法把什么放进去时迷路了。

click$.skipWhile(/* No focus$ events in the past 500ms*/).subscribe()

请给我指正确的方向。

您应该能够通过编写基于焦点事件的可观察对象来完成您想要的操作。像这样:

const focusedClick$ = focus$
// Switch to the click observable, but only after the specified
// duration has elapsed:
.switchMap(() => Observable.timer(500).ignoreElements().concat(click$));

如果有可能在没有先前焦点事件的情况下发生点击事件,则可以使用startWith运算符来解决该问题

const focusedClick$ = focus$
// Map the focus event to a duration:
.mapTo(500)
// Start the observable chain with a duration of zero, so click
// events don't have to be preceded by focus events:
.startWith(0)
// Switch to the click observable, but only after the specified
// duration has elapsed:
.switchMap(duration => Observable.timer(duration).ignoreElements().concat(click$));

最新更新