From b37352534b93b41386cb4eb194644b2593a4d4af Mon Sep 17 00:00:00 2001 From: flashwave Date: Fri, 1 Mar 2024 20:35:44 +0000 Subject: [PATCH] Made UmiWebSocket use EventTarget instead of taking a massive callback. --- src/mami.js/main.js | 3 -- src/mami.js/sockchat_old.js | 53 +++++++---------------- src/mami.js/websock.js | 83 ++++++++++++++----------------------- src/websock.js/main.js | 51 +++++++---------------- 4 files changed, 64 insertions(+), 126 deletions(-) diff --git a/src/mami.js/main.js b/src/mami.js/main.js index d383510..bb4499b 100644 --- a/src/mami.js/main.js +++ b/src/mami.js/main.js @@ -622,9 +622,6 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } }; wsCloseReasons[`_${ev.detail.code}`] ?? `Something caused an unexpected connection loss. (${ev.detail.code})` ); }); - sockChat.watch('conn:error', ev => { - console.error('conn:error', ev); - }); sockChat.watch('ping:send', ev => { if(dumpEvents) console.log('ping:send', ev); diff --git a/src/mami.js/sockchat_old.js b/src/mami.js/sockchat_old.js index 15982cb..509cb1d 100644 --- a/src/mami.js/sockchat_old.js +++ b/src/mami.js/sockchat_old.js @@ -120,7 +120,7 @@ Umi.Protocol.SockChat.Protocol = function(pingDuration) { if(wasKicked) return; - let code = ev.code; + let code = ev.detail.code; if(isRestarting && code === 1006) { code = 1012; } else if(code === 1012) @@ -137,19 +137,10 @@ Umi.Protocol.SockChat.Protocol = function(pingDuration) { setTimeout(() => beginConnecting(), 5000); }; - const onError = ex => { - eventTarget.dispatch('conn:error', ex); - }; - - const unfuckText = text => { - const elem = document.createElement('div'); - elem.innerHTML = text.replace(/ /g, "\n"); - text = elem.innerText; - return text; - }; + const unfuckText = text => text.replace(/ /g, "\n"); const onMessage = ev => { - const args = ev.data.split("\t"); + const args = ev.detail.data.split("\t"); let handler = handlers; if(dumpPackets) @@ -556,31 +547,19 @@ Umi.Protocol.SockChat.Protocol = function(pingDuration) { attempt: ++connectAttempts, }); - sock = new UmiWebSocket(server, ev => { - switch(ev.act) { - case 'ws:open': - onOpen(ev); - break; - case 'ws:close': - onClose(ev); - break; - case 'ws:message': - onMessage(ev); - break; - case 'ws:create_interval': - pingTimer = ev.id; - break; - case 'ws:call_interval': - if(ev.id === pingTimer) - onSendPing(); - break; - case 'ws:clear_intervals': - pingTimer = undefined; - break; - default: - console.log(ev.data); - break; - } + sock = new UmiWebSocket(server); + sock.watch('open', onOpen); + sock.watch('close', onClose); + sock.watch('message', onMessage); + sock.watch('create_interval', ev => { + pingTimer = ev.detail.id; + }); + sock.watch('call_interval', ev => { + if(ev.detail.id === pingTimer) + onSendPing(); + }); + sock.watch('create_intervals', ev => { + pingTimer = undefined; }); }); }; diff --git a/src/mami.js/websock.js b/src/mami.js/websock.js index e13424b..b219685 100644 --- a/src/mami.js/websock.js +++ b/src/mami.js/websock.js @@ -1,6 +1,8 @@ -const UmiWebSocket = function(server, message, useWorker) { - if(typeof useWorker === 'undefined') - useWorker = (function() { +#include eventtarget.js + +const UmiWebSocket = function(url, useWorker) { + if(typeof useWorker !== 'boolean') + useWorker = (() => { // Overrides if(mami.settings.get('neverUseWorker')) return false; @@ -18,90 +20,68 @@ const UmiWebSocket = function(server, message, useWorker) { return false; })(); + const eventTarget = new MamiEventTarget('ws'); let send, close, sendInterval, clearIntervals; if(useWorker) { const worker = new Worker(MAMI_WS); - worker.addEventListener('message', function(ev) { - message(ev.data); + worker.addEventListener('message', ev => { + if(ev.data.act.startsWith('ws:')) + eventTarget.dispatch(ev.data.act.substring(3), ev.data.detail); }); - worker.postMessage({act: 'ws:open', server: server}); - send = function(text) { + worker.postMessage({act: 'ws:open', url: url}); + send = text => { worker.postMessage({act: 'ws:send', text: text}); }; - close = function() { + close = () => { worker.postMessage({act: 'ws:close'}); }; - getIntervals = function() { - worker.postMessage({act: 'ws:intervals'}); - }; - sendInterval = function(text, interval) { + sendInterval = (text, interval) => { worker.postMessage({act: 'ws:send_interval', text: text, interval: interval}); }; - clearIntervals = function() { + clearIntervals = () => { worker.postMessage({act: 'ws:clear_intervals'}); }; } else { - const websocket = new WebSocket(server), intervals = []; - websocket.addEventListener('open', function(ev) { - message({ - act: 'ws:open', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, - }); + const websocket = new WebSocket(url), intervals = []; + websocket.addEventListener('open', ev => { + eventTarget.dispatch('open'); }); - websocket.addEventListener('close', function(ev) { - message({ - act: 'ws:close', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, + websocket.addEventListener('close', ev => { + eventTarget.dispatch('close', { code: ev.code, reason: ev.reason, wasClean: ev.wasClean, }); }); - websocket.addEventListener('error', function(ev) { - message({ - act: 'ws:error', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, - }); + websocket.addEventListener('error', ev => { + eventTarget.dispatch('error'); }); - websocket.addEventListener('message', function(ev) { - message({ - act: 'ws:message', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, + websocket.addEventListener('message', ev => { + eventTarget.dispatch('message', { data: ev.data, origin: ev.origin, lastEventId: ev.lastEventId, }); }); - send = function(text) { + send = text => { websocket.send(text); }; - close = function() { + close = () => { websocket.close(); }; - getIntervals = function() { - return intervals; - }; - sendInterval = function(text, interval) { - const intervalId = setInterval(function() { + sendInterval = (text, interval) => { + const intervalId = setInterval(() => { if(websocket) { websocket.send(text); - message({ act: 'ws:call_interval', id: intervalId }); + eventTarget.dispatch('call_interval', { id: intervalId }); } }, interval); intervals.push(intervalId); - message({ act: 'ws:create_interval', id: intervalId }); + eventTarget.dispatch('create_interval', { id: intervalId }); }; - clearIntervals = function() { + clearIntervals = () => { for(let i = 0; i < intervals.length; ++i) clearInterval(intervals[i]); }; @@ -111,8 +91,9 @@ const UmiWebSocket = function(server, message, useWorker) { isUsingWorker: useWorker, send: send, close: close, - getIntervals: getIntervals, sendInterval: sendInterval, clearIntervals: clearIntervals, + watch: eventTarget.watch, + unwatch: eventTarget.unwatch, }; }; diff --git a/src/websock.js/main.js b/src/websock.js/main.js index ca142ef..e7134df 100644 --- a/src/websock.js/main.js +++ b/src/websock.js/main.js @@ -6,46 +6,34 @@ addEventListener('message', function(ev) { switch(ev.data.act) { case 'ws:open': - if(!ev.data.server) + if(!ev.data.url) break; - websocket = new WebSocket(ev.data.server); + websocket = new WebSocket(ev.data.url); websocket.addEventListener('open', function(ev) { - postMessage({ - act: 'ws:open', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, - }); + postMessage({ act: 'ws:open' }); }); websocket.addEventListener('close', function(ev) { postMessage({ act: 'ws:close', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, - code: ev.code, - reason: ev.reason, - wasClean: ev.wasClean, + detail: { + code: ev.code, + reason: ev.reason, + wasClean: ev.wasClean, + }, }); }); websocket.addEventListener('error', function(ev) { - postMessage({ - act: 'ws:error', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, - }); + postMessage({ act: 'ws:error' }); }); websocket.addEventListener('message', function(ev) { postMessage({ act: 'ws:message', - type: ev.type, - timeStamp: ev.timeStamp, - isTrusted: ev.isTrusted, - data: ev.data, - origin: ev.origin, - lastEventId: ev.lastEventId, + detail: { + data: ev.data, + origin: ev.origin, + lastEventId: ev.lastEventId, + }, }); }); break; @@ -66,24 +54,17 @@ addEventListener('message', function(ev) { websocket.send(ev.data.text); break; - case 'ws:intervals': - postMessage({ - act: 'ws:intervals', - intervals: intervals, - }); - break; - case 'ws:send_interval': (function(interval, text) { const intervalId = setInterval(function() { if(websocket) { websocket.send(text); - postMessage({ act: 'ws:call_interval', id: intervalId }); + postMessage({ act: 'ws:call_interval', detail: { id: intervalId } }); } }, interval); intervals.push(intervalId); - postMessage({ act: 'ws:create_interval', id: intervalId }); + postMessage({ act: 'ws:create_interval', detail: { id: intervalId } }); })(ev.data.interval, ev.data.text); break;