-
Notifications
You must be signed in to change notification settings - Fork 0
/
pending-queue.ts
304 lines (284 loc) · 8.02 KB
/
pending-queue.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
import OneBlinkAppsError from './errors/oneBlinkAppsError'
import utilsService from './utils'
import Sentry from '../Sentry'
import {
FormSubmission,
PendingFormSubmission,
ProgressListener,
ProgressListenerEvent,
} from '../types/submissions'
function errorHandler(error: Error): Error {
Sentry.captureException(error)
console.error('Local Forage Error', error)
if (/The serialized value is too large/.test(error.message)) {
return new OneBlinkAppsError(
'It seems you have run out of space. To free up space, please connect to the internet to process pending submissions.',
{
originalError: error,
},
)
}
return error
}
export type PendingQueueAction =
| 'SUBMIT_STARTED'
| 'SUBMIT_FAILED'
| 'SUBMIT_SUCCEEDED'
| 'ADDITION'
| 'DELETION'
export type PendingQueueListener = (
results: PendingFormSubmission[],
action: PendingQueueAction,
) => unknown
const pendingQueueListeners: Array<PendingQueueListener> = []
/**
* Register a listener function that will be passed an array of
* PendingFormSubmissions when the pending queue is modified.
*
* ### Example
*
* ```js
* const listener = async (pendingSubmissions) => {
* // use pending submissions here...
* }
* const deregister =
* await submissionService.registerPendingQueueListener(listener)
*
* // When no longer needed, remember to deregister the listener
* deregister()
* ```
*
* @param listener
* @returns
*/
export function registerPendingQueueListener(
listener: PendingQueueListener,
): () => void {
pendingQueueListeners.push(listener)
return () => {
const index = pendingQueueListeners.indexOf(listener)
if (index !== -1) {
pendingQueueListeners.splice(index, 1)
}
}
}
function executePendingQueueListeners(
newSubmissions: PendingFormSubmission[],
action: PendingQueueAction,
) {
console.log(
'Pending Queue submissions have been updated',
action,
newSubmissions,
)
for (const pendingQueueListener of pendingQueueListeners) {
pendingQueueListener(newSubmissions, action)
}
}
export async function addFormSubmissionToPendingQueue(
formSubmission: FormSubmission,
) {
const pendingTimestamp = new Date().toISOString()
try {
await utilsService.setLocalForageItem(
`SUBMISSION_${pendingTimestamp}`,
formSubmission,
)
const submissions: PendingFormSubmission[] =
await getPendingQueueSubmissions()
submissions.push({
...formSubmission,
pendingTimestamp,
submission: undefined,
} as PendingFormSubmission)
await utilsService.localForage.setItem('submissions', submissions)
executePendingQueueListeners(submissions, 'ADDITION')
} catch (error) {
Sentry.captureException(error)
throw error instanceof Error ? errorHandler(error) : error
}
}
export async function updatePendingQueueSubmission(
pendingTimestamp: string,
newSubmission: PendingFormSubmission,
action: 'SUBMIT_FAILED' | 'SUBMIT_STARTED',
) {
try {
const submissions = await getPendingQueueSubmissions()
const newSubmissions = submissions.map((submission) => {
if (submission.pendingTimestamp === pendingTimestamp) {
return { ...newSubmission }
}
return submission
})
await utilsService.localForage.setItem('submissions', newSubmissions)
executePendingQueueListeners(newSubmissions, action)
} catch (error) {
Sentry.captureException(error)
throw error instanceof Error ? errorHandler(error) : error
}
}
/**
* Get an array of PendingFormSubmission
*
* ### Example
*
* ```js
* const pendingSubmission =
* await submissionService.getPendingQueueSubmissions()
* // Display pending submissions to user...
* ```
*
* @returns
*/
export function getPendingQueueSubmissions(): Promise<PendingFormSubmission[]> {
return utilsService.localForage
.getItem('submissions')
.then((submissions) => (Array.isArray(submissions) ? submissions : []))
}
export function getFormSubmission(
pendingTimestamp: string,
): Promise<FormSubmission | null> {
return utilsService.getLocalForageItem(`SUBMISSION_${pendingTimestamp}`)
}
/**
* Delete a PendingFormSubmission before it is processed based on the
* `pendingTimestamp` property.
*
* ### Example
*
* ```js
* const pendingTimestamp = '2020-07-29T01:03:26.573Z'
* await submissionService.deletePendingQueueSubmission(pendingTimestamp)
* ```
*
* @param pendingTimestamp
*/
export async function deletePendingQueueSubmission(pendingTimestamp: string) {
await removePendingQueueSubmission(pendingTimestamp, 'DELETION')
}
export async function removePendingQueueSubmission(
pendingTimestamp: string,
action: 'SUBMIT_SUCCEEDED' | 'DELETION',
) {
try {
await utilsService.removeLocalForageItem(`SUBMISSION_${pendingTimestamp}`)
const submissions = await getPendingQueueSubmissions()
const newSubmissions = submissions.filter(
(submission) => submission.pendingTimestamp !== pendingTimestamp,
)
await utilsService.localForage.setItem('submissions', newSubmissions)
executePendingQueueListeners(newSubmissions, action)
} catch (error) {
Sentry.captureException(error)
throw error instanceof Error ? errorHandler(error) : error
}
}
const pendingQueueAttachmentProgressListeners: Array<{
attachmentId: string
listener: ProgressListener
}> = []
/**
* Register a listener function that will be passed a progress event when an
* attachment for an item in the pending queue is being processed.
*
* ### Example
*
* ```js
* const listener = async ({ progress }) => {
* // update the UI to reflect the progress here...
* }
* const deregister =
* await submissionService.registerPendingQueueAttachmentProgressListener(
* attachment.id,
* listener,
* )
*
* // When no longer needed, remember to deregister the listener
* deregister()
* ```
*
* @param attachmentId
* @param listener
* @returns
*/
export function registerPendingQueueAttachmentProgressListener(
attachmentId: string,
listener: ProgressListener,
): () => void {
const item = { attachmentId, listener }
pendingQueueAttachmentProgressListeners.push(item)
return () => {
const index = pendingQueueAttachmentProgressListeners.indexOf(item)
if (index !== -1) {
pendingQueueAttachmentProgressListeners.splice(index, 1)
}
}
}
export function executePendingQueueAttachmentProgressListeners(
event: ProgressListenerEvent & {
attachmentId: string
},
) {
for (const pendingQueueAttachmentProgressListener of pendingQueueAttachmentProgressListeners) {
if (
event.attachmentId === pendingQueueAttachmentProgressListener.attachmentId
) {
pendingQueueAttachmentProgressListener.listener(event)
}
}
}
const pendingQueueProgressListeners: Array<{
pendingTimestamp: string
listener: ProgressListener
}> = []
/**
* Register a listener function that will be passed a progress event when an
* item in the pending queue is being processed.
*
* ### Example
*
* ```js
* const listener = async ({ progress }) => {
* // update the UI to reflect the progress here...
* }
* const deregister =
* await submissionService.registerPendingQueueProgressListener(
* pendingQueueItem.pendingTimestamp,
* listener,
* )
*
* // When no longer needed, remember to deregister the listener
* deregister()
* ```
*
* @param pendingTimestamp
* @param listener
* @returns
*/
export function registerPendingQueueProgressListener(
pendingTimestamp: string,
listener: ProgressListener,
): () => void {
const item = { pendingTimestamp, listener }
pendingQueueProgressListeners.push(item)
return () => {
const index = pendingQueueProgressListeners.indexOf(item)
if (index !== -1) {
pendingQueueProgressListeners.splice(index, 1)
}
}
}
export function executePendingQueueProgressListeners(
event: ProgressListenerEvent & {
pendingTimestamp: string
},
) {
for (const pendingQueueProgressListener of pendingQueueProgressListeners) {
if (
event.pendingTimestamp === pendingQueueProgressListener.pendingTimestamp
) {
pendingQueueProgressListener.listener(event)
}
}
}