@@ -9,24 +9,94 @@ import ParseObject from './ParseObject';
9
9
import ParseQuery from './ParseQuery' ;
10
10
import Storage from './Storage' ;
11
11
12
+ import type { SaveOptions } from './ParseObject' ;
13
+ import type { RequestOptions } from './RESTController' ;
14
+
15
+ type QueueObject = {
16
+ queueId : string ,
17
+ action : string ,
18
+ object : ParseObject ,
19
+ serverOptions : SaveOptions | RequestOptions ,
20
+ id : string ,
21
+ className : string ,
22
+ hash : string ,
23
+ createdAt : Date ,
24
+ } ;
25
+
26
+ type Queue = Array < QueueObject > ;
27
+
28
+ /**
29
+ * Provides utility functions to queue objects that will be
30
+ * saved to the server at a later date.
31
+ *
32
+ * @class Parse.EventuallyQueue
33
+ * @static
34
+ */
12
35
const EventuallyQueue = {
13
- localStorageKey : 'Parse. Eventually. Queue' ,
36
+ localStorageKey : 'Parse/ Eventually/ Queue' ,
14
37
polling : undefined ,
15
38
16
- save ( object , serverOptions = { } ) {
39
+ /**
40
+ * Add object to queue with save operation.
41
+ *
42
+ * @function save
43
+ * @name Parse.EventuallyQueue.save
44
+ * @param {ParseObject } object Parse.Object to be saved eventually
45
+ * @param {object } [serverOptions] See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Object.html#save Parse.Object.save} options.
46
+ * @returns {Promise } A promise that is fulfilled if object is added to queue.
47
+ * @static
48
+ * @see Parse.Object#saveEventually
49
+ */
50
+ save ( object : ParseObject , serverOptions : SaveOptions = { } ) : Promise {
17
51
return this . enqueue ( 'save' , object, serverOptions) ;
18
52
} ,
19
53
20
- destroy ( object , serverOptions = { } ) {
54
+ /**
55
+ * Add object to queue with save operation.
56
+ *
57
+ * @function destroy
58
+ * @name Parse.EventuallyQueue.destroy
59
+ * @param {ParseObject } object Parse.Object to be destroyed eventually
60
+ * @param {object } [serverOptions] See {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Object.html#destroy Parse.Object.destroy} options
61
+ * @returns {Promise } A promise that is fulfilled if object is added to queue.
62
+ * @static
63
+ * @see Parse.Object#destroyEventually
64
+ */
65
+ destroy ( object : ParseObject , serverOptions : RequestOptions = { } ) : Promise {
21
66
return this . enqueue ( 'destroy' , object, serverOptions) ;
22
67
} ,
23
- generateQueueId ( action , object ) {
68
+
69
+ /**
70
+ * Generate unique identifier to avoid duplicates and maintain previous state.
71
+ *
72
+ * @param {string } action save / destroy
73
+ * @param {object } object Parse.Object to be queued
74
+ * @returns {string }
75
+ * @static
76
+ * @ignore
77
+ */
78
+ generateQueueId ( action : string , object : ParseObject ) : string {
24
79
object . _getId ( ) ;
25
80
const { className, id, _localId } = object ;
26
81
const uniqueId = object . get ( 'hash' ) || _localId ;
27
82
return [ action , className , id , uniqueId ] . join ( '_' ) ;
28
83
} ,
29
- async enqueue ( action , object , serverOptions ) {
84
+
85
+ /**
86
+ * Build queue object and add to queue.
87
+ *
88
+ * @param {string } action save / destroy
89
+ * @param {object } object Parse.Object to be queued
90
+ * @param {object } [serverOptions]
91
+ * @returns {Promise } A promise that is fulfilled if object is added to queue.
92
+ * @static
93
+ * @ignore
94
+ */
95
+ async enqueue (
96
+ action: string ,
97
+ object : ParseObject ,
98
+ serverOptions : SaveOptions | RequestOptions
99
+ ) : Promise {
30
100
const queueData = await this . getQueue ( ) ;
31
101
const queueId = this . generateQueueId ( action , object ) ;
32
102
@@ -54,19 +124,43 @@ const EventuallyQueue = {
54
124
return this . setQueue ( queueData ) ;
55
125
} ,
56
126
57
- async getQueue ( ) {
127
+ /**
128
+ * Returns the queue from local storage
129
+ *
130
+ * @function getQueue
131
+ * @name Parse.EventuallyQueue.getQueue
132
+ * @returns {Promise<Array> }
133
+ * @static
134
+ */
135
+ async getQueue ( ) : Promise < Array > {
58
136
const q = await Storage . getItemAsync ( this . localStorageKey ) ;
59
137
if ( ! q ) {
60
138
return [ ] ;
61
139
}
62
140
return JSON . parse ( q ) ;
63
141
} ,
64
142
65
- setQueue ( queueData ) {
66
- return Storage . setItemAsync ( this . localStorageKey , JSON . stringify ( queueData ) ) ;
143
+ /**
144
+ * Saves the queue to local storage
145
+ *
146
+ * @param {Queue } queue Queue containing Parse.Object data.
147
+ * @returns {Promise } A promise that is fulfilled when queue is stored.
148
+ * @static
149
+ * @ignore
150
+ */
151
+ setQueue ( queue : Queue ) : Promise < void > {
152
+ return Storage . setItemAsync ( this . localStorageKey , JSON . stringify ( queue ) ) ;
67
153
} ,
68
154
69
- async remove ( queueId ) {
155
+ /**
156
+ * Removes Parse.Object data from queue.
157
+ *
158
+ * @param {string } queueId Unique identifier for Parse.Object data.
159
+ * @returns {Promise } A promise that is fulfilled when queue is stored.
160
+ * @static
161
+ * @ignore
162
+ */
163
+ async remove ( queueId : string ) : Promise < void > {
70
164
const queueData = await this . getQueue ( ) ;
71
165
const index = this . queueItemExists ( queueData , queueId ) ;
72
166
if ( index > - 1 ) {
@@ -75,20 +169,53 @@ const EventuallyQueue = {
75
169
}
76
170
} ,
77
171
78
- clear ( ) {
172
+ /**
173
+ * Removes all objects from queue.
174
+ *
175
+ * @function clear
176
+ * @name Parse.EventuallyQueue.clear
177
+ * @returns {Promise } A promise that is fulfilled when queue is cleared.
178
+ * @static
179
+ */
180
+ clear ( ) : Promise {
79
181
return Storage . setItemAsync ( this . localStorageKey , JSON . stringify ( [ ] ) ) ;
80
182
} ,
81
183
82
- queueItemExists ( queueData , queueId ) {
83
- return queueData . findIndex ( data => data . queueId === queueId ) ;
184
+ /**
185
+ * Return the index of a queueId in the queue. Returns -1 if not found.
186
+ *
187
+ * @param {Queue } queue Queue containing Parse.Object data.
188
+ * @param {string } queueId Unique identifier for Parse.Object data.
189
+ * @returns {number }
190
+ * @static
191
+ * @ignore
192
+ */
193
+ queueItemExists ( queue : Queue , queueId : string ) : number {
194
+ return queue . findIndex ( data => data . queueId === queueId ) ;
84
195
} ,
85
196
86
- async length ( ) {
197
+ /**
198
+ * Return the number of objects in the queue.
199
+ *
200
+ * @function length
201
+ * @name Parse.EventuallyQueue.length
202
+ * @returns {number }
203
+ * @static
204
+ */
205
+ async length ( ) : number {
87
206
const queueData = await this . getQueue ( ) ;
88
207
return queueData . length ;
89
208
} ,
90
209
91
- async sendQueue ( ) {
210
+ /**
211
+ * Sends the queue to the server.
212
+ *
213
+ * @function sendQueue
214
+ * @name Parse.EventuallyQueue.sendQueue
215
+ * @returns {Promise<boolean> } Returns true if queue was sent successfully.
216
+ * @static
217
+ */
218
+ async sendQueue ( ) : Promise < boolean > {
92
219
const queueData = await this . getQueue ( ) ;
93
220
if ( queueData . length === 0 ) {
94
221
return false ;
@@ -106,7 +233,16 @@ const EventuallyQueue = {
106
233
return true ;
107
234
} ,
108
235
109
- async sendQueueCallback ( object , queueObject ) {
236
+ /**
237
+ * Build queue object and add to queue.
238
+ *
239
+ * @param {ParseObject } object Parse.Object to be processed
240
+ * @param {QueueObject } queueObject Parse.Object data from the queue
241
+ * @returns {Promise } A promise that is fulfilled when operation is performed.
242
+ * @static
243
+ * @ignore
244
+ */
245
+ async sendQueueCallback ( object : ParseObject , queueObject : QueueObject ) : Promise < void > {
110
246
if ( ! object ) {
111
247
return this . remove ( queueObject . queueId ) ;
112
248
}
@@ -141,6 +277,14 @@ const EventuallyQueue = {
141
277
}
142
278
} ,
143
279
280
+ /**
281
+ * Start polling server for network connection.
282
+ * Will send queue if connection is established.
283
+ *
284
+ * @function poll
285
+ * @name Parse.EventuallyQueue.poll
286
+ * @static
287
+ */
144
288
poll ( ) {
145
289
if ( this . polling ) {
146
290
return ;
@@ -149,17 +293,25 @@ const EventuallyQueue = {
149
293
const RESTController = CoreManager . getRESTController ( ) ;
150
294
RESTController . ajax ( 'GET' , CoreManager . get ( 'SERVER_URL' ) ) . catch ( error => {
151
295
if ( error !== 'Unable to connect to the Parse API' ) {
152
- clearInterval ( this . polling ) ;
153
- this . polling = undefined ;
296
+ this . stopPoll ( ) ;
154
297
return this . sendQueue ( ) ;
155
298
}
156
299
} ) ;
157
300
} , 2000 ) ;
158
301
} ,
302
+
303
+ /**
304
+ * Turns off polling.
305
+ *
306
+ * @function stopPoll
307
+ * @name Parse.EventuallyQueue.stopPoll
308
+ * @static
309
+ */
159
310
stopPoll ( ) {
160
311
clearInterval ( this . polling ) ;
161
312
this . polling = undefined ;
162
313
} ,
314
+
163
315
reprocess : {
164
316
create ( ObjectType , queueObject ) {
165
317
const newObject = new ObjectType ( ) ;
0 commit comments