forked from adamritter/nostr-relaypool-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback-replayer.ts
59 lines (55 loc) · 1.56 KB
/
callback-replayer.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Cancellable<OnEvent> is perfect for this.
export type Cancellable<Process> = (process: Process) => () => void;
export type Callback<Args extends any[]> = (...args: Args) => void;
export class CancellableCallbackReplayer<Args extends any[]> {
events: Args[] = [];
unsubAll?: () => void;
subs: Set<Callback<Args>> = new Set();
constructor(cancellableCallback: Cancellable<Callback<Args>>) {
this.unsubAll = cancellableCallback((...args: Args) => {
this.events.push(args);
for (let sub of this.subs) {
sub(...args);
}
});
}
sub(): Cancellable<Callback<Args>> {
return (callback: Callback<Args>) => {
this.subs.add(callback);
this.events.forEach((arg) => callback(...arg));
return () => {
this.subs.delete(callback);
if (this.subs.size === 0) {
this.unsubAll?.();
this.unsubAll = undefined;
}
};
};
}
}
export class CallbackReplayer<
Args extends any[],
T extends (...args: Args) => void
> {
subs: T[] = [];
events: Args[] = [];
onunsub: (() => void) | undefined;
constructor(onunsub: (() => void) | undefined) {
this.onunsub = onunsub;
}
event(...args: Args) {
this.events.push(args);
this.subs.forEach((sub) => sub(...args));
}
sub(callback: T) {
this.events.forEach((event) => callback(...event));
this.subs.push(callback);
return () => {
this.subs = this.subs.filter((sub) => sub !== callback);
if (this.subs.length === 0) {
this.onunsub?.();
this.onunsub = undefined;
}
};
}
}