当一个流依赖于另一个流的值时该怎么办?

What to do when one stream depends on the value from another?

我是 Rx.js 的新手,正在寻找 FRP 概念方面的一些基本帮助。我有一个流使用 generateWithRelativeTime 保持游戏循环 运行,还有一个流跟踪屏幕分辨率。我当前代码的问题是我的游戏循环仅在屏幕分辨率更改时运行。

我的模型函数生成的流需要来自 res$ 的最新值。

function model(res$) {
  return res$.map(res => {
    const rows = ceil(res[1] / CELL.HEIGHT)+1
    const cols = ceil(res[0] / CELL.WIDTH)+1

    return Observable.generateWithRelativeTime(
      initWorld(rows, cols, 1.618),
      noOp,
      world => step(world),
      noOp,
      _ => 100,
      Rx.Scheduler.requestAnimationFrame
    ).timeInterval()
  });
}

function intent() {
  return Observable.fromEvent(window, 'resize')
                   .map(e => [e.currentTarget.innerWidth, e.currentTarget.innerHeight])
                   .debounce(100, Rx.Scheduler.requestAnimationFrame)
                   .startWith([window.innerWidth, window.innerHeight])
}

model(intent())

欢迎来到 FRP 的欢乐世界。事实上,很难理解你的问题。 What to do when one stream depends on the value from another? 如何连接到 My problem with my current code is that my game loop only runs when my screen resolution changes.?你给出实际行为,你期望的行为是什么?

由于我对这个问题的理解有限,我认为您需要将这一行 return res$.map(res => { 替换为 return res$.flatMapLatest(res => {,即使用运算符 flatMapLatest 而不是 map 运算符。

要了解 flatMap 运算符,我建议您查看以下资源:The introduction to reactive programming you've been missing, the illustrated marbles and the official documentation.

PS : 我看到您似乎遵循 MODEL-VIEW-INTENT 体系结构。出于好奇,您是否正在试验 cyclejs