我需要忽略观测值一段时间当另一个可观测提供了一个值。
I need to ignore Observable values for a period of time when another Observable provides a value.
目前,我实现使用一个变量来控制阻塞(或忽略)。
Currently, my implementation uses a variable to control blocking (or ignoring).
bool block = false;
var blocker = observable1.Do(_ => block = true )
.Throttle( _ => Observable.Timer(_timeToBlock)
.Subscribe( _ => block = false ));
var receiver = observable2.Where( i => !block && SomeCondition(i) )
.Subscribe( i=> EvenMoreStuff(i) );
有没有更多的接收方式做到这一点,把两者结合起来观测?
Is there a more Rx way to do this, by combining these two observables?
编辑:小的变化,以阻断订阅
small change to blocker subscription
第一个任务是给前preSS你的块
变量可观察到的。
The first task is to express your block
variables as an observable.
IObservable<bool> blockObservable = observable1
.Select(x => Observable.Concat(
Observable.Return(true),
Observable.Return(false).Delay(_timeToBlock)))
.Switch()
.DistinctUntilChanged();
每个时间 observable1
发出一个值,我们选择一个可观察的发射真
,等待 _timeToBlock
,然后发出假
。该开关
只是总是切换到最新的观测值。
Each time observable1
emits a value, we select an observable that emits true
, waits _timeToBlock
, then emits false
. The Switch
just always switches to the most recent of those observables.
下面是一个大理石图。让我们假设 _timeToBlock
为3个字符长。
Here's a marble diagram. Let's assume that _timeToBlock
is 3 characters long.
observable1 -----x--xx-x-----x-----
select0 T--F
select1 T--F
select2 T--F
select3 T--F
select4 T--F
switch -----T--TT-T--F--T--F--
blockObservable -----T--------F--T--F--
现在我们可以邮编
你用的 MostRecent
值值序列的 blockObservable
。
Now we can Zip
your sequence of values with the MostRecent
value of the blockObservable
.
var receiverObservable = observable2
.Zip(blockObservable.MostRecent(false), (value, block) => new { value, block })
.Where(x => !x.block)
.Select(x => x.value);