fix: sync

This commit is contained in:
cha0s 2024-08-06 00:00:39 -05:00
parent 219ee71c2c
commit 8a3ef7842c

View File

@ -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);
};