Files
devplacepy/devplacepy/static/js/chat/ChatSocket.js
T
blindxfishandClaude Opus 5.5 17da9cb30c
DevPlace CI / test (pull_request) Failing after 1h7m10s
Messages: Facebook-style Chats panel and docked chat windows
The topnav Messages icon opens a Chats panel (people search, recent
conversations, New message, link to the full page). Picking a person
opens a small chat window docked bottom-right; windows minimize to
avatar heads with unread counts and survive navigation within the tab.
Profile Send Message, the live message toast and message notifications
on /notifications open the same window. Phones and /messages keep the
full page.

Each window is dp-chat in mode="window", sharing one WebSocket through
chat/ChatSocketHub.js; chat/ConversationItem.js is the shared row
builder. No server changes.

The message input becomes [+] [text + emoji] [send] everywhere: + reveals
the attach and GIF buttons, which tuck away while typing.

Also fixes linked toasts never receiving clicks (pointer-events).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 13:21:29 +01:00

113 lines
3.2 KiB
JavaScript

// 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;
const PING_MS = 25000;
export class ChatSocket {
static resolveUrl(path) {
if (/^wss?:\/\//i.test(path)) return path;
const scheme = location.protocol === "https:" ? "wss:" : "ws:";
const normalized = path.startsWith("/") ? path : `/${path}`;
return `${scheme}//${location.host}${normalized}`;
}
constructor(url, handlers) {
this.url = url;
this.handlers = handlers || {};
this.socket = null;
this._shouldRun = false;
this._settled = false;
this._backoff = INITIAL_BACKOFF_MS;
this._pingId = null;
}
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("open", () => this._startPing());
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 && payload.type === "ready" ? payload : { type: "ready" });
}
if (payload && payload.type === "ready") return;
this._emit("onMessage", payload);
});
socket.addEventListener("close", (event) => {
this._stopPing();
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;
this._stopPing();
if (this.socket) this.socket.close();
}
_startPing() {
this._stopPing();
this._pingId = window.setInterval(() => {
if (this.isOpen()) this.send({ type: "ping" });
}, PING_MS);
}
_stopPing() {
if (this._pingId) {
window.clearInterval(this._pingId);
this._pingId = null;
}
}
_emit(name, payload) {
const handler = this.handlers[name];
if (typeof handler === "function") handler(payload);
}
}