-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.ts
More file actions
101 lines (81 loc) · 2.72 KB
/
Copy pathindex.ts
File metadata and controls
101 lines (81 loc) · 2.72 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { getGlobal, registerGlobal } from "../utils/globals.js";
import { NoopSessionStreamManager } from "./noopManager.js";
import {
InputStreamOncePromise,
SessionChannelIO,
SessionStreamManager,
} from "./types.js";
import { InputStreamOnceOptions } from "../realtimeStreams/types.js";
const API_NAME = "session-streams";
const NOOP_MANAGER = new NoopSessionStreamManager();
export class SessionStreamsAPI implements SessionStreamManager {
private static _instance?: SessionStreamsAPI;
private constructor() {}
public static getInstance(): SessionStreamsAPI {
if (!this._instance) {
this._instance = new SessionStreamsAPI();
}
return this._instance;
}
setGlobalManager(manager: SessionStreamManager): boolean {
return registerGlobal(API_NAME, manager);
}
#getManager(): SessionStreamManager {
return getGlobal(API_NAME) ?? NOOP_MANAGER;
}
public on(
sessionId: string,
io: SessionChannelIO,
handler: (data: unknown) => void | boolean | Promise<void>
): { off: () => void } {
return this.#getManager().on(sessionId, io, handler);
}
public once(
sessionId: string,
io: SessionChannelIO,
options?: InputStreamOnceOptions
): InputStreamOncePromise<unknown> {
return this.#getManager().once(sessionId, io, options);
}
public peek(sessionId: string, io: SessionChannelIO): unknown | undefined {
return this.#getManager().peek(sessionId, io);
}
public lastSeqNum(sessionId: string, io: SessionChannelIO): number | undefined {
return this.#getManager().lastSeqNum(sessionId, io);
}
public setLastSeqNum(sessionId: string, io: SessionChannelIO, seqNum: number): void {
this.#getManager().setLastSeqNum(sessionId, io, seqNum);
}
public lastDispatchedSeqNum(sessionId: string, io: SessionChannelIO): number | undefined {
return this.#getManager().lastDispatchedSeqNum(sessionId, io);
}
public setLastDispatchedSeqNum(
sessionId: string,
io: SessionChannelIO,
seqNum: number
): void {
this.#getManager().setLastDispatchedSeqNum(sessionId, io, seqNum);
}
public setMinTimestamp(
sessionId: string,
io: SessionChannelIO,
minTimestamp: number | undefined
): void {
this.#getManager().setMinTimestamp(sessionId, io, minTimestamp);
}
public shiftBuffer(sessionId: string, io: SessionChannelIO): boolean {
return this.#getManager().shiftBuffer(sessionId, io);
}
public disconnectStream(sessionId: string, io: SessionChannelIO): void {
this.#getManager().disconnectStream(sessionId, io);
}
public clearHandlers(): void {
this.#getManager().clearHandlers();
}
public reset(): void {
this.#getManager().reset();
}
public disconnect(): void {
this.#getManager().disconnect();
}
}