refactor: synchronizer is now 'stateless'
This commit is contained in:
parent
dc82ce9266
commit
077b20a56c
|
@ -4,10 +4,9 @@ import immutablediff from 'immutablediff';
|
||||||
export class StateSynchronizer {
|
export class StateSynchronizer {
|
||||||
|
|
||||||
constructor(statefuls) {
|
constructor(statefuls) {
|
||||||
this._previousState = I.Map();
|
|
||||||
this._state = I.Map();
|
this._state = I.Map();
|
||||||
this._statefuls = statefuls;
|
this._statefuls = statefuls;
|
||||||
this.updateState();
|
this.tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
acceptStateChange(change) {
|
acceptStateChange(change) {
|
||||||
|
@ -20,27 +19,17 @@ export class StateSynchronizer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
diff() {
|
diff(previousState) {
|
||||||
const state = this.state();
|
const state = this._state;
|
||||||
// if (this._previousState === state) {
|
|
||||||
// return StateSynchronizer.noChange;
|
|
||||||
// }
|
|
||||||
const diff = {};
|
const diff = {};
|
||||||
let dirty = false;
|
|
||||||
for (const key in this._statefuls) {
|
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(
|
diff[key] = this.diffStateful(
|
||||||
this._previousState.get(key),
|
previousState.get(key),
|
||||||
state.get(key)
|
state.get(key)
|
||||||
);
|
);
|
||||||
dirty = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Side-effect.
|
|
||||||
this._previousState = this.state();
|
|
||||||
if (!dirty) {
|
|
||||||
return StateSynchronizer.noChange;
|
|
||||||
}
|
|
||||||
return diff;
|
return diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,12 +62,11 @@ export class StateSynchronizer {
|
||||||
return diff;
|
return diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
state() {
|
get state() {
|
||||||
this.updateState();
|
|
||||||
return this._state;
|
return this._state;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateState() {
|
tick() {
|
||||||
for (const key in this._statefuls) {
|
for (const key in this._statefuls) {
|
||||||
const stateful = this._statefuls[key];
|
const stateful = this._statefuls[key];
|
||||||
this._state = this._state.set(key, stateful.state);
|
this._state = this._state.set(key, stateful.state);
|
||||||
|
@ -86,5 +74,3 @@ export class StateSynchronizer {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StateSynchronizer.noChange = {};
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user