Made UmiWebSocket use EventTarget instead of taking a massive callback.

This commit is contained in:
flash 2024-03-01 20:35:44 +00:00
parent 9f61c0d4b9
commit b37352534b
4 changed files with 64 additions and 126 deletions

View file

@ -622,9 +622,6 @@ window.Umi = { UI: {}, Protocol: { SockChat: { Protocol: {} } } };
wsCloseReasons[`_${ev.detail.code}`] ?? `Something caused an unexpected connection loss. (${ev.detail.code})` 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 => { sockChat.watch('ping:send', ev => {
if(dumpEvents) console.log('ping:send', ev); if(dumpEvents) console.log('ping:send', ev);

View file

@ -120,7 +120,7 @@ Umi.Protocol.SockChat.Protocol = function(pingDuration) {
if(wasKicked) if(wasKicked)
return; return;
let code = ev.code; let code = ev.detail.code;
if(isRestarting && code === 1006) { if(isRestarting && code === 1006) {
code = 1012; code = 1012;
} else if(code === 1012) } else if(code === 1012)
@ -137,19 +137,10 @@ Umi.Protocol.SockChat.Protocol = function(pingDuration) {
setTimeout(() => beginConnecting(), 5000); setTimeout(() => beginConnecting(), 5000);
}; };
const onError = ex => { const unfuckText = text => text.replace(/ <br\/> /g, "\n");
eventTarget.dispatch('conn:error', ex);
};
const unfuckText = text => {
const elem = document.createElement('div');
elem.innerHTML = text.replace(/ <br\/> /g, "\n");
text = elem.innerText;
return text;
};
const onMessage = ev => { const onMessage = ev => {
const args = ev.data.split("\t"); const args = ev.detail.data.split("\t");
let handler = handlers; let handler = handlers;
if(dumpPackets) if(dumpPackets)
@ -556,31 +547,19 @@ Umi.Protocol.SockChat.Protocol = function(pingDuration) {
attempt: ++connectAttempts, attempt: ++connectAttempts,
}); });
sock = new UmiWebSocket(server, ev => { sock = new UmiWebSocket(server);
switch(ev.act) { sock.watch('open', onOpen);
case 'ws:open': sock.watch('close', onClose);
onOpen(ev); sock.watch('message', onMessage);
break; sock.watch('create_interval', ev => {
case 'ws:close': pingTimer = ev.detail.id;
onClose(ev); });
break; sock.watch('call_interval', ev => {
case 'ws:message': if(ev.detail.id === pingTimer)
onMessage(ev);
break;
case 'ws:create_interval':
pingTimer = ev.id;
break;
case 'ws:call_interval':
if(ev.id === pingTimer)
onSendPing(); onSendPing();
break; });
case 'ws:clear_intervals': sock.watch('create_intervals', ev => {
pingTimer = undefined; pingTimer = undefined;
break;
default:
console.log(ev.data);
break;
}
}); });
}); });
}; };

View file

@ -1,6 +1,8 @@
const UmiWebSocket = function(server, message, useWorker) { #include eventtarget.js
if(typeof useWorker === 'undefined')
useWorker = (function() { const UmiWebSocket = function(url, useWorker) {
if(typeof useWorker !== 'boolean')
useWorker = (() => {
// Overrides // Overrides
if(mami.settings.get('neverUseWorker')) if(mami.settings.get('neverUseWorker'))
return false; return false;
@ -18,90 +20,68 @@ const UmiWebSocket = function(server, message, useWorker) {
return false; return false;
})(); })();
const eventTarget = new MamiEventTarget('ws');
let send, close, sendInterval, clearIntervals; let send, close, sendInterval, clearIntervals;
if(useWorker) { if(useWorker) {
const worker = new Worker(MAMI_WS); const worker = new Worker(MAMI_WS);
worker.addEventListener('message', function(ev) { worker.addEventListener('message', ev => {
message(ev.data); if(ev.data.act.startsWith('ws:'))
eventTarget.dispatch(ev.data.act.substring(3), ev.data.detail);
}); });
worker.postMessage({act: 'ws:open', server: server}); worker.postMessage({act: 'ws:open', url: url});
send = function(text) { send = text => {
worker.postMessage({act: 'ws:send', text: text}); worker.postMessage({act: 'ws:send', text: text});
}; };
close = function() { close = () => {
worker.postMessage({act: 'ws:close'}); worker.postMessage({act: 'ws:close'});
}; };
getIntervals = function() { sendInterval = (text, interval) => {
worker.postMessage({act: 'ws:intervals'});
};
sendInterval = function(text, interval) {
worker.postMessage({act: 'ws:send_interval', text: text, interval: interval}); worker.postMessage({act: 'ws:send_interval', text: text, interval: interval});
}; };
clearIntervals = function() { clearIntervals = () => {
worker.postMessage({act: 'ws:clear_intervals'}); worker.postMessage({act: 'ws:clear_intervals'});
}; };
} else { } else {
const websocket = new WebSocket(server), intervals = []; const websocket = new WebSocket(url), intervals = [];
websocket.addEventListener('open', function(ev) { websocket.addEventListener('open', ev => {
message({ eventTarget.dispatch('open');
act: 'ws:open',
type: ev.type,
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
}); });
}); websocket.addEventListener('close', ev => {
websocket.addEventListener('close', function(ev) { eventTarget.dispatch('close', {
message({
act: 'ws:close',
type: ev.type,
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
code: ev.code, code: ev.code,
reason: ev.reason, reason: ev.reason,
wasClean: ev.wasClean, wasClean: ev.wasClean,
}); });
}); });
websocket.addEventListener('error', function(ev) { websocket.addEventListener('error', ev => {
message({ eventTarget.dispatch('error');
act: 'ws:error',
type: ev.type,
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
}); });
}); websocket.addEventListener('message', ev => {
websocket.addEventListener('message', function(ev) { eventTarget.dispatch('message', {
message({
act: 'ws:message',
type: ev.type,
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
data: ev.data, data: ev.data,
origin: ev.origin, origin: ev.origin,
lastEventId: ev.lastEventId, lastEventId: ev.lastEventId,
}); });
}); });
send = function(text) { send = text => {
websocket.send(text); websocket.send(text);
}; };
close = function() { close = () => {
websocket.close(); websocket.close();
}; };
getIntervals = function() { sendInterval = (text, interval) => {
return intervals; const intervalId = setInterval(() => {
};
sendInterval = function(text, interval) {
const intervalId = setInterval(function() {
if(websocket) { if(websocket) {
websocket.send(text); websocket.send(text);
message({ act: 'ws:call_interval', id: intervalId }); eventTarget.dispatch('call_interval', { id: intervalId });
} }
}, interval); }, interval);
intervals.push(intervalId); 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) for(let i = 0; i < intervals.length; ++i)
clearInterval(intervals[i]); clearInterval(intervals[i]);
}; };
@ -111,8 +91,9 @@ const UmiWebSocket = function(server, message, useWorker) {
isUsingWorker: useWorker, isUsingWorker: useWorker,
send: send, send: send,
close: close, close: close,
getIntervals: getIntervals,
sendInterval: sendInterval, sendInterval: sendInterval,
clearIntervals: clearIntervals, clearIntervals: clearIntervals,
watch: eventTarget.watch,
unwatch: eventTarget.unwatch,
}; };
}; };

View file

@ -6,46 +6,34 @@ addEventListener('message', function(ev) {
switch(ev.data.act) { switch(ev.data.act) {
case 'ws:open': case 'ws:open':
if(!ev.data.server) if(!ev.data.url)
break; break;
websocket = new WebSocket(ev.data.server); websocket = new WebSocket(ev.data.url);
websocket.addEventListener('open', function(ev) { websocket.addEventListener('open', function(ev) {
postMessage({ postMessage({ act: 'ws:open' });
act: 'ws:open',
type: ev.type,
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
});
}); });
websocket.addEventListener('close', function(ev) { websocket.addEventListener('close', function(ev) {
postMessage({ postMessage({
act: 'ws:close', act: 'ws:close',
type: ev.type, detail: {
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
code: ev.code, code: ev.code,
reason: ev.reason, reason: ev.reason,
wasClean: ev.wasClean, wasClean: ev.wasClean,
},
}); });
}); });
websocket.addEventListener('error', function(ev) { websocket.addEventListener('error', function(ev) {
postMessage({ postMessage({ act: 'ws:error' });
act: 'ws:error',
type: ev.type,
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
});
}); });
websocket.addEventListener('message', function(ev) { websocket.addEventListener('message', function(ev) {
postMessage({ postMessage({
act: 'ws:message', act: 'ws:message',
type: ev.type, detail: {
timeStamp: ev.timeStamp,
isTrusted: ev.isTrusted,
data: ev.data, data: ev.data,
origin: ev.origin, origin: ev.origin,
lastEventId: ev.lastEventId, lastEventId: ev.lastEventId,
},
}); });
}); });
break; break;
@ -66,24 +54,17 @@ addEventListener('message', function(ev) {
websocket.send(ev.data.text); websocket.send(ev.data.text);
break; break;
case 'ws:intervals':
postMessage({
act: 'ws:intervals',
intervals: intervals,
});
break;
case 'ws:send_interval': case 'ws:send_interval':
(function(interval, text) { (function(interval, text) {
const intervalId = setInterval(function() { const intervalId = setInterval(function() {
if(websocket) { if(websocket) {
websocket.send(text); websocket.send(text);
postMessage({ act: 'ws:call_interval', id: intervalId }); postMessage({ act: 'ws:call_interval', detail: { id: intervalId } });
} }
}, interval); }, interval);
intervals.push(intervalId); intervals.push(intervalId);
postMessage({ act: 'ws:create_interval', id: intervalId }); postMessage({ act: 'ws:create_interval', detail: { id: intervalId } });
})(ev.data.interval, ev.data.text); })(ev.data.interval, ev.data.text);
break; break;