collab_client: Move task queue class definition to top of file

rhansen-collab_client
Richard Hansen 2021-04-01 02:25:08 -04:00
parent 0dc66e629e
commit 5f80c3f3c9
1 changed files with 16 additions and 14 deletions

View File

@ -38,6 +38,21 @@ class Gate extends Promise {
}
}
class TaskQueue {
constructor() {
this._promiseChain = Promise.resolve();
}
async enqueue(fn) {
const taskPromise = this._promiseChain.then(fn);
// Use .catch() to prevent rejections from halting the queue.
this._promiseChain = taskPromise.catch(() => {});
// Do NOT do `return await this._promiseChain;` because the caller would not see an error if
// fn() throws/rejects (due to the .catch() added above).
return await taskPromise;
}
}
/** Call this when the document is ready, and a new Ace2Editor() has been created and inited.
ACE's ready callback does not need to have fired yet.
"serverVars" are from calling doc.getCollabClientVars() on the server. */
@ -171,20 +186,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, pad) => {
});
};
const serverMessageTaskQueue = new class {
constructor() {
this._promiseChain = Promise.resolve();
}
async enqueue(fn) {
const taskPromise = this._promiseChain.then(fn);
// Use .catch() to prevent rejections from halting the queue.
this._promiseChain = taskPromise.catch(() => {});
// Do NOT do `return await this._promiseChain;` because the caller would not see an error if
// fn() throws/rejects (due to the .catch() added above).
return await taskPromise;
}
}();
const serverMessageTaskQueue = new TaskQueue();
const handleMessageFromServer = (evt) => {
if (!pad.socket) return;