diff --git a/packages/state/synchronizer.js b/packages/state/synchronizer.js index 2224637..a154df6 100644 --- a/packages/state/synchronizer.js +++ b/packages/state/synchronizer.js @@ -4,10 +4,9 @@ import immutablediff from 'immutablediff'; export class StateSynchronizer { constructor(statefuls) { - this._previousState = I.Map(); this._state = I.Map(); this._statefuls = statefuls; - this.updateState(); + this.tick(); } acceptStateChange(change) { @@ -20,27 +19,17 @@ export class StateSynchronizer { } } - diff() { - const state = this.state(); - // if (this._previousState === state) { - // return StateSynchronizer.noChange; - // } + diff(previousState) { + const state = this._state; const diff = {}; - let dirty = false; for (const key in this._statefuls) { - if (this._previousState.get(key) !== state.get(key)) { + if (previousState.get(key) !== state.get(key)) { diff[key] = this.diffStateful( - this._previousState.get(key), + previousState.get(key), state.get(key) ); - dirty = true; } } - // Side-effect. - this._previousState = this.state(); - if (!dirty) { - return StateSynchronizer.noChange; - } return diff; } @@ -73,12 +62,11 @@ export class StateSynchronizer { return diff; } - state() { - this.updateState(); + get state() { return this._state; } - updateState() { + tick() { for (const key in this._statefuls) { const stateful = this._statefuls[key]; this._state = this._state.set(key, stateful.state); @@ -86,5 +74,3 @@ export class StateSynchronizer { } } - -StateSynchronizer.noChange = {};