RxJS 的循环依赖.造型孢子 [英] Circular Dependencies with RxJS. Modeling spores

查看:52
本文介绍了RxJS 的循环依赖.造型孢子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 RxJS 为一些游戏建模.但我发现循环依赖有一些问题.所以,我将我的游戏简化为一个简单的模拟(我只留下了移动"动作).你可以在下面找到代码(我省略了一些部分,你可以找到这里的repo)

I try to model some game with RxJS. but I found some troubles with circular dependencies. So, I simplified my game to a simple simulation(I left only 'move' action). You can find code below(I omitted some parts, you can find the repo here)

const rx = require('rx')
const math = require('mathjs')
const _ = require('underscore')

const FIELD_SIZE = 10

const ctx = require('axel');

let getInitialState = () => {
  return {
    size: FIELD_SIZE,
    people: [
      { x: 0, y: 0 },
      { x: 9, y: 9 },
      { x: 5, y: 5 }
    ]
  }
}

var drawWorld = ({size, people}) => {
  // draw world logic
}


let getMove = (index)=> {
  let [xOffset, yOffset] = [[0,1], [1,0]][math.pickRandom([0, 1])]
  let direction = math.pickRandom([-1, 1])
  return (state) => {
    let {people} = state
    let p = people[index]
    people[index] = {
      x: math.max(
          0,
          math.min(p.x + xOffset * direction, FIELD_SIZE-1)),
      y: math.max(
          0,
          math.min(p.y + yOffset * direction, FIELD_SIZE-1))
    }
    return _.extend({}, state, {people})
  }
}

var POOL = []

var produceActions = (state) => {
  _.range(state.people.length).forEach((i) => {
    POOL.push(getMove(i))
  })
}

var stateObservable = rx.Observable
  .interval(100)
  .timeInterval()
  .map(()=> {
    var x = POOL.slice()
    POOL.splice(0, POOL.length)
    return x
  })
  .scan(
      (state, ops) => ops.reduce(
        (st, o) => o(st),
        state
      ),
      getInitialState()
  )

stateObservable.subscribe(drawWorld)
stateObservable.tap(produceActions).subscribe()

有没有办法重写这个依赖全局变量的丑陋的produceActions"部分?就像@user3743222 回答中提出的

Is there a way to rewrite this ugly `produceActions' part which relies on global variable? Like proposed in the @user3743222 answer

推荐答案

按照您更新的问题和示例请求,这是未经测试的,但这是我的尝试,基于提供的主题技术链接.检查它是否有效,如果有效,我会在此之后发布一些解释.另外,请检查语法,我对ES6不太了解.

Following your updated question and request for an example, this is untested but here is my try, based on the provided link on the subject technique. Check if it works, I'll post some explanations after that if it does. Also, please check the syntax, I don't know so much about ES6.

使用 POOL 的代理主体:

var POOL_proxyS = new Rx.BehaviorSubject([]); // instead of var POOL = []

var stateObservable = rx.Observable
  .interval(100)
  .timeInterval()
  .combineLatest(POOL_proxyS, (_, POOL) => POOL.slice() )
  .scan(
      (state, ops) => ops.reduce(
        (st, o) => o(st),
        state
      ),
      getInitialState()
  );

var produceActions = (state) => _.range(state.people.length).map((i) => getMove(i));

stateObservable
  .do(drawWorld)
  .map(produceActions)
  .subscribe(POOL_proxyS);

这篇关于RxJS 的循环依赖.造型孢子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆