javascript - Tracker.autorun() not running on every update of a ReactiveVar -
i have meteor reactivevar
, used update trigger in data store. tracker not run every time reactive var set.
it seems when state set in quick succession, tracker doesn't run.
this code store:
const mystore = { states = { ... }, updatetrigger = new reactivevar({ name: null, timestamp: null }), setstate({ name, value }) { this.states[name] = value; console.log('set state', name); this.updatetrigger.set({ name, timestamp: new date().gettime() }); }, };
and tracker:
tracker.autorun(() => { const updatetrigger = mystore.updatetrigger.get(); console.log('tracker', updatetrigger.name); if (updatetrigger.name === state-two) myfunction(); });
the console logs this:
'set state state-one' // update state-one 'tracker state-one' 'set state state-two' // update state-two 'set state state-one' // update state-one 'tracker state-one' 'set state state-three' // update state-three 'tracker state-three' 'set state state-one' // update state-one 'set state state-two' // update state-two 'set state state-three' // update state-three 'tracker state-three'
i can't see why happening.
it seems race condition it's indiscriminate updates logs , ones doesn't.
the states updated quite (state 1 once every 1.5s, , others every second or so).
any suggestions going wrong, or other approaches welcome.
i use pubsub package. i'm not big fan of tracker
, reactivevar
, i'm not sure what's best practice here, , don't want use tracker
+reactivevar
in places, , pubsub in others.
having each individual state reactivevar
not option, need persist state database on update.
this due tracker's flush cycle.
from tracker manual:
... default, all changes reactive values batched , put effect @ once, when javascript code has finished executing , system idle. ... process ... called flushing, , it's possible manually trigger it calling
tracker.flush()
.
therefore, adding explicit flush in code yield desired effect:
const mystore = { states = { ... }, updatetrigger = new reactivevar({ name: null, timestamp: null }), setstate({ name, value }) { this.states[name] = value; console.log('set state', name); this.updatetrigger.set({ name, timestamp: new date().gettime() }); tracker.flush(); // added }, };
Comments
Post a Comment