feat: rooms broadcast-hub primitive, auto-provided to handlers

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-06-28 13:23:54 -05:00
1 parent 2bbc43505e
commit 6171e5e6bd
5 files changed
+68

No files matched your search

+19
View File
@@ -216,6 +216,7 @@ describe('per-route subgraph slicing', () => {
});
});
import { Rooms } from '../src/rooms';
import { toMermaid as renderMermaid } from '../src/graph-viz';
describe('app.graph / explain', () => {
@@ -317,3 +318,21 @@ describe('overrides', () => {
await expect(app.listen(0)).rejects.toThrow(/unknown token 'nope'/);
});
});
describe('rooms auto-provide', () => {
it('injects a built-in rooms instance resolvable via @needs("rooms")', async () => {
@Route('/r') class Ctl { @Get('/x') x(@needs('rooms') rooms: Rooms) { return { ok: rooms instanceof Rooms }; } }
@Module({ mountpoint: '/api', controllers: [Ctl] }) class M {}
const app = createApp({ modules: [M] });
const s = await app.listen(0);
const port = (s.address() as any).port;
expect(await fetch(`http://127.0.0.1:${port}/api/r/x`).then((r) => r.json())).toEqual({ ok: true });
s.close();
});
it('a user-declared rooms provider wins (no duplicate-name throw)', () => {
@Provider({ provides: 'rooms' }) class MyRooms { provide() { return { rooms: { custom: true } }; } }
@Route('/r') class Ctl { @Get('/x') x(@needs('rooms') rooms: any) { return rooms; } }
@Module({ mountpoint: '/api', providers: [MyRooms], controllers: [Ctl] }) class M {}
expect(() => createApp({ modules: [M] })).not.toThrow();
});
});
+28
View File
@@ -0,0 +1,28 @@
import { describe, it, expect } from 'vitest';
import { Rooms } from '../src/rooms';
describe('Rooms', () => {
it('returns the same shared channel for a given name', () => {
const r = new Rooms();
expect(r.room('general')).toBe(r.room('general'));
expect(r.room('a')).not.toBe(r.room('b'));
});
it('broadcasts a push to all current subscribers of a room', async () => {
const r = new Rooms();
const hub = r.room<number>('x');
const a = hub[Symbol.asyncIterator]();
const b = hub[Symbol.asyncIterator]();
await Promise.resolve();
hub.push(7);
expect((await a.next()).value).toBe(7);
expect((await b.next()).value).toBe(7);
});
it('isolates distinct rooms', async () => {
const r = new Rooms();
const it = r.room<number>('a')[Symbol.asyncIterator]();
await Promise.resolve();
r.room<number>('b').push(1);
r.room<number>('a').push(2);
expect((await it.next()).value).toBe(2);
});
});