forked from orkestral/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.layer.ts
359 lines (335 loc) · 13.9 KB
/
listener.layer.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mMMMMMMMMMNNNmmNNNMMNMM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MN/ +NMMMN- /yyyyyys d. dMMMMNo` dNy-+ymmmho-+NN- dMMMMMMMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMNyNMMMMN+::::::::::m+/mMMMMMMd: dMMNho///+ymMMN+/mMMMMMMMMNs/hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMNsmMMMMMMMMMMMMMMNNNNMMNNNMMNNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
*/
import { EventEmitter } from 'events';
import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { ExposedFn } from '../helpers/exposed.enum';
import { Ack, Chat, LiveLocation, Message, ParticipantEvent } from '../model';
import { SocketState, SocketStream } from '../model/enum';
import { InterfaceMode } from '../model/enum/interface-mode';
import { InterfaceState } from '../model/enum/interface-state';
import { ProfileLayer } from './profile.layer';
import { callbackWile } from '../helpers';
declare global {
interface Window {
onMessage: any;
onAnyMessage: any;
onStateChange: any;
onIncomingCall: any;
onAck: any;
onStreamChange: any;
}
}
const callonMessage = new callbackWile();
const callOnack = new callbackWile();
export class ListenerLayer extends ProfileLayer {
private listenerEmitter = new EventEmitter();
constructor(public page: Page, session?: string, options?: CreateConfig) {
super(page, session, options);
this.page.on('load', async () => {
try {
await page.waitForSelector('canvas, #app .two, #startup', {
visible: true
});
} catch {}
await this._initialize(this.page);
await this.initialize();
});
this.page.on('close', () => {
this.cancelAutoClose();
this.spin('Page Closed', 'fail');
});
}
public async initialize() {
const functions = [
...Object.values(ExposedFn),
'onAddedToGroup',
'onIncomingCall'
];
for (const func of functions) {
const has = await this.page
.evaluate((func) => typeof window[func] === 'function', func)
.catch(() => false);
if (!has) {
await this.page
.exposeFunction(func, (...args) =>
this.listenerEmitter.emit(func, ...args)
)
.catch(() => {});
}
}
await this.page
.evaluate(() => {
if (!window['onAnyMessage'].exposed) {
window.WAPI.allNewMessagesListener(window['onAnyMessage']);
window['onAnyMessage'].exposed = true;
}
if (!window['onStateChange'].exposed) {
window.WAPI.onStateChange(window['onStateChange']);
window['onStateChange'].exposed = true;
}
if (!window['onStreamChange'].exposed) {
window.WAPI.onStreamChange(window['onStreamChange']);
window['onStreamChange'].exposed = true;
}
if (!window['onAddedToGroup'].exposed) {
window.WAPI.onAddedToGroup(window['onAddedToGroup']);
window['onAddedToGroup'].exposed = true;
}
if (!window['onIncomingCall'].exposed) {
window.WAPI.onIncomingCall(window['onIncomingCall']);
window['onIncomingCall'].exposed = true;
}
if (!window['onInterfaceChange'].exposed) {
window.WAPI.onInterfaceChange(window['onInterfaceChange']);
window['onInterfaceChange'].exposed = true;
}
if (!window['onMessage'].exposed) {
window.WAPI.onMessage(window['onMessage']);
window['onMessage'].exposed = true;
}
if (!window['onAck'].exposed) {
window.WAPI.onMessage(window['onAck']);
window['onAck'].exposed = true;
}
})
.catch(() => {});
}
/**
* @returns Returns the current state of the connection
*/
public async onStreamChange(fn: (state: SocketStream) => void) {
this.listenerEmitter.on(ExposedFn.onStreamChange, (state: SocketStream) => {
fn(state);
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onStreamChange, fn);
}
};
}
/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public async onMessage(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnMessage, (state: Message) => {
if (!callonMessage.checkObj(state.from, state.id)) {
callonMessage.addObjects(state.from, state.id);
fn(state);
}
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnMessage, (state: Message) => {
if (!callonMessage.checkObj(state.from, state.id)) {
callonMessage.addObjects(state.from, state.id);
fn(state);
}
});
}
};
}
/**
* @event Listens to all new messages
* @param to callback
* @fires Message
*/
public async onAnyMessage(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnAnyMessage, fn);
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnAnyMessage, fn);
}
};
}
/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public async onStateChange(fn: (state: SocketState) => void) {
this.listenerEmitter.on(ExposedFn.onStateChange, fn);
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onStateChange, fn);
}
};
}
/**
* @event Listens to interface mode change See {@link InterfaceState} and {@link InterfaceMode} for details
* @returns A disposable object to cancel the event
*/
public onInterfaceChange(
fn: (state: { displayInfo: InterfaceState; mode: InterfaceMode }) => void
) {
this.listenerEmitter.on(ExposedFn.onInterfaceChange, fn);
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onInterfaceChange, fn);
}
};
}
/**
* @event Listens to messages acknowledgement Changes
* @returns Observable stream of messages
*/
public async onAck(fn: (ack: Ack) => void) {
this.listenerEmitter.on(ExposedFn.onAck, (e: Ack) => {
if (!callOnack.checkObj(e.ack, e.id._serialized)) {
let key = callOnack.getObjKey(e.id._serialized);
if (key) {
callOnack.module[key].id = e.ack;
fn(e);
} else {
callOnack.addObjects(e.ack, e.id._serialized);
fn(e);
}
}
});
return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.onAck, (e: Ack) => {
if (!callOnack.checkObj(e.ack, e.id._serialized)) {
let key = callOnack.getObjKey(e.id._serialized);
if (key) {
callOnack.module[key].id = e.ack;
fn(e);
} else {
callOnack.addObjects(e.ack, e.id._serialized);
fn(e);
}
}
});
}
};
}
/**
* @event Listens to live locations from a chat that already has valid live locations
* @param chatId the chat from which you want to subscribes to live location updates
* @param fn callback that takes in a LiveLocation
* @returns boolean, if returns false then there were no valid live locations in the chat of chatId
* @emits <LiveLocation> LiveLocation
*/
public async onLiveLocation(
chatId: string,
fn: (liveLocationChangedEvent: LiveLocation) => void
) {
const method = 'onLiveLocation_' + chatId.replace('_', '').replace('_', '');
return this.page
.exposeFunction(method, (liveLocationChangedEvent: LiveLocation) =>
fn(liveLocationChangedEvent)
)
.then((_) =>
this.page.evaluate(
({ chatId, method }) => {
//@ts-ignore
return WAPI.onLiveLocation(chatId, window[method]);
},
{ chatId, method }
)
);
}
/**
* @event Listens to participants changed
* @param to group id: [email protected]
* @param to callback
* @returns Stream of ParticipantEvent
*/
public async onParticipantsChanged(
groupId: string,
fn: (participantChangedEvent: ParticipantEvent) => void
) {
const method =
'onParticipantsChanged_' + groupId.replace('_', '').replace('_', '');
return this.page
.exposeFunction(method, (participantChangedEvent: ParticipantEvent) =>
fn(participantChangedEvent)
)
.then((_) =>
this.page.evaluate(
({ groupId, method }) => {
//@ts-ignore
WAPI.onParticipantsChanged(groupId, window[method]);
},
{ groupId, method }
)
);
}
/**
* @event Fires callback with Chat object every time the host phone is added to a group.
* @param to callback
* @returns Observable stream of Chats
*/
public async onAddedToGroup(fn: (chat: Chat) => any) {
this.listenerEmitter.on('onAddedToGroup', fn);
return {
dispose: () => {
this.listenerEmitter.off('onAddedToGroup', fn);
}
};
}
/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public async onIncomingCall(fn: (call: any) => any) {
this.listenerEmitter.on('onIncomingCall', fn);
return {
dispose: () => {
this.listenerEmitter.off('onIncomingCall', fn);
}
};
}
}