mami/src/proto.js/timedp.js
flash cf71bab92d Rewrote connection handling.
This has been in the works for over a month and might break things because it's a very radical change.
If it causes you to be unable to join chat, report it on the forum or try joining using the legacy chat on https://sockchat.flashii.net.
2024-04-17 15:42:50 +00:00

49 lines
1.1 KiB
JavaScript

const TimedPromise = function(resolve, reject, always, timeoutMs) {
let timeout, resolved = false;
const cancelTimeout = () => {
if(timeout === undefined) {
clearTimeout(timeout);
timeout = undefined;
}
};
const doResolve = (...args) => {
if(resolved) return;
resolved = true;
cancelTimeout();
reject = undefined;
if(typeof resolve === 'function')
resolve(...args);
if(typeof always === 'function')
always();
};
const doReject = (...args) => {
if(resolved) return;
resolved = true;
cancelTimeout();
resolve = undefined;
if(typeof reject === 'function')
reject(...args);
if(typeof always === 'function')
always();
};
timeout = setTimeout(() => doReject('timeout'), timeoutMs);
return {
resolve: doResolve,
reject: doReject,
cancel: () => {
if(timeout === undefined)
return;
doReject('timeout');
},
};
};