pad: Defer message handling until handshake completes

pull/5252/head
Richard Hansen 2021-10-29 18:54:52 -04:00
parent be0298290d
commit cd4f5ff281
1 changed files with 25 additions and 1 deletions

View File

@ -314,11 +314,33 @@ const handshake = () => {
return; return;
} else { } else {
pad.collabClient.handleMessageFromServer(obj); pad._messageQ.enqueue(obj);
} }
}); });
}; };
/** Defers message handling until setCollabClient() is called with a non-null value. */
class MessageQueue {
constructor() {
this._q = [];
this._cc = null;
}
setCollabClient(cc) {
this._cc = cc;
this.enqueue(); // Flush.
}
enqueue(...msgs) {
if (this._cc == null) {
this._q.push(...msgs);
} else {
while (this._q.length > 0) this._cc.handleMessageFromServer(this._q.shift());
for (const msg of msgs) this._cc.handleMessageFromServer(msg);
}
}
}
const pad = { const pad = {
// don't access these directly from outside this file, except // don't access these directly from outside this file, except
// for debugging // for debugging
@ -328,6 +350,7 @@ const pad = {
initTime: 0, initTime: 0,
clientTimeOffset: null, clientTimeOffset: null,
padOptions: {}, padOptions: {},
_messageQ: new MessageQueue(),
// these don't require init; clientVars should all go through here // these don't require init; clientVars should all go through here
getPadId: () => clientVars.padId, getPadId: () => clientVars.padId,
@ -423,6 +446,7 @@ const pad = {
pad.collabClient = getCollabClient( pad.collabClient = getCollabClient(
padeditor.ace, clientVars.collab_client_vars, pad.myUserInfo, padeditor.ace, clientVars.collab_client_vars, pad.myUserInfo,
{colorPalette: pad.getColorPalette()}, pad); {colorPalette: pad.getColorPalette()}, pad);
this._messageQ.setCollabClient(this.collabClient);
pad.collabClient.setOnUserJoin(pad.handleUserJoin); pad.collabClient.setOnUserJoin(pad.handleUserJoin);
pad.collabClient.setOnUpdateUserInfo(pad.handleUserUpdate); pad.collabClient.setOnUpdateUserInfo(pad.handleUserUpdate);
pad.collabClient.setOnUserLeave(pad.handleUserLeave); pad.collabClient.setOnUserLeave(pad.handleUserLeave);