From cd4f5ff281539f339fc31e1373d44696567a8bb5 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 29 Oct 2021 18:54:52 -0400 Subject: [PATCH] pad: Defer message handling until handshake completes --- src/static/js/pad.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/static/js/pad.js b/src/static/js/pad.js index 0dc4a4a54..d91929bb6 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -314,11 +314,33 @@ const handshake = () => { return; } 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 = { // don't access these directly from outside this file, except // for debugging @@ -328,6 +350,7 @@ const pad = { initTime: 0, clientTimeOffset: null, padOptions: {}, + _messageQ: new MessageQueue(), // these don't require init; clientVars should all go through here getPadId: () => clientVars.padId, @@ -423,6 +446,7 @@ const pad = { pad.collabClient = getCollabClient( padeditor.ace, clientVars.collab_client_vars, pad.myUserInfo, {colorPalette: pad.getColorPalette()}, pad); + this._messageQ.setCollabClient(this.collabClient); pad.collabClient.setOnUserJoin(pad.handleUserJoin); pad.collabClient.setOnUpdateUserInfo(pad.handleUserUpdate); pad.collabClient.setOnUserLeave(pad.handleUserLeave);