title: Circular Dependencies with RxJS. Modeling spores
body: <p>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 <a href="https://github.com/kharandziuk/spores" rel="nofollow">the repo here</a>)</p>
  
  <pre><code>const rx = require('rx')
  const math = require('mathjs')
  const _ = require('underscore')
  
  const FIELD_SIZE = 10
  
  const ctx = require('axel');
  
  let getInitialState = () =&gt; {
    return {
      size: FIELD_SIZE,
      people: [
        { x: 0, y: 0 },
        { x: 9, y: 9 },
        { x: 5, y: 5 }
      ]
    }
  }
  
  var drawWorld = ({size, people}) =&gt; {
    // draw world logic
  }
  
  
  let getMove = (index)=&gt; {
    let [xOffset, yOffset] = [[0,1], [1,0]][math.pickRandom([0, 1])]
    let direction = math.pickRandom([-1, 1])
    return (state) =&gt; {
      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) =&gt; {
    _.range(state.people.length).forEach((i) =&gt; {
      POOL.push(getMove(i))
    })
  }
  
  var stateObservable = rx.Observable
    .interval(100)
    .timeInterval()
    .map(()=&gt; {
      var x = POOL.slice()
      POOL.splice(0, POOL.length)
      return x
    })
    .scan(
        (state, ops) =&gt; ops.reduce(
          (st, o) =&gt; o(st),
          state
        ),
        getInitialState()
    )
  
  stateObservable.subscribe(drawWorld)
  stateObservable.tap(produceActions).subscribe()
  </code></pre>
  
  <p>Is there a way to rewrite this ugly `produceActions' part which relies on global variable?
  Like proposed in the @user3743222 answer</p>