-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathlistener.layer.ts
139 lines (130 loc) Β· 4.14 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
import { Page } from 'puppeteer';
import { ExposedFn } from '../helpers/exposed.enum';
import { Ack, Chat, LiveLocation, Message, ParticipantEvent } from '../model';
import { SocketState } from '../model/enum';
import { ProfileLayer } from './profile.layer';
declare module WAPI {
const waitNewMessages: (rmCallback: boolean, callback: Function) => void;
const allNewMessagesListener: (callback: Function) => void;
const onStateChange: (callback: Function) => void;
const onAddedToGroup: (callback: Function) => any;
const onParticipantsChanged: (groupId: string, callback: Function) => any;
const onLiveLocation: (chatId: string, callback: Function) => any;
}
export class ListenerLayer extends ProfileLayer {
constructor(public page: Page) {
super(page);
}
/**
* Listens to messages received
* @returns Observable stream of messages
*/
public onMessage(fn: (message: Message) => void) {
this.page.exposeFunction(ExposedFn.OnMessage, (message: Message) =>
fn(message)
);
}
/**
* @event Listens to all new messages
* @param to callback
* @fires Message
*/
public async onAnyMessage(fn: (message: Message) => void) {
this.page
.exposeFunction(ExposedFn.OnAnyMessage, (message: Message) => fn(message))
.then((_) =>
this.page.evaluate(() => {
WAPI.allNewMessagesListener(window['onAnyMessage']);
})
);
}
/**
* @event Listens to messages received
* @returns Observable stream of messages
*/
public onStateChange(fn: (state: SocketState) => void) {
this.page
.exposeFunction(ExposedFn.onStateChange, (state: SocketState) =>
fn(state)
)
.then(() =>
this.page.evaluate(() => {
WAPI.onStateChange((_) => window['onStateChange'](_.state));
})
);
}
/**
* @event Listens to messages acknowledgement Changes
* @returns Observable stream of messages
*/
public onAck(fn: (ack: Ack) => void) {
this.page.exposeFunction(ExposedFn.onAck, (ack: Ack) => fn(ack));
}
/**
* @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 }
)
);
}
/**
* @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) {
const method = 'onAddedToGroup';
return this.page
.exposeFunction(method, (chat: any) => fn(chat))
.then((_) =>
this.page.evaluate(() => {
//@ts-ignore
WAPI.onAddedToGroup(window.onAddedToGroup);
})
);
}
}