-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqueue-manager.ts
More file actions
298 lines (270 loc) · 9.42 KB
/
queue-manager.ts
File metadata and controls
298 lines (270 loc) · 9.42 KB
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
import Gist from '../gist';
import { log } from '../utilities/log';
import { getUserToken, isAnonymousUser } from './user-manager';
import {
getUserQueue,
getQueueSSEEndpoint,
userQueueNextPullCheckLocalStoreName,
} from '../services/queue-service';
import { showMessage, embedMessage, resetMessage } from './message-manager';
import { resolveMessageProperties } from './gist-properties-manager';
import { clearKeyFromLocalStore, getKeyFromLocalStore } from '../utilities/local-storage';
import {
updateBroadcastsLocalStore,
getEligibleBroadcasts,
isShowAlwaysBroadcast,
} from './message-broadcast-manager';
import {
updateQueueLocalStore,
getMessagesFromLocalStore,
isMessageLoading,
setMessageLoading,
getSavedMessageState,
} from './message-user-queue-manager';
import { updateInboxMessagesLocalStore } from './inbox-message-manager';
import type { InboxMessage } from './inbox-message-manager';
import { settings } from '../services/settings';
import { applyDisplaySettings } from '../utilities/message-utils';
import { findElement } from '../utilities/dom';
import type { GistMessage, DisplaySettings } from '../types';
const sleep = (time: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, time));
const poll = (promiseFn: () => Promise<unknown>, time: number): Promise<unknown> =>
promiseFn().then(
sleep(time).then(() => poll(promiseFn, time)) as unknown as (value: unknown) => unknown
);
let pollingSetup = false;
let sseSource: EventSource | null = null;
export async function startQueueListener(): Promise<void> {
if (!pollingSetup) {
if (getUserToken()) {
log('Queue watcher started');
pollingSetup = true;
poll(
() =>
new Promise(() => {
void pullMessagesFromQueue();
}),
1000
);
} else {
log('User token not setup, queue not started.');
}
} else {
await checkMessageQueue();
}
}
export async function checkMessageQueue(): Promise<void> {
const broadcastMessages = await getEligibleBroadcasts();
const userMessages = await getMessagesFromLocalStore();
const allMessages = broadcastMessages.concat(userMessages);
log(`Messages in local queue: ${allMessages.length}`);
const orderedMessages = allMessages.sort(
(a, b) =>
(a as GistMessage & { priority: number }).priority -
(b as GistMessage & { priority: number }).priority
);
for (const message of orderedMessages) {
await handleMessage(message);
}
}
export async function checkCurrentMessagesAfterRouteChange(): Promise<void> {
if (Gist.currentMessages.length === 0) {
return;
}
for (const message of [...Gist.currentMessages]) {
if (document.querySelector(`#gist-${message.instanceId}`) == null) {
log(`Removing active message ${message.instanceId} that no longer exists after route change`);
await resetMessage(message);
}
}
}
// TODO: Move this to a utility and only return valid messages (from: getEligibleBroadcasts getMessagesFromLocalStore) & to handleMessage
export async function handleMessage(message: GistMessage): Promise<boolean> {
let messageProperties = resolveMessageProperties(message);
if (messageProperties.hasRouteRule) {
let currentUrl = Gist.currentRoute;
if (currentUrl == null) {
currentUrl = new URL(window.location.href).pathname;
}
const routeRule = messageProperties.routeRule;
log(`Verifying route ${currentUrl} against rule: ${routeRule}`);
const urlTester = new RegExp(routeRule);
if (!urlTester.test(currentUrl)) {
log(`Route ${currentUrl} does not match rule.`);
return false;
}
}
if (messageProperties.hasPosition) {
message.position = messageProperties.position;
}
if (messageProperties.hasTooltipPosition) {
message.tooltipPosition = messageProperties.tooltipPosition;
}
if (messageProperties.persistent || isShowAlwaysBroadcast(message)) {
const savedState = (await getSavedMessageState(message.queueId ?? '')) as {
stepName?: string;
displaySettings?: DisplaySettings;
} | null;
if (savedState) {
log(`Restoring saved state for queueId ${message.queueId}`);
if (savedState.displaySettings) {
applyDisplaySettings(message, savedState.displaySettings);
messageProperties = resolveMessageProperties(message);
}
message.savedStepName = savedState.stepName ?? null;
}
}
if (
!messageProperties.persistent &&
!isShowAlwaysBroadcast(message) &&
(await isMessageLoading(message.queueId ?? ''))
) {
log(`Not showing message with queueId ${message.queueId} because its already loading.`);
return false;
} else {
let result: GistMessage | null = null;
if (messageProperties.isEmbedded) {
const isLivePreview = Gist.config.isPreviewSession && message.properties?.gist?.livePreview;
if (isLivePreview && !findElement(messageProperties.elementId)) {
log(
`Preview: element "${messageProperties.elementId}" not found, showing as overlay so placement can be changed`
);
result = await showMessage(message);
} else {
result = embedMessage(message, messageProperties.elementId);
}
} else {
result = await showMessage(message);
}
if (result) setMessageLoading(message.queueId ?? '');
return result !== null;
}
}
export async function pullMessagesFromQueue(): Promise<void> {
if (settings.hasActiveSSEConnection()) {
if (!settings.isSSEConnectionManagedBySDK() && sseSource) {
log('Not the main instance, closing our SSE connection.');
stopSSEListener();
}
await checkMessageQueue();
return;
} else {
if (sseSource) {
log('SSE connection not active, closing it.');
stopSSEListener();
}
}
if (settings.useSSE() && !isAnonymousUser()) {
await setupSSEQueueListener();
return;
}
await checkQueueThroughPolling();
}
async function checkQueueThroughPolling(): Promise<void> {
if (getUserToken()) {
if (Gist.isDocumentVisible) {
if (getKeyFromLocalStore(userQueueNextPullCheckLocalStoreName) === null) {
const response = await getUserQueue();
if (response) {
if (response.status === 200 || response.status === 204) {
log('200 response, updating local store.');
const data = response.data as
| {
inAppMessages?: GistMessage[];
inboxMessages?: InboxMessage[];
}
| undefined;
const inAppMessages = data?.inAppMessages ?? [];
const inboxMessages = data?.inboxMessages ?? [];
updateQueueLocalStore(inAppMessages);
updateBroadcastsLocalStore(inAppMessages);
updateInboxMessagesLocalStore(inboxMessages);
} else if (response.status === 304) {
log('304 response, using local store.');
}
await checkMessageQueue();
} else {
log(`No response object returned while checking message queue.`);
}
} else {
log(`Next queue pull scheduled for later.`);
}
} else {
log(`Document not visible, skipping queue check.`);
}
} else {
log(`User token reset, skipping queue check.`);
}
}
async function setupSSEQueueListener(): Promise<void> {
stopSSEListener();
const sseURL = getQueueSSEEndpoint();
if (sseURL === null) {
log('SSE endpoint not available, falling back to polling.');
await checkQueueThroughPolling();
return;
}
log(`Starting SSE queue listener on ${sseURL}`);
sseSource = new EventSource(sseURL);
settings.setActiveSSEConnection();
sseSource.addEventListener('connected', async (event) => {
try {
log('SSE connection received');
settings.setUseSSEFlag(true);
const config = JSON.parse(event.data);
if (config.heartbeat) {
settings.setSSEHeartbeat(config.heartbeat);
log(`SSE heartbeat set to ${config.heartbeat} seconds`);
}
settings.setActiveSSEConnection();
} catch (e) {
log(`Failed to parse SSE settings: ${e}`);
}
clearKeyFromLocalStore(userQueueNextPullCheckLocalStoreName);
await checkQueueThroughPolling();
});
sseSource.addEventListener('messages', async (event) => {
try {
const messages = JSON.parse(event.data);
log('SSE message received');
await updateQueueLocalStore(messages);
await updateBroadcastsLocalStore(messages);
await checkMessageQueue();
} catch (e) {
log(`Failed to parse SSE message: ${e}`);
stopSSEListener();
}
});
sseSource.addEventListener('inbox_messages', async (event) => {
try {
const inboxMessages = JSON.parse(event.data);
log('SSE inbox messages received');
await updateInboxMessagesLocalStore(inboxMessages);
} catch (e) {
log(`Failed to parse SSE inbox messages: ${e}`);
}
});
sseSource.addEventListener('error', async () => {
log('SSE error received');
stopSSEListener();
});
sseSource.addEventListener('heartbeat', async () => {
log('SSE heartbeat received');
settings.setActiveSSEConnection();
settings.setUseSSEFlag(true);
});
}
export function stopSSEListener(disconnectGlobally = false): void {
if (disconnectGlobally) {
settings.removeActiveSSEConnection();
}
if (disconnectGlobally || settings.isSSEConnectionManagedBySDK()) {
settings.setUseSSEFlag(false);
}
if (!sseSource) {
return;
}
log('Stopping SSE queue listener...');
sseSource.close();
sseSource = null;
}