Reactive Extensions 应该评估其各种运算符多少次?
我有以下测试代码:
var seconds = Observable
.Interval(TimeSpan.FromSeconds(5))
.Do(_ => Console.WriteLine("{0} Generated Data", DateTime.Now.ToLongTimeString()));
var split = seconds
.Do(_ => Console.WriteLine("{0} Split/Branch received Data", DateTime.Now.ToLongTimeString()));
var merged = seconds
.Merge(split)
.Do(_ => Console.WriteLine("{0} Received Merged data", DateTime.Now.ToLongTimeString()));
var pipeline = merged.Subscribe();
我希望这将每五秒写入一次“生成的数据”。然后,它将数据传递给写入“Split/Branch received Data”的“split”流和写入“Received Merged data”的“merged”流。最后,因为“合并”流也从“拆分”流接收,所以它第二次接收数据并第二次写入“Received Merged data”。(它写其中一些的顺序并不是特别相关)
但我得到的输出是这样的:
8:29:56 AM Generated Data
8:29:56 AM Generated Data
8:29:56 AM Split/Branch received Data
8:29:56 AM Received Merged data
8:29:56 AM Received Merged data
8:30:01 AM Generated Data
8:30:01 AM Generated Data
8:30:01 AM Split/Branch received Data
8:30:01 AM Received Merged data
8:30:01 AM Received Merged data
它正在写入两次“生成的数据”。据我了解,订阅“秒”IObservable 的下游观察者的数量不应影响“生成数据”写入的次数(应该是 ONCE),但确实如此。为什么?
注意我在 .Net Framework 3.5 环境中使用反应式扩展的稳定版本 v1.0 SP1。