-
Notifications
You must be signed in to change notification settings - Fork 5
/
subscription.manager.ts
196 lines (171 loc) · 5.39 KB
/
subscription.manager.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { CensusClient } from './census.client';
import { StreamClient } from '../stream/stream.client';
import { PS2EventNames } from '../stream/types/ps2.events';
import { EventSubscribed, EventSubscription } from './types';
import { CommandHandler } from './command.handler';
import { StreamResponseException } from './exceptions/stream-response.exception';
import nextTick from '../utils/next-tick';
export class SubscriptionManager {
/**
* Characters subscribed to
*/
private readonly characters: Set<string>;
/**
* Worlds subscribed to
*/
private readonly worlds: Set<string>;
/**
* Events to listen to
*/
private readonly eventNames: Set<
'all' | PS2EventNames | `GainExperience_experience_id_${number}`
>;
/**
* Whether both worlds and characters should match
*/
private logicalAndCharactersWithWorlds: boolean;
/**
* @param {CensusClient} client the client used to emit debug events
* @param {StreamClient} stream the stream to comment, like, and subscribe to
* @param {CommandHandler} commandHandler handles rpc of the stream
* @param {EventSubscription} subscription the initial subscription
*/
constructor(
private readonly client: CensusClient,
private readonly stream: StreamClient,
private readonly commandHandler: CommandHandler,
subscription: EventSubscription = {},
) {
this.characters = new Set(subscription.characters);
this.worlds = new Set(subscription.worlds);
this.eventNames = new Set(subscription.eventNames);
this.logicalAndCharactersWithWorlds =
subscription.logicalAndCharactersWithWorlds ?? false;
this.registerClientEvents();
}
/**
* Registers important stream events
*/
private registerClientEvents(): void {
this.stream.on('ready', async () => {
nextTick(() => this.client.emit('debug', `Subscribing to events`));
try {
await this.commandHandler.subscribe(this.subscription);
} catch (err) {
if (!(err instanceof StreamResponseException)) throw err;
}
});
}
/**
* Make a subscription to the stream
*
* @param {EventSubscription} subscription
*/
subscribe({
characters,
worlds,
eventNames,
logicalAndCharactersWithWorlds,
}: EventSubscription): Promise<EventSubscribed> {
characters?.forEach(character => this.characters.add(character));
worlds?.forEach(world => this.worlds.add(world));
eventNames?.forEach(eventName => this.eventNames.add(eventName));
this.logicalAndCharactersWithWorlds =
logicalAndCharactersWithWorlds ?? this.logicalAndCharactersWithWorlds;
return this.commandHandler.subscribe({
characters,
worlds,
eventNames,
logicalAndCharactersWithWorlds,
});
}
/**
* Remove a subscription from the stream
*
* @param {EventSubscription} subscription
*/
unsubscribe({
characters,
worlds,
eventNames,
logicalAndCharactersWithWorlds,
}: EventSubscription): Promise<EventSubscribed> {
characters?.forEach(character => this.characters.delete(character));
worlds?.forEach(world => this.worlds.delete(world));
eventNames?.forEach(eventName => this.eventNames.delete(eventName));
this.logicalAndCharactersWithWorlds =
logicalAndCharactersWithWorlds ?? this.logicalAndCharactersWithWorlds;
return this.commandHandler.clearSubscribe({
characters,
worlds,
eventNames,
logicalAndCharactersWithWorlds,
});
}
/**
* Purge all subscriptions
*/
async unsubscribeAll(): Promise<void> {
this.characters.clear();
this.worlds.clear();
this.eventNames.clear();
this.logicalAndCharactersWithWorlds = false;
await this.commandHandler.clearSubscribeAll();
}
/**
* Verifies if the subscription matches with what is expected
*
* @return {Promise<boolean>} whether the subscription is correct
*/
async verifySubscription(): Promise<boolean> {
const subscription = await this.commandHandler.subscribe({});
if (
this.logicalAndCharactersWithWorlds !=
subscription.logicalAndCharactersWithWorlds
)
return false;
if (this.characters.has('all')) {
if (!('characters' in subscription)) return false;
} else {
if (
!('characterCount' in subscription) ||
subscription.characterCount != this.characters.size
)
return false;
}
for (const world of this.worlds) {
if (!subscription.worlds.some(id => world == id)) return false;
}
for (const event of this.eventNames) {
if (!subscription.eventNames.some(name => event == name)) return false;
}
return true;
}
/**
* Rerun all subscriptions
*
* @param {boolean} reset When true unsubscribes from all events first
* @return {Promise<boolean>} whether it has been run(depends on stream being ready)
*/
async resubscribe(reset = false): Promise<boolean> {
if (this.stream.isReady) {
if (reset) await this.commandHandler.clearSubscribeAll();
await this.commandHandler.subscribe(this.subscription);
return true;
}
return false;
}
/**
* Get current subscription
*
* @return {Subscribe}
*/
private get subscription(): EventSubscription {
return {
characters: Array.from(this.characters),
worlds: Array.from(this.worlds),
eventNames: Array.from(this.eventNames),
logicalAndCharactersWithWorlds: this.logicalAndCharactersWithWorlds,
};
}
}