-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathoverlay_plugin_api.ts
313 lines (264 loc) · 8.25 KB
/
overlay_plugin_api.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// OverlayPlugin API setup
import {
EventMap,
EventType,
IOverlayHandler,
OverlayHandlerFuncs,
OverlayHandlerTypes,
} from '../types/event';
type BaseResponse = { rseq?: number; '$error'?: boolean };
declare global {
interface Window {
__OverlayCallback: EventMap[EventType];
dispatchOverlayEvent?: typeof processEvent;
OverlayPluginApi: {
ready: boolean;
callHandler: (msg: string, cb?: (value: string) => unknown) => void;
};
/**
* @deprecated This is for backward compatibility.
*
* It is recommended to import from this file:
*
* `import { addOverlayListener } from '/path/to/overlay_plugin_api';`
*/
addOverlayListener: IAddOverlayListener;
/**
* @deprecated This is for backward compatibility.
*
* It is recommended to import from this file:
*
* `import { removeOverlayListener } from '/path/to/overlay_plugin_api';`
*/
removeOverlayListener: IRemoveOverlayListener;
/**
* @deprecated This is for backward compatibility.
*
* It is recommended to import from this file:
*
* `import { callOverlayHandler } from '/path/to/overlay_plugin_api';`
*/
callOverlayHandler: IOverlayHandler;
}
}
type IAddOverlayListener = <T extends EventType>(event: T, cb: EventMap[T]) => void;
type IRemoveOverlayListener = <T extends EventType>(event: T, cb: EventMap[T]) => void;
type Subscriber<T> = {
[key in EventType]?: T[];
};
type EventParameter = Parameters<EventMap[EventType]>[0];
type VoidFunc<T> = (...args: T[]) => void;
let inited = false;
let wsUrl: string | null = null;
let ws: WebSocket | null = null;
let queue: (
| { [s: string]: unknown }
| [{ [s: string]: unknown }, ((value: string | null) => unknown) | undefined]
)[] | null = [];
let rseqCounter = 0;
type PromiseFuncs = {
resolve: (value: unknown) => void;
reject: (value: unknown) => void;
};
const responsePromises: { [rseqIdx: number]: PromiseFuncs } = {};
const subscribers: Subscriber<VoidFunc<unknown>> = {};
const sendMessage = (
msg: { [s: string]: unknown },
cb?: (value: string | null) => unknown,
): void => {
if (ws) {
if (queue)
queue.push(msg);
else
ws.send(JSON.stringify(msg));
} else {
if (queue)
queue.push([msg, cb]);
else
window.OverlayPluginApi.callHandler(JSON.stringify(msg), cb);
}
};
const processEvent = <T extends EventType>(msg: Parameters<EventMap[T]>[0]): void => {
init();
const subs = subscribers[msg.type];
subs?.forEach((sub) => {
try {
sub(msg);
} catch (e) {
console.error(e);
}
});
};
export const dispatchOverlayEvent = processEvent;
export const addOverlayListener: IAddOverlayListener = (event, cb): void => {
init();
if (!subscribers[event]) {
subscribers[event] = [];
if (!queue) {
sendMessage({
call: 'subscribe',
events: [event],
});
}
}
subscribers[event]?.push(cb as VoidFunc<unknown>);
};
export const removeOverlayListener: IRemoveOverlayListener = (event, cb): void => {
init();
if (subscribers[event]) {
const list = subscribers[event];
const pos = list?.indexOf(cb as VoidFunc<unknown>);
if (pos !== undefined && pos > -1)
list?.splice(pos, 1);
}
};
const callOverlayHandlerInternal: IOverlayHandler = (
_msg: { [s: string]: unknown },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> => {
init();
const msg = {
..._msg,
rseq: 0,
};
let p: Promise<unknown>;
if (ws) {
msg.rseq = rseqCounter++;
p = new Promise((resolve, reject) => {
responsePromises[msg.rseq] = { resolve: resolve, reject: reject };
});
sendMessage(msg);
} else {
p = new Promise((resolve, reject) => {
sendMessage(msg, (data) => {
if (data === null) {
resolve(data);
return;
}
const parsed = JSON.parse(data) as BaseResponse;
if (parsed['$error'])
reject(parsed);
else
resolve(parsed);
});
});
}
return p;
};
type OverrideMap = { [call in OverlayHandlerTypes]?: OverlayHandlerFuncs[call] };
const callOverlayHandlerOverrideMap: OverrideMap = {};
export const callOverlayHandler: IOverlayHandler = (
_msg: { [s: string]: unknown },
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> => {
init();
// If this `as` is incorrect, then it will not find an override.
// TODO: we could also replace this with a type guard.
const type = _msg.call as keyof OverrideMap;
const callFunc = callOverlayHandlerOverrideMap[type] ?? callOverlayHandlerInternal;
// The `IOverlayHandler` type guarantees that parameters/return type match
// one of the overlay handlers. The OverrideMap also only stores functions
// that match by the discriminating `call` field, and so any overrides
// should be correct here.
// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-argument
return callFunc(_msg as any);
};
export const setOverlayHandlerOverride = <T extends keyof OverlayHandlerFuncs>(
type: T,
override?: OverlayHandlerFuncs[T],
): void => {
if (!override) {
delete callOverlayHandlerOverrideMap[type];
return;
}
callOverlayHandlerOverrideMap[type] = override;
};
export const init = (): void => {
if (inited)
return;
if (typeof window !== 'undefined') {
wsUrl = new URLSearchParams(window.location.search).get('OVERLAY_WS');
if (wsUrl !== null) {
const connectWs = function(wsUrl: string) {
ws = new WebSocket(wsUrl);
ws.addEventListener('error', (e) => {
console.error(e);
});
ws.addEventListener('open', () => {
console.log('Connected!');
const q = queue ?? [];
queue = null;
sendMessage({
call: 'subscribe',
events: Object.keys(subscribers),
});
for (const msg of q) {
if (!Array.isArray(msg))
sendMessage(msg);
}
});
ws.addEventListener('message', (_msg) => {
try {
if (typeof _msg.data !== 'string') {
console.error('Invalid message data received: ', _msg);
return;
}
const msg = JSON.parse(_msg.data) as EventParameter & BaseResponse;
const promiseFuncs = msg?.rseq !== undefined ? responsePromises[msg.rseq] : undefined;
if (msg.rseq !== undefined && promiseFuncs) {
if (msg['$error'])
promiseFuncs.reject(msg);
else
promiseFuncs.resolve(msg);
delete responsePromises[msg.rseq];
} else {
processEvent(msg);
}
} catch (e) {
console.error('Invalid message received: ', _msg);
return;
}
});
ws.addEventListener('close', () => {
queue = null;
console.log('Trying to reconnect...');
// Don't spam the server with retries.
window.setTimeout(() => {
connectWs(wsUrl);
}, 300);
});
};
connectWs(wsUrl);
} else {
const waitForApi = function() {
if (!window.OverlayPluginApi?.ready) {
window.setTimeout(waitForApi, 300);
return;
}
const q = queue ?? [];
queue = null;
window.__OverlayCallback = processEvent;
sendMessage({
call: 'subscribe',
events: Object.keys(subscribers),
});
for (const item of q) {
if (Array.isArray(item))
sendMessage(item[0], item[1]);
}
};
waitForApi();
}
// Here the OverlayPlugin API is registered to the window object,
// but this is mainly for backwards compatibility. For cactbot's built-in files,
// it is recommended to use the various functions exported in resources/overlay_plugin_api.ts.
/* eslint-disable deprecation/deprecation */
window.addOverlayListener = addOverlayListener;
window.removeOverlayListener = removeOverlayListener;
window.callOverlayHandler = callOverlayHandler;
window.dispatchOverlayEvent = dispatchOverlayEvent;
/* eslint-enable deprecation/deprecation */
}
inited = true;
};