From 8a3ef7842c5d3eb10309026ea97f32a31ea42a47 Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 6 Aug 2024 00:00:39 -0500 Subject: [PATCH] fix: sync --- app/client/prediction.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/client/prediction.js b/app/client/prediction.js index 6660074..4fd3dc8 100644 --- a/app/client/prediction.js +++ b/app/client/prediction.js @@ -4,16 +4,13 @@ import {withResolvers} from '@/util/promise.js'; let connected = false; let socket; -function onMessage(event) { - postMessage(event.data); -} +const {promise, resolve} = withResolvers(); onmessage = async (event) => { if (!connected) { const url = new URL(`wss://${event.data.host}/ws`) socket = new WebSocket(url.href); socket.binaryType = 'arraybuffer'; - const {promise, resolve} = withResolvers(); socket.addEventListener('open', resolve); socket.addEventListener('error', () => { postMessage(encode({type: 'ConnectionStatus', payload: 'aborted'})); @@ -21,7 +18,9 @@ onmessage = async (event) => { }); await promise; socket.removeEventListener('open', resolve); - socket.addEventListener('message', onMessage); + socket.addEventListener('message', (event) => { + postMessage(event.data); + }); socket.addEventListener('close', () => { postMessage(encode({type: 'ConnectionStatus', payload: 'aborted'})); close(); @@ -30,5 +29,6 @@ onmessage = async (event) => { connected = true; return; } + await promise; socket.send(event.data); };