refactor: collapse state diffs
This commit is contained in:
parent
d3326350e6
commit
7a7bd84f4b
|
@ -37,6 +37,7 @@
|
|||
"@avocado/topdown": "^1.0.0",
|
||||
"glob": "^7.1.3",
|
||||
"immutablediff": "^0.4.4",
|
||||
"is-plain-object": "2.0.4",
|
||||
"source-map-support": "^0.5.11"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import * as I from 'immutable';
|
||||
import isPlainObject from 'is-plain-object';
|
||||
|
||||
import {compose} from '@avocado/core';
|
||||
import {StateProperty, Trait} from '@avocado/entity';
|
||||
|
@ -30,6 +31,21 @@ class InformedBase extends Trait {
|
|||
}
|
||||
}
|
||||
|
||||
collapseStateDiff(diff) {
|
||||
for (const index in diff) {
|
||||
const value = diff[index];
|
||||
if (isPlainObject(value)) {
|
||||
if (!this.collapseStateDiff(value)) {
|
||||
delete diff[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (0 === Object.keys(diff).length) {
|
||||
return null;
|
||||
}
|
||||
return diff;
|
||||
}
|
||||
|
||||
createIndexedReducer() {
|
||||
let previous = {};
|
||||
return (diff, indexed) => {
|
||||
|
@ -81,7 +97,7 @@ class InformedBase extends Trait {
|
|||
indexedEntities,
|
||||
);
|
||||
}
|
||||
return diff;
|
||||
return this.collapseStateDiff(diff);
|
||||
}
|
||||
|
||||
get socket() {
|
||||
|
@ -100,6 +116,7 @@ class InformedBase extends Trait {
|
|||
// Take diff.
|
||||
let diff = stateSynchronizer.diff(this._informState);
|
||||
diff = this.reduceStateDiff(diff);
|
||||
diff = diff ? diff : {};
|
||||
this._informState = stateSynchronizer.state;
|
||||
// Let the client know who they are.
|
||||
if (!this._sentSelf) {
|
||||
|
|
|
@ -2609,7 +2609,7 @@ is-path-inside@^1.0.0:
|
|||
dependencies:
|
||||
path-is-inside "^1.0.1"
|
||||
|
||||
is-plain-object@^2.0.1, is-plain-object@^2.0.3, is-plain-object@^2.0.4:
|
||||
is-plain-object@2.0.4, is-plain-object@^2.0.1, is-plain-object@^2.0.3, is-plain-object@^2.0.4:
|
||||
version "2.0.4"
|
||||
resolved "https://npm.i12e.cha0s.io/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"
|
||||
dependencies:
|
||||
|
|
Loading…
Reference in New Issue
Block a user