collab_client: Implement callWhenNotCommitting() with a Gate

This commit is contained in:
Richard Hansen 2021-04-01 02:52:33 -04:00
parent e5375fe425
commit 1adf24db79

View file

@ -102,7 +102,8 @@ class CollabClient {
this._serverMessageTaskQueue = new TaskQueue(); this._serverMessageTaskQueue = new TaskQueue();
this._idleFuncs = []; this._idleGate = new Gate();
this._idleGate.open();
this.addHistoricalAuthors(serverVars.historicalAuthorData); this.addHistoricalAuthors(serverVars.historicalAuthorData);
this._tellAceActiveAuthorInfo(this._initialUserInfo); this._tellAceActiveAuthorInfo(this._initialUserInfo);
@ -157,6 +158,7 @@ class CollabClient {
if (userChangesData.changeset) { if (userChangesData.changeset) {
this._lastCommitTime = now; this._lastCommitTime = now;
this._committing = true; this._committing = true;
this._idleGate = new Gate();
this._stateMessage = { this._stateMessage = {
type: 'USER_CHANGES', type: 'USER_CHANGES',
baseRev: this._rev, baseRev: this._rev,
@ -434,28 +436,17 @@ class CollabClient {
setStateIdle() { setStateIdle() {
this._committing = false; this._committing = false;
this._idleGate.open();
this._callbacks.onInternalAction('newlyIdle'); this._callbacks.onInternalAction('newlyIdle');
this._schedulePerhapsCallIdleFuncs();
} }
setIsPendingRevision(value) { setIsPendingRevision(value) {
this._isPendingRevision = value; this._isPendingRevision = value;
} }
callWhenNotCommitting(func) { async callWhenNotCommitting(func) {
this._idleFuncs.push(func); await this._idleGate;
this._schedulePerhapsCallIdleFuncs(); return await func();
}
_schedulePerhapsCallIdleFuncs() {
setTimeout(() => {
if (!this._committing) {
while (this._idleFuncs.length > 0) {
const f = this._idleFuncs.shift();
f();
}
}
}, 0);
} }
setOnUserJoin(cb) { setOnUserJoin(cb) {