javascript - Circular Dependencies with RxJS. Modeling spores -


i try model game rxjs. found troubles circular dependencies. so, simplified game simple simulation(i left 'move' action). can find code below(i omitted parts, 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() 

is there way rewrite ugly `produceactions' part relies on global variable? proposed in @user3743222 answer

following updated question , request example, untested here try, based on provided link on subject technique. check if works, i'll post explanations after if does. also, please check syntax, don't know es6.

using proxy subject 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); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -