feat: complete TASK-WP0-02 contract baseline

This commit is contained in:
suyx
2026-07-27 17:49:24 +08:00
parent ae20eaf01f
commit dbe3e73b91
28 changed files with 2540 additions and 23 deletions
+31
View File
@@ -0,0 +1,31 @@
import { isSseEvent, type SseEvent } from "@dada/shared-contracts";
interface Connection {
close: () => void;
send: (event: SseEvent) => void;
}
export class EventHub {
readonly #connections = new Set<Connection>();
get subscriberCount() {
return this.#connections.size;
}
connect(send: Connection["send"], close: Connection["close"]) {
const connection = { close, send };
this.#connections.add(connection);
return () => this.#connections.delete(connection);
}
publish(event: SseEvent) {
if (!isSseEvent(event)) throw new Error("SSE event does not match the frozen non-sensitive schema.");
for (const connection of this.#connections) connection.send(event);
}
disconnectAll() {
const connections = [...this.#connections];
this.#connections.clear();
for (const connection of connections) connection.close();
}
}