refactor: synchronizer is now 'stateless'

This commit is contained in:
cha0s 2019-03-28 02:09:46 -05:00
parent dc82ce9266
commit 077b20a56c

View File

@ -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 = {};