From 1adf24db79300f7220bb672215b47946b006f4a9 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 1 Apr 2021 02:52:33 -0400 Subject: [PATCH] collab_client: Implement `callWhenNotCommitting()` with a `Gate` --- src/static/js/collab_client.js | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/static/js/collab_client.js b/src/static/js/collab_client.js index c630e2f65..a6f77ba3e 100644 --- a/src/static/js/collab_client.js +++ b/src/static/js/collab_client.js @@ -102,7 +102,8 @@ class CollabClient { this._serverMessageTaskQueue = new TaskQueue(); - this._idleFuncs = []; + this._idleGate = new Gate(); + this._idleGate.open(); this.addHistoricalAuthors(serverVars.historicalAuthorData); this._tellAceActiveAuthorInfo(this._initialUserInfo); @@ -157,6 +158,7 @@ class CollabClient { if (userChangesData.changeset) { this._lastCommitTime = now; this._committing = true; + this._idleGate = new Gate(); this._stateMessage = { type: 'USER_CHANGES', baseRev: this._rev, @@ -434,28 +436,17 @@ class CollabClient { setStateIdle() { this._committing = false; + this._idleGate.open(); this._callbacks.onInternalAction('newlyIdle'); - this._schedulePerhapsCallIdleFuncs(); } setIsPendingRevision(value) { this._isPendingRevision = value; } - callWhenNotCommitting(func) { - this._idleFuncs.push(func); - this._schedulePerhapsCallIdleFuncs(); - } - - _schedulePerhapsCallIdleFuncs() { - setTimeout(() => { - if (!this._committing) { - while (this._idleFuncs.length > 0) { - const f = this._idleFuncs.shift(); - f(); - } - } - }, 0); + async callWhenNotCommitting(func) { + await this._idleGate; + return await func(); } setOnUserJoin(cb) {