@@ -25,6 +25,11 @@ type QueueObject = {
25
25
26
26
type Queue = Array < QueueObject > ;
27
27
28
+ const QUEUE_KEY = 'Parse/Eventually/Queue' ;
29
+ let queueCache = [ ] ;
30
+ let dirtyCache = true ;
31
+ let polling = undefined ;
32
+
28
33
/**
29
34
* Provides utility functions to queue objects that will be
30
35
* saved to the server at a later date.
@@ -33,9 +38,6 @@ type Queue = Array<QueueObject>;
33
38
* @static
34
39
*/
35
40
const EventuallyQueue = {
36
- localStorageKey : 'Parse/Eventually/Queue' ,
37
- polling : undefined ,
38
-
39
41
/**
40
42
* Add object to queue with save operation.
41
43
*
@@ -103,9 +105,9 @@ const EventuallyQueue = {
103
105
let index = this . queueItemExists ( queueData , queueId ) ;
104
106
if ( index > - 1 ) {
105
107
// Add cached values to new object if they don't exist
106
- for ( const prop in queueData [ index ] . object . attributes ) {
108
+ for ( const prop in queueData [ index ] . object ) {
107
109
if ( typeof object . get ( prop ) === 'undefined' ) {
108
- object . set ( prop , queueData [ index ] . object . attributes [ prop ] ) ;
110
+ object . set ( prop , queueData [ index ] . object [ prop ] ) ;
109
111
}
110
112
}
111
113
} else {
@@ -114,7 +116,7 @@ const EventuallyQueue = {
114
116
queueData [ index ] = {
115
117
queueId,
116
118
action,
117
- object,
119
+ object : object . toJSON ( ) ,
118
120
serverOptions,
119
121
id : object . id ,
120
122
className : object . className ,
@@ -124,20 +126,28 @@ const EventuallyQueue = {
124
126
return this . setQueue ( queueData ) ;
125
127
} ,
126
128
129
+ store ( data ) {
130
+ return Storage . setItemAsync ( QUEUE_KEY , JSON . stringify ( data ) ) ;
131
+ } ,
132
+
133
+ load ( ) {
134
+ return Storage . getItemAsync ( QUEUE_KEY ) ;
135
+ } ,
136
+
127
137
/**
128
- * Returns the queue from local storage
138
+ * Sets the in-memory queue from local storage and returns.
129
139
*
130
140
* @function getQueue
131
141
* @name Parse.EventuallyQueue.getQueue
132
142
* @returns {Promise<Array> }
133
143
* @static
134
144
*/
135
145
async getQueue ( ) : Promise < Array > {
136
- const q = await Storage . getItemAsync ( this . localStorageKey ) ;
137
- if ( ! q ) {
138
- return [ ] ;
146
+ if ( dirtyCache ) {
147
+ queueCache = JSON . parse ( ( await this . load ( ) ) || '[]' ) ;
148
+ dirtyCache = false ;
139
149
}
140
- return JSON . parse ( q ) ;
150
+ return queueCache ;
141
151
} ,
142
152
143
153
/**
@@ -149,7 +159,8 @@ const EventuallyQueue = {
149
159
* @ignore
150
160
*/
151
161
setQueue ( queue : Queue ) : Promise < void > {
152
- return Storage . setItemAsync ( this . localStorageKey , JSON . stringify ( queue ) ) ;
162
+ queueCache = queue ;
163
+ return this . store ( queueCache ) ;
153
164
} ,
154
165
155
166
/**
@@ -165,7 +176,6 @@ const EventuallyQueue = {
165
176
const index = this . queueItemExists ( queueData , queueId ) ;
166
177
if ( index > - 1 ) {
167
178
queueData . splice ( index , 1 ) ;
168
- await this . setQueue ( queueData ) ;
169
179
}
170
180
} ,
171
181
@@ -178,7 +188,8 @@ const EventuallyQueue = {
178
188
* @static
179
189
*/
180
190
clear ( ) : Promise {
181
- return Storage . setItemAsync ( this . localStorageKey , JSON . stringify ( [ ] ) ) ;
191
+ queueCache = [ ] ;
192
+ return this . store ( [ ] ) ;
182
193
} ,
183
194
184
195
/**
@@ -216,18 +227,22 @@ const EventuallyQueue = {
216
227
* @static
217
228
*/
218
229
async sendQueue ( ) : Promise < boolean > {
219
- const queueData = await this . getQueue ( ) ;
230
+ const queue = await this . getQueue ( ) ;
231
+ const queueData = [ ...queue ] ;
232
+
220
233
if ( queueData . length === 0 ) {
221
234
return false ;
222
235
}
223
236
for ( let i = 0 ; i < queueData . length ; i += 1 ) {
224
- const ObjectType = ParseObject . extend ( queueData [ i ] . className ) ;
225
- if ( queueData [ i ] . id ) {
226
- await this . reprocess . byId ( ObjectType , queueData [ i ] ) ;
227
- } else if ( queueData [ i ] . hash ) {
228
- await this . reprocess . byHash ( ObjectType , queueData [ i ] ) ;
237
+ const queueObject = queueData [ i ] ;
238
+ const { id, hash, className } = queueObject ;
239
+ const ObjectType = ParseObject . extend ( className ) ;
240
+ if ( id ) {
241
+ await this . process . byId ( ObjectType , queueObject ) ;
242
+ } else if ( hash ) {
243
+ await this . process . byHash ( ObjectType , queueObject ) ;
229
244
} else {
230
- await this . reprocess . create ( ObjectType , queueData [ i ] ) ;
245
+ await this . process . create ( ObjectType , queueObject ) ;
231
246
}
232
247
}
233
248
return true ;
@@ -283,21 +298,22 @@ const EventuallyQueue = {
283
298
*
284
299
* @function poll
285
300
* @name Parse.EventuallyQueue.poll
301
+ * @param [ms] Milliseconds to ping the server. Default 2000ms
286
302
* @static
287
303
*/
288
- poll ( ) {
289
- if ( this . polling ) {
304
+ poll ( ms : number = 2000 ) {
305
+ if ( polling ) {
290
306
return ;
291
307
}
292
- this . polling = setInterval ( ( ) => {
308
+ polling = setInterval ( ( ) => {
293
309
const RESTController = CoreManager . getRESTController ( ) ;
294
310
RESTController . ajax ( 'GET' , CoreManager . get ( 'SERVER_URL' ) ) . catch ( error => {
295
311
if ( error !== 'Unable to connect to the Parse API' ) {
296
312
this . stopPoll ( ) ;
297
313
return this . sendQueue ( ) ;
298
314
}
299
315
} ) ;
300
- } , 2000 ) ;
316
+ } , ms ) ;
301
317
} ,
302
318
303
319
/**
@@ -308,14 +324,30 @@ const EventuallyQueue = {
308
324
* @static
309
325
*/
310
326
stopPoll ( ) {
311
- clearInterval ( this . polling ) ;
312
- this . polling = undefined ;
327
+ clearInterval ( polling ) ;
328
+ polling = undefined ;
329
+ } ,
330
+
331
+ /**
332
+ * Return true if pinging the server.
333
+ *
334
+ * @function isPolling
335
+ * @name Parse.EventuallyQueue.isPolling
336
+ * @returns {boolean }
337
+ * @static
338
+ */
339
+ isPolling ( ) : boolean {
340
+ return ! ! polling ;
341
+ } ,
342
+
343
+ _setPolling ( flag : boolean ) {
344
+ polling = flag ;
313
345
} ,
314
346
315
- reprocess : {
347
+ process : {
316
348
create ( ObjectType , queueObject ) {
317
- const newObject = new ObjectType ( ) ;
318
- return EventuallyQueue . sendQueueCallback ( newObject , queueObject ) ;
349
+ const object = new ObjectType ( ) ;
350
+ return EventuallyQueue . sendQueueCallback ( object , queueObject ) ;
319
351
} ,
320
352
async byId ( ObjectType , queueObject ) {
321
353
const { sessionToken } = queueObject . serverOptions ;
@@ -332,7 +364,7 @@ const EventuallyQueue = {
332
364
if ( results . length > 0 ) {
333
365
return EventuallyQueue . sendQueueCallback ( results [ 0 ] , queueObject ) ;
334
366
}
335
- return EventuallyQueue . reprocess . create ( ObjectType , queueObject ) ;
367
+ return EventuallyQueue . process . create ( ObjectType , queueObject ) ;
336
368
} ,
337
369
} ,
338
370
} ;
0 commit comments