feat: rooms broadcast-hub primitive, auto-provided to handlers
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+68
No files matched your search
@@ -13,6 +13,7 @@ import {
|
||||
} from './metadata';
|
||||
import { getArgs, getHandlerNeeds, resolveArgs } from './params';
|
||||
import { connectLink, type Link } from './mesh/link';
|
||||
import { Rooms } from './rooms';
|
||||
import { buildRemote } from './mesh/teacup';
|
||||
import { buildManifest, createMeshControl } from './mesh/teapot';
|
||||
import type { MeshControl } from './http';
|
||||
@@ -126,6 +127,14 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
|
||||
setRunner(n.name, n.run);
|
||||
}
|
||||
|
||||
// built-in: auto-provide a shared Rooms instance (unless the user declares one)
|
||||
if (!runners.has('rooms')) {
|
||||
const roomsInstance = new Rooms();
|
||||
providerNodes.push({ name: 'rooms', needs: [], provides: ['rooms'], origin: 'builtin' });
|
||||
providerMeta.set('rooms', { optional: false });
|
||||
setRunner('rooms', () => ({ rooms: roomsInstance })); // merge object -> ctx.rooms
|
||||
}
|
||||
|
||||
// For each route, resolve which providers/steps feed it via topo order.
|
||||
// MVP: every route depends on every declared step + provider in the app graph.
|
||||
// For mesh apps, finalize is DEFERRED to listen() so remote scopes can join the graph first.
|
||||
|
||||
@@ -16,6 +16,7 @@ export { needs, ctx, param, query, body, headers } from './params';
|
||||
export type { Query, Headers, Context, ArgSpec } from './params';
|
||||
export { channel, isAsyncIterable } from './channel';
|
||||
export type { Channel } from './channel';
|
||||
export { Rooms } from './rooms';
|
||||
export { Sse, Ws, Stream, Post, Put, Patch, Delete } from './metadata';
|
||||
export type { Transport, HttpMethod } from './metadata';
|
||||
export { inbound, abort } from './params';
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { channel, type Channel } from './channel';
|
||||
|
||||
export class Rooms {
|
||||
private readonly hubs = new Map<string, Channel<unknown>>();
|
||||
room<T = unknown>(name: string): Channel<T> {
|
||||
let hub = this.hubs.get(name);
|
||||
if (!hub) { hub = channel<unknown>(); this.hubs.set(name, hub); }
|
||||
return hub as unknown as Channel<T>;
|
||||
}
|
||||
names(): string[] { return [...this.hubs.keys()]; }
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user