From 80e84636d7a87d2ea87100847040fbb2e6af758e Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 28 Oct 2021 17:15:42 -0400 Subject: [PATCH] pad: Promisify `handshake()` --- src/static/js/pad.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/static/js/pad.js b/src/static/js/pad.js index d91929bb6..e1c3186fb 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -214,7 +214,7 @@ const sendClientReady = (isReconnect) => { socket.json.send(msg); }; -const handshake = () => { +const handshake = async () => { let receivedClientVars = false; let padId = document.location.pathname.substring(document.location.pathname.lastIndexOf('/') + 1); // unescape neccesary due to Safari and Opera interpretation of spaces @@ -295,14 +295,8 @@ const handshake = () => { } } } else if (!receivedClientVars && obj.type === 'CLIENT_VARS') { - // if we haven't recieved the clientVars yet, then this message should it be receivedClientVars = true; - - // set some client vars window.clientVars = obj.data; - - // initialize the pad - pad._afterHandshake(); } else if (obj.disconnect) { padconnectionstatus.disconnected(obj.disconnect); socket.disconnect(); @@ -317,6 +311,15 @@ const handshake = () => { pad._messageQ.enqueue(obj); } }); + + await new Promise((resolve) => { + const h = (obj) => { + if (obj.accessStatus || obj.type !== 'CLIENT_VARS') return; + socket.off('message', h); + resolve(); + }; + socket.on('message', h); + }); }; /** Defers message handling until setCollabClient() is called with a non-null value. */ @@ -364,16 +367,20 @@ const pad = { pad.collabClient.sendClientMessage(msg); }, - init: () => { + init() { padutils.setupGlobalExceptionHandler(); - $(document).ready(() => { + // $(handler), $().ready(handler), $.wait($.ready).then(handler), etc. don't work if handler is + // an async function for some bizarre reason, so the async function is wrapped in a non-async + // function. + $(() => (async () => { if (window.customStart != null) window.customStart(); $('#colorpicker').farbtastic({callback: '#mycolorpickerpreview', width: 220}); $('#readonlyinput').on('click', () => { padeditbar.setEmbedLinks(); }); padcookie.init(); - handshake(); - }); + await handshake(); + this._afterHandshake(); + })()); }, _afterHandshake() { pad.clientTimeOffset = Date.now() - clientVars.serverTimestamp;