/**
 * Simple pub/sub bus for SSE event_log payloads.
 *
 * The layout component publishes events from the single global SSE connection.
 * Pages (e.g. /events) subscribe without opening a duplicate SSE connection.
 */

import type { EventLogSSEPayload } from '$lib/sse';

type Listener = (payload: EventLogSSEPayload) => void;

const listeners = new Set<Listener>();

export function subscribeEventLog(fn: Listener): () => void {
	listeners.add(fn);
	return () => { listeners.delete(fn); };
}

export function publishEventLog(payload: EventLogSSEPayload): void {
	for (const fn of listeners) {
		fn(payload);
	}
}
