diff --git a/package.json b/package.json index e83b8ef..b72932e 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/traits/informed.js b/traits/informed.js index 43a96d3..78b1e53 100644 --- a/traits/informed.js +++ b/traits/informed.js @@ -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) { diff --git a/yarn.lock b/yarn.lock index 0fecbae..ae1eeed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: