2026-06-02 00:11:29 +07:00
|
|
|
/**
|
2026-06-09 11:56:03 +07:00
|
|
|
* Broadcast functions for WebSocket events.
|
2026-06-02 00:11:29 +07:00
|
|
|
*
|
2026-06-09 11:56:03 +07:00
|
|
|
* These are injected by ws/server.ts when the WebSocket server initializes.
|
2026-06-02 00:11:29 +07:00
|
|
|
* Other modules call them to push real-time events to connected frontend clients.
|
|
|
|
|
*
|
|
|
|
|
* Usage:
|
|
|
|
|
* import { broadcastMessageCreated } from "../ws/broadcast.js";
|
|
|
|
|
* broadcastMessageCreated(messageData);
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
type BroadcastFn = (data: unknown) => void;
|
2026-06-02 00:32:38 +07:00
|
|
|
type BroadcastRawFn = (type: string, data: unknown) => void;
|
2026-06-08 19:14:34 +07:00
|
|
|
type BroadcastBinaryFn = (data: Buffer) => void;
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-09 11:56:03 +07:00
|
|
|
export interface BroadcastFunctions {
|
|
|
|
|
messageCreated: BroadcastFn;
|
|
|
|
|
messageUpdated: BroadcastFn;
|
|
|
|
|
messageDeleted: BroadcastFn;
|
|
|
|
|
attachmentUploaded: BroadcastFn;
|
|
|
|
|
raw: BroadcastRawFn;
|
|
|
|
|
binary: BroadcastBinaryFn;
|
2026-06-02 00:11:29 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const noop: BroadcastFn = () => {};
|
2026-06-02 00:32:38 +07:00
|
|
|
const noopRaw: BroadcastRawFn = () => {};
|
2026-06-08 19:14:34 +07:00
|
|
|
const noopBinary: BroadcastBinaryFn = () => {};
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-09 11:56:03 +07:00
|
|
|
let _fns: BroadcastFunctions | null = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inject broadcast functions from the WebSocket server initializer.
|
|
|
|
|
* Must be called once during server startup before any broadcast is used.
|
|
|
|
|
*/
|
|
|
|
|
export function setBroadcastFunctions(fns: BroadcastFunctions): void {
|
|
|
|
|
_fns = fns;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Clear injected functions (used during cleanup). */
|
|
|
|
|
export function clearBroadcastFunctions(): void {
|
|
|
|
|
_fns = null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-02 00:32:38 +07:00
|
|
|
export const broadcastMessageCreated: BroadcastFn = (data) =>
|
2026-06-09 11:56:03 +07:00
|
|
|
(_fns?.messageCreated ?? noop)(data);
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-02 00:32:38 +07:00
|
|
|
export const broadcastMessageUpdated: BroadcastFn = (data) =>
|
2026-06-09 11:56:03 +07:00
|
|
|
(_fns?.messageUpdated ?? noop)(data);
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-02 00:32:38 +07:00
|
|
|
export const broadcastMessageDeleted: BroadcastFn = (data) =>
|
2026-06-09 11:56:03 +07:00
|
|
|
(_fns?.messageDeleted ?? noop)(data);
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-02 00:32:38 +07:00
|
|
|
export const broadcastAttachmentUploaded: BroadcastFn = (data) =>
|
2026-06-09 11:56:03 +07:00
|
|
|
(_fns?.attachmentUploaded ?? noop)(data);
|
2026-06-02 00:32:38 +07:00
|
|
|
|
|
|
|
|
export const broadcastRaw: BroadcastRawFn = (type, data) =>
|
2026-06-09 11:56:03 +07:00
|
|
|
(_fns?.raw ?? noopRaw)(type, data);
|
2026-06-08 19:14:34 +07:00
|
|
|
|
|
|
|
|
export const broadcastBinary: BroadcastBinaryFn = (data) =>
|
2026-06-09 11:56:03 +07:00
|
|
|
(_fns?.binary ?? noopBinary)(data);
|