system.reactive - CombineLatest 反应式操作符是如何工作的?
问题描述
我在 Linqpad 中运行了以下代码片段:
$"[{(DateTime.Now.ToString("HH:mm:ss.fff"))}] 0 0".Dump();
Observable
.Interval(TimeSpan.FromSeconds(3))
.CombineLatest(Observable.Interval(TimeSpan.FromSeconds(10)), (x, y) => $"[{(DateTime.Now.ToString("HH:mm:ss.fff"))}] {x} {y}")
.Do(Console.WriteLine).Wait();
这是我得到的结果:
[23:38:40.111] 0 0
[23:38:50.183] 2 0
[23:38:52.180] 3 0
[23:38:55.196] 4 0
[23:38:58.197] 5 0
[23:39:00.181] 5 1
[23:39:01.198] 6 1
[23:39:04.198] 7 1
[23:39:07.210] 8 1
[23:39:10.196] 8 2
[23:39:10.211] 9 2
[23:39:13.211] 10 2
[23:39:16.211] 11 2
[23:39:19.212] 12 2
[23:39:20.197] 12 3
[23:39:22.227] 13 3
[23:39:25.228] 14 3
[23:39:28.229] 15 3
[23:39:30.196] 15 4
[23:39:31.241] 16 4
[23:39:34.242] 17 4
我无法解释这个序列的开始:
- 为什么是第一个计算值
2 0
? - 为什么
2 0
开始后10秒输出?
解决方案
来自:http ://reactivex.io/documentation/operators/combinelatest.html
结合最新
当两个 Observable 中的任何一个发出 item 时,通过指定的函数组合每个 Observable 发出的最新 item,并根据该函数的结果发出 item
也许这个修改后的代码将帮助您了解正在发生的事情:
$"[{(DateTime.Now.ToString("HH:mm:ss.fff"))}] 0 0".Dump();
Observable.Interval(TimeSpan.FromSeconds(3))
.Do(x => $"[{(DateTime.Now.ToString("HH:mm:ss.fff"))}] {x} _".Dump())
.CombineLatest(
Observable.Interval(TimeSpan.FromSeconds(10))
.Do(y => $"[{(DateTime.Now.ToString("HH:mm:ss.fff"))}] _ {y}".Dump()),
(x, y) => $"[{(DateTime.Now.ToString("HH:mm:ss.fff"))}] {x} {y}")
.Do(s => s.Dump())
.Wait();
推荐阅读
- sql - 如何跨多个表/列查找不同的日期
- python - MongoDB 存储查询和返回值的最佳方式
- php - 提交按钮不起作用。我有一个 mysql 数据库,我正在尝试制作一个 php 表单
- cordova - 如何在 VS2017 Cordova (Windows 10) 中安装模拟器
- javascript - 谷歌地图 API - ReferenceError:谷歌未定义
- python - 如何在 IntelliJ 中打开 .ipynb 文件?
- ajax - 您如何处理 Razor Pages PageRemote 对“加载”数据的验证(例如编辑 ViewModel 页面)?
- php - 如何使用 PHP 下载生成的 pdf 文件?
- python - 如何将 pandas groupby 函数中的数据分配给变量?
- php - 如何将具有相同格式的多个数组与具有相同格式的 1 个数组进行比较?