【发布时间】:2015-11-10 11:31:20
【问题描述】:
我想知道是否有办法获取可观察流并使用 *While 运算符,特别是 TakeWhile、SkipWhile 和 BufferWhile,以便它们的订阅者在 bool 'while' 条件被满足时不会收到 .OnComplete ?
当我开始使用 .TakeWhile / SkipWhile 和 BufferWhile 运算符时,我假设它们不会终止 / .OnComplete() 而只是(不)在满足 bool 条件时发出。
举个例子可能更有意义:
我有一个 bool 标志,用于指示实例是否忙,以及一个 Observable 数据流:
private bool IsBusy { get;set; }
private bool IgnoreChanges { get;set; }
private IObservable<int> Producer { get;set; }
private IDisposable ConsumerSubscription { get;set; }
.. 并像这样使用/设置 RX 流(简化)
private void SetupRx()
{
ConsumerSubscription = Producer
.SkipWhile(_ => IgnoreChanges == true) // Drop the producer's stream of ints whenever the IgnoreChanges flag is set to true, but forward them whenever the IgnoreChanges flag is set to false
.BufferWhile(_ => IsBusy == true) // for all streamed instances buffer them as long as we are busy handling the previous one(s)
.Subscribe(i => DoSomething(i));
}
private void DoSomething(int i)
{
try
{
IsBusy = true;
// ... do something
}
finally
{
IsBusy = false;
}
}
当 IsBusy/IgnoreChanges 标志从 true 切换到 false 并返回时,.SkipeWhile/.BufferWhile 不应该完成/OnComplete(..),但要保持流处于活动状态。
开箱即用的 RX.Net 是否可行和/或有人知道如何做到这一点?
【问题讨论】:
-
澄清一下,RX.net 中没有 BufferWhile(我的错误)
标签: .net system.reactive reactive-programming