Skip to content

Commit 820751b

Browse files
committed
Documentation
1 parent 28999b9 commit 820751b

File tree

2 files changed

+170
-18
lines changed

2 files changed

+170
-18
lines changed

src/EventuallyQueue.js

Lines changed: 169 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,94 @@ import ParseObject from './ParseObject';
99
import ParseQuery from './ParseQuery';
1010
import Storage from './Storage';
1111

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+
*/
1235
const EventuallyQueue = {
13-
localStorageKey: 'Parse.Eventually.Queue',
36+
localStorageKey: 'Parse/Eventually/Queue',
1437
polling: undefined,
1538

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 {
1751
return this.enqueue('save', object, serverOptions);
1852
},
1953

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 {
2166
return this.enqueue('destroy', object, serverOptions);
2267
},
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 {
2479
object._getId();
2580
const { className, id, _localId } = object;
2681
const uniqueId = object.get('hash') || _localId;
2782
return [action, className, id, uniqueId].join('_');
2883
},
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 {
30100
const queueData = await this.getQueue();
31101
const queueId = this.generateQueueId(action, object);
32102

@@ -54,19 +124,43 @@ const EventuallyQueue = {
54124
return this.setQueue(queueData);
55125
},
56126

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> {
58136
const q = await Storage.getItemAsync(this.localStorageKey);
59137
if (!q) {
60138
return [];
61139
}
62140
return JSON.parse(q);
63141
},
64142

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));
67153
},
68154

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> {
70164
const queueData = await this.getQueue();
71165
const index = this.queueItemExists(queueData, queueId);
72166
if (index > -1) {
@@ -75,20 +169,53 @@ const EventuallyQueue = {
75169
}
76170
},
77171

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 {
79181
return Storage.setItemAsync(this.localStorageKey, JSON.stringify([]));
80182
},
81183

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);
84195
},
85196

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 {
87206
const queueData = await this.getQueue();
88207
return queueData.length;
89208
},
90209

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> {
92219
const queueData = await this.getQueue();
93220
if (queueData.length === 0) {
94221
return false;
@@ -106,7 +233,16 @@ const EventuallyQueue = {
106233
return true;
107234
},
108235

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> {
110246
if (!object) {
111247
return this.remove(queueObject.queueId);
112248
}
@@ -141,6 +277,14 @@ const EventuallyQueue = {
141277
}
142278
},
143279

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+
*/
144288
poll() {
145289
if (this.polling) {
146290
return;
@@ -149,17 +293,25 @@ const EventuallyQueue = {
149293
const RESTController = CoreManager.getRESTController();
150294
RESTController.ajax('GET', CoreManager.get('SERVER_URL')).catch(error => {
151295
if (error !== 'Unable to connect to the Parse API') {
152-
clearInterval(this.polling);
153-
this.polling = undefined;
296+
this.stopPoll();
154297
return this.sendQueue();
155298
}
156299
});
157300
}, 2000);
158301
},
302+
303+
/**
304+
* Turns off polling.
305+
*
306+
* @function stopPoll
307+
* @name Parse.EventuallyQueue.stopPoll
308+
* @static
309+
*/
159310
stopPoll() {
160311
clearInterval(this.polling);
161312
this.polling = undefined;
162313
},
314+
163315
reprocess: {
164316
create(ObjectType, queueObject) {
165317
const newObject = new ObjectType();

src/ParseObject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type SaveParams = {
5757
body: AttributeMap,
5858
};
5959

60-
type SaveOptions = FullOptions & {
60+
export type SaveOptions = FullOptions & {
6161
cascadeSave?: boolean,
6262
context?: AttributeMap,
6363
};

0 commit comments

Comments
 (0)