u
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
* after a successful mutation so other tabs/users can refetch.
|
||||
*/
|
||||
|
||||
/** @typedef {{ res: import('express').Response, companyId: number, userId: number, keepAlive: NodeJS.Timeout }} SseClient */
|
||||
/** @typedef {{ res: import('express').Response, companyId: number, userId: number, clientId: string|null, keepAlive: NodeJS.Timeout }} SseClient */
|
||||
|
||||
/** @type {Set<SseClient>} */
|
||||
const clients = new Set();
|
||||
@@ -13,16 +13,18 @@ const KEEP_ALIVE_MS = 20_000;
|
||||
/**
|
||||
* Register an open SSE response. Caller must write the initial headers.
|
||||
* @param {import('express').Response} res
|
||||
* @param {{ companyId: number, userId: number }} meta
|
||||
* @param {{ companyId: number, userId: number, clientId?: string|null }} meta
|
||||
* @param {import('express').Request} [req]
|
||||
* @returns {() => void} unsubscribe
|
||||
*/
|
||||
export function subscribe(res, { companyId, userId }, req) {
|
||||
export function subscribe(res, { companyId, userId, clientId }, req) {
|
||||
const resolvedClientId = clientId || req?.headers?.['x-client-id'] || req?.query?.cid || null;
|
||||
/** @type {SseClient} */
|
||||
const client = {
|
||||
res,
|
||||
companyId,
|
||||
userId,
|
||||
clientId: resolvedClientId ? String(resolvedClientId) : null,
|
||||
keepAlive: setInterval(() => {
|
||||
if (res.writableEnded) return;
|
||||
res.write(': heartbeat\n\n');
|
||||
@@ -55,15 +57,59 @@ export function closeAll() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast a named signal to subscribers of the given company.
|
||||
* @param {string} signal - SSE event name (e.g. `category`)
|
||||
* @param {{ companyId: number, data?: string|object }} options
|
||||
* Checks whether an active SSE client connection exists matching the given criteria.
|
||||
* @param {{ companyId?: number, userId?: number, clientId?: string }} [options]
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function emit(signal, { companyId, data }) {
|
||||
export function hasActiveClient({ companyId, userId, clientId } = {}) {
|
||||
for (const client of clients) {
|
||||
if (companyId != null && client.companyId !== companyId) continue;
|
||||
if (userId != null && client.userId !== userId) continue;
|
||||
if (clientId != null && client.clientId !== clientId) continue;
|
||||
if (!client.res.writableEnded) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast or target a named signal to subscribers.
|
||||
* If `targetClientId` or `targetUserId` is specified, sends only to matching clients of that user.
|
||||
*
|
||||
* @param {string} signal - SSE event name (e.g. `category`, `draft`, `draft:request`)
|
||||
* @param {{ companyId: number, data?: string|object, targetClientId?: string, targetUserId?: number }} options
|
||||
*/
|
||||
export function emit(signal, { companyId, data, targetClientId, targetUserId }) {
|
||||
if (companyId == null) return;
|
||||
|
||||
const payload = data == null ? '{}' : (typeof data === 'string' ? data : JSON.stringify(data));
|
||||
const frame = `event: ${signal}\ndata: ${payload}\n\n`;
|
||||
|
||||
if (targetClientId || targetUserId != null) {
|
||||
let sent = false;
|
||||
if (targetClientId) {
|
||||
for (const client of clients) {
|
||||
if (client.companyId !== companyId) continue;
|
||||
if (targetUserId != null && client.userId !== targetUserId) continue;
|
||||
if (client.clientId === targetClientId && !client.res.writableEnded) {
|
||||
client.res.write(frame);
|
||||
sent = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!sent && targetUserId != null) {
|
||||
for (const client of clients) {
|
||||
if (client.companyId !== companyId) continue;
|
||||
if (client.userId === targetUserId && !client.res.writableEnded) {
|
||||
client.res.write(frame);
|
||||
sent = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sent) return;
|
||||
// Targeted to a specific user but no active client was found: do not blast to other users
|
||||
if (targetUserId != null) return;
|
||||
}
|
||||
|
||||
for (const client of clients) {
|
||||
if (client.companyId !== companyId) continue;
|
||||
if (client.res.writableEnded) continue;
|
||||
|
||||
Reference in New Issue
Block a user