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})`
);
});
sockChat.watch('conn:error', ev => {
console.error('conn:error', ev);
});
sockChat.watch('ping:send', ev => {
if(dumpEvents) console.log('ping:send', ev);

View file

@ -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(/ <br\/> /g, "\n");
text = elem.innerText;
return text;
};
const unfuckText = text => text.replace(/ <br\/> /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)
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();
break;
case 'ws:clear_intervals':
});
sock.watch('create_intervals', ev => {
pingTimer = undefined;
break;
default:
console.log(ev.data);
break;
}
});
});
};

View file

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

View file

@ -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,
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,
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;