【发布时间】:2014-10-24 19:43:01
【问题描述】:
我阅读了以下从 Ollie Riches 的 博客帖子 Trying to be more functional with Rx 中截取的内容,并开始和作者一样想知道:为什么 OnCompleted 没有通过?有人可以告诉这里发生了什么吗?也许是简单得令人尴尬的事情?
为了方便起见,这里对代码进行了一些修改和复制(如果不能在这里撕掉他的代码,我向 Ollie 道歉):
public static class RxExtensions
{
public static IObservable<T> Suspendable<T>(this IObservable<T> stream, IObservable<bool> suspend, bool initialState = false)
{
return Observable.Create<T>(o =>
{
var disposable = suspend.StartWith(initialState)
.DistinctUntilChanged()
.Select(s => s ? Observable.Empty<T>() : stream)
.Switch()
.Subscribe(o);
return disposable;
});
}
}
var testScheduler = new TestScheduler();
var generatorCount = 10;
//If the limit will be hardcoded to something less than generatorCount, an exception will be
//thrown and the exception object will be set. Why it doesn't happen to completed in the following?
var generator = Observable.Generate(1,
x => x <= generatorCount,
x => x + 1,
x => { if(x != 11) { Console.WriteLine(x); return x; } else { throw new ArgumentException(); } },
x => TimeSpan.FromSeconds(1),
testScheduler);
Exception exception = null;
var completed = false;
generator.Suspendable(new Subject<bool>()).Subscribe(_ => { }, e => exception = e, () => completed = true);
testScheduler.AdvanceBy(TimeSpan.FromMilliseconds(1001000).Ticks);
Console.WriteLine(exception);
Console.WriteLine(completed);
为了记录,我正在考虑尝试生成一个可以暂停和停止的流,区别在于暂停流累积事件,暂停只是跳过它们。它开始看起来比我预期的要复杂一些,特别是如果有人想对暂停的位设置限制或“保存策略”。哦,好吧……
【问题讨论】:
标签: c# system.reactive rxjs