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

时间:2016-01-20 01:34:27

标签: javascript system.reactive reactive-programming rxjs frp

我是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())

1 个答案:

答案 0 :(得分:1)

欢迎来到FRP的喜悦。事实上,很难理解你的问题。 text1 = rootView.findViewById(R.id.text1); text2 = rootView.findViewById(R.id.text2); 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运营商,建议您查看以下资源:The introduction to reactive programming you've been missingillustrated marblesofficial documentation

PS:我发现你似乎遵循了MODEL-VIEW-INTENT架构。出于好奇,你在试验flatMap吗?