@@ -0,0 +1,84 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
const WRONG_WORKER_CODE = 4013;
|
||||
const WRONG_WORKER_RETRY_MS = 200;
|
||||
const INITIAL_BACKOFF_MS = 200;
|
||||
const MAX_BACKOFF_MS = 5000;
|
||||
|
||||
export class ChatSocket {
|
||||
constructor(url, handlers) {
|
||||
this.url = url;
|
||||
this.handlers = handlers || {};
|
||||
this.socket = null;
|
||||
this._shouldRun = false;
|
||||
this._settled = false;
|
||||
this._backoff = INITIAL_BACKOFF_MS;
|
||||
}
|
||||
|
||||
connect() {
|
||||
this._shouldRun = true;
|
||||
this._open();
|
||||
}
|
||||
|
||||
isOpen() {
|
||||
return !!this.socket && this.socket.readyState === WebSocket.OPEN;
|
||||
}
|
||||
|
||||
_open() {
|
||||
this._settled = false;
|
||||
const socket = new WebSocket(this.url);
|
||||
this.socket = socket;
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
let payload;
|
||||
try {
|
||||
payload = JSON.parse(event.data);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
if (!this._settled) {
|
||||
this._settled = true;
|
||||
this._backoff = INITIAL_BACKOFF_MS;
|
||||
this._emit("onReady", payload);
|
||||
}
|
||||
this._emit("onMessage", payload);
|
||||
});
|
||||
|
||||
socket.addEventListener("close", (event) => {
|
||||
const wrongWorker = !this._settled && event.code === WRONG_WORKER_CODE;
|
||||
this._settled = false;
|
||||
if (wrongWorker) {
|
||||
if (this._shouldRun) {
|
||||
window.setTimeout(() => this._open(), WRONG_WORKER_RETRY_MS);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this._emit("onClose", event);
|
||||
if (this._shouldRun) {
|
||||
const delay = this._backoff;
|
||||
this._backoff = Math.min(this._backoff * 2, MAX_BACKOFF_MS);
|
||||
window.setTimeout(() => this._open(), delay);
|
||||
}
|
||||
});
|
||||
|
||||
socket.addEventListener("error", (event) => this._emit("onError", event));
|
||||
}
|
||||
|
||||
send(frame) {
|
||||
if (this.isOpen()) {
|
||||
this.socket.send(JSON.stringify(frame));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
close() {
|
||||
this._shouldRun = false;
|
||||
if (this.socket) this.socket.close();
|
||||
}
|
||||
|
||||
_emit(name, payload) {
|
||||
const handler = this.handlers[name];
|
||||
if (typeof handler === "function") handler(payload);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
export const GROUP_GAP_SECONDS = 300;
|
||||
|
||||
export function toEpochMs(value) {
|
||||
if (typeof value === "number") return Number.isFinite(value) ? value : null;
|
||||
if (!value) return null;
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isNaN(parsed) ? null : parsed;
|
||||
}
|
||||
|
||||
export function shouldGroup(previousMessage, currentMessage) {
|
||||
if (!previousMessage || !currentMessage) return false;
|
||||
if (previousMessage.senderUid !== currentMessage.senderUid) return false;
|
||||
const previousMs = toEpochMs(previousMessage.createdAt);
|
||||
const currentMs = toEpochMs(currentMessage.createdAt);
|
||||
if (previousMs === null || currentMs === null) return false;
|
||||
const gapSeconds = (currentMs - previousMs) / 1000;
|
||||
if (gapSeconds > GROUP_GAP_SECONDS) return false;
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user