From 3de86c3b29aca1b9a2db33a37a086b17b95b51c3 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 7 Aug 2016 22:50:12 -0400 Subject: [PATCH] Better pendingPatch logic --- js/src/dataspace.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/js/src/dataspace.js b/js/src/dataspace.js index dc3cc83..92ce6b9 100644 --- a/js/src/dataspace.js +++ b/js/src/dataspace.js @@ -219,19 +219,21 @@ Dataspace.prototype.flushPendingPatch = function () { }; Dataspace.prototype.enqueueAction = function (pid, action) { - if (action.type === 'stateChange') { - if (!this.pendingPatch) { - this.pendingPatch = [pid, action.patch]; - return; - } - if (this.pendingPatch[0] === pid) { - this.pendingPatch[1] = this.pendingPatch[1].andThen(action.patch); - return; - } - /* fall through */ + if (action.type === 'stateChange' && this.pendingPatch && this.pendingPatch[0] === pid) { + this.pendingPatch[1] = this.pendingPatch[1].andThen(action.patch); + return; } + + // If we get here, any pendingPatch that might exist is definitely + // not something we can extend, but it has to happen before action, + // so flush it now. this.flushPendingPatch(); - this.pendingActions = this.pendingActions.push([pid, action]); + + if (action.type === 'stateChange') { + this.pendingPatch = [pid, action.patch]; + } else { + this.pendingActions = this.pendingActions.push([pid, action]); + } }; Dataspace.prototype.dispatchActions = function () {