javascript - RxJS。组合发射时间小于 1 秒的可观察对象
问题描述
有两个 observables 可以一起或单独发射:stream1
和stream2
. stream2
仅当触发时间少于 1 秒时,我才需要触发订阅stream1
。
有什么方法可以用 RxJS 实现吗?
解决方案
您可以使用时间戳并withLatestFrom
决定要发出哪些值在这里我只是过滤,以便只有满足您条件的值才能通过。
stream2.pipe(
timestamp(),
withLatestFrom(stream1.pipe(
timestamp(),
startWith({timestamp: 0, value: null})
)),
filter(([s2, s1]) => s2.timestamp - s1.timestamp < 1000),
map(([s2, s1]) => ({
stream1: s1.value,
stream2: s2.value
}))
);
推荐阅读
- tensorflow - ('输入具有未定义的等级:',TensorShape(None)) <- 构建 ResNet 时出错
- c++ - 从父目录导入 proto 不起作用
- r - R中的时间序列模型样本ACF
- c - 我如何使用 GTK 3 中的模型过滤器和 C 中的 GTK_SELECTION_MULTIPLE 在树视图中进行 DND?
- reactjs - 我可以禁用底部标签导航器中的选项卡之一吗?反应原生
- reactjs - 在 React JS 中尝试上下文 API 时出错。我在 ReactJS 中使用上下文 API 和类
- java - 单击 ::before 的 psedu 元素并在其中发送密钥
- .net - dotnet 格式验证抛出 dll not found
- regex - InputFormatter 应该允许负数和十进制数
- php - 仅打印 foreach 中每个重复项的最后一项