Files
devplacepy/devplacepy/static/js/chat/ChatSocketHub.js
T
blindxfishandClaude Opus 5.5 17da9cb30c 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

73 lines
2.0 KiB
JavaScript

// retoor <retoor@molodetz.nl>
import { ChatSocket } from "./ChatSocket.js";
export class ChatSocketHub {
constructor(url) {
this.url = url;
this.socket = null;
this.ready = null;
this.subscribers = new Set();
}
subscribe(handlers) {
this.subscribers.add(handlers);
if (!this.socket) {
this._connect();
} else if (this.ready) {
queueMicrotask(() => {
if (this.ready && this.subscribers.has(handlers)) this._call(handlers, "onReady", this.ready);
});
}
return {
send: (frame) => this.send(frame),
isOpen: () => this.isOpen(),
close: () => this._unsubscribe(handlers),
};
}
send(frame) {
return !!this.socket && this.socket.send(frame);
}
isOpen() {
return !!this.socket && this.socket.isOpen();
}
_connect() {
const socket = new ChatSocket(this.url, {
onReady: (payload) => {
if (this.socket !== socket) return;
this.ready = payload;
this._emit("onReady", payload);
},
onMessage: (frame) => {
if (this.socket === socket) this._emit("onMessage", frame);
},
onClose: (event) => {
if (this.socket !== socket) return;
this.ready = null;
this._emit("onClose", event);
},
});
this.socket = socket;
socket.connect();
}
_unsubscribe(handlers) {
this.subscribers.delete(handlers);
if (this.subscribers.size || !this.socket) return;
this.socket.close();
this.socket = null;
this.ready = null;
}
_emit(name, payload) {
for (const handlers of [...this.subscribers]) this._call(handlers, name, payload);
}
_call(handlers, name, payload) {
if (typeof handlers[name] === "function") handlers[name](payload);
}
}