-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhub.ts
42 lines (34 loc) · 1.09 KB
/
hub.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as fmt from './sse_formatter';
import { ISseFunctions } from './sse_middleware';
export interface IHub extends ISseFunctions {
/**
* Remember a new client.
*
* @param funcs SSE functions bound to the client.
*/
register(funcs: ISseFunctions): void;
/**
* Unregister a known client.
*
* @param funcs SSE function to unregister.
*/
unregister(funcs: ISseFunctions): void;
}
export class Hub implements IHub {
protected readonly clients = new Set<ISseFunctions>();
public register(funcs: ISseFunctions): void {
this.clients.add(funcs);
}
public unregister(funcs: ISseFunctions): void {
this.clients.delete(funcs);
}
public data(data: fmt.SseValue, id?: string): void {
this.clients.forEach(client => client.data(data, id));
}
public event(event: string, data: fmt.SseValue, id?: string): void {
this.clients.forEach(client => client.event(event, data, id));
}
public comment(comment: string): void {
this.clients.forEach(client => client.comment(comment));
}
}