Skip to content

Commit f92e4d4

Browse files
authored
feat: Allow setting custom queue for handling offline operations via Parse.EventuallyQueue (#2106)
1 parent 94e9948 commit f92e4d4

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

src/CoreManager.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMuta
66
import type ParseFile from './ParseFile';
77
import type { FileSource } from './ParseFile';
88
import type { Op } from './ParseOp';
9-
import type ParseObject from './ParseObject';
9+
import type ParseObject, {SaveOptions} from './ParseObject';
1010
import type { QueryJSON } from './ParseQuery';
1111
import type ParseUser from './ParseUser';
1212
import type { AuthData } from './ParseUser';
@@ -73,6 +73,11 @@ type QueryController = {
7373
find: (className: string, params: QueryJSON, options: RequestOptions) => Promise,
7474
aggregate: (className: string, params: any, options: RequestOptions) => Promise,
7575
};
76+
type EventuallyQueue = {
77+
save: (object: ParseObject, serverOptions: SaveOptions) => Promise,
78+
destroy: (object: ParseObject, serverOptions: RequestOptions) => Promise,
79+
poll: (ms: number) => void
80+
};
7681
type RESTController = {
7782
request: (method: string, path: string, data: mixed, options: RequestOptions) => Promise,
7883
ajax: (method: string, url: string, data: any, headers?: any, options: FullOptions) => Promise,
@@ -363,6 +368,15 @@ const CoreManager = {
363368
return config['RESTController'];
364369
},
365370

371+
setEventuallyQueue(controller: EventuallyQueue) {
372+
requireMethods('EventuallyQueue', ['poll', 'save', 'destroy'], controller);
373+
config['EventuallyQueue'] = controller;
374+
},
375+
376+
getEventuallyQueue(): EventuallyQueue {
377+
return config['EventuallyQueue'];
378+
},
379+
366380
setSchemaController(controller: SchemaController) {
367381
requireMethods(
368382
'SchemaController',

src/Parse.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ const Parse: ParseType = {
123123
CoreManager: CoreManager,
124124
Config: Config,
125125
Error: ParseError,
126-
EventuallyQueue: EventuallyQueue,
127126
FacebookUtils: FacebookUtils,
128127
File: File,
129128
GeoPoint: GeoPoint,
@@ -153,6 +152,18 @@ const Parse: ParseType = {
153152
Hooks: undefined,
154153
Parse: undefined,
155154

155+
/**
156+
* @member {EventuallyQueue} Parse.EventuallyQueue
157+
* @static
158+
*/
159+
set EventuallyQueue(queue: EventuallyQueue) {
160+
CoreManager.setEventuallyQueue(queue);
161+
},
162+
163+
get EventuallyQueue() {
164+
return CoreManager.getEventuallyQueue();
165+
},
166+
156167
/**
157168
* Call this method first to set up your authentication tokens for Parse.
158169
*
@@ -189,6 +200,8 @@ const Parse: ParseType = {
189200
CoreManager.setIfNeeded('StorageController', StorageController);
190201
CoreManager.setIfNeeded('WebSocketController', WebSocketController);
191202

203+
CoreManager.setIfNeeded('EventuallyQueue', EventuallyQueue);
204+
192205
if (process.env.PARSE_BUILD === 'browser') {
193206
Parse.IndexedDB = CoreManager.setIfNeeded('IndexedDBStorageController', IndexedDBStorageController);
194207
}
@@ -395,7 +408,7 @@ const Parse: ParseType = {
395408
if (!this.LocalDatastore.isEnabled) {
396409
this.LocalDatastore.isEnabled = true;
397410
if (polling) {
398-
EventuallyQueue.poll(ms);
411+
CoreManager.getEventuallyQueue().poll(ms);
399412
}
400413
}
401414
},

src/ParseObject.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import canBeSerialized from './canBeSerialized';
77
import decode from './decode';
88
import encode from './encode';
99
import escape from './escape';
10-
import EventuallyQueue from './EventuallyQueue';
1110
import ParseACL from './ParseACL';
1211
import parseDate from './parseDate';
1312
import ParseError from './ParseError';
@@ -1220,8 +1219,8 @@ class ParseObject {
12201219
await this.save(null, options);
12211220
} catch (e) {
12221221
if (e.code === ParseError.CONNECTION_FAILED) {
1223-
await EventuallyQueue.save(this, options);
1224-
EventuallyQueue.poll();
1222+
await CoreManager.getEventuallyQueue().save(this, options);
1223+
CoreManager.getEventuallyQueue().poll();
12251224
}
12261225
}
12271226
return this;
@@ -1366,8 +1365,8 @@ class ParseObject {
13661365
await this.destroy(options);
13671366
} catch (e) {
13681367
if (e.code === ParseError.CONNECTION_FAILED) {
1369-
await EventuallyQueue.destroy(this, options);
1370-
EventuallyQueue.poll();
1368+
await CoreManager.getEventuallyQueue().destroy(this, options);
1369+
CoreManager.getEventuallyQueue().poll();
13711370
}
13721371
}
13731372
return this;

src/__tests__/Parse-test.js

+20
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ describe('Parse module', () => {
9494
expect(CoreManager.getLocalDatastoreController()).toBe(controller);
9595
});
9696

97+
it('cannot set EventuallyQueue controller with missing functions', () => {
98+
const controller = {
99+
};
100+
expect(() => Parse.EventuallyQueue = controller).toThrow(
101+
'EventuallyQueue must implement poll()'
102+
);
103+
});
104+
97105
it('can set AsyncStorage', () => {
98106
const controller = {
99107
getItem: function () {},
@@ -258,4 +266,16 @@ describe('Parse module', () => {
258266
process.env.PARSE_BUILD = 'node';
259267
});
260268
});
269+
270+
it('can set EventuallyQueue', () => {
271+
const controller = {
272+
poll: function () {},
273+
save: function () {},
274+
destroy: function () {},
275+
};
276+
277+
Parse.EventuallyQueue = controller;
278+
expect(CoreManager.getEventuallyQueue()).toBe(controller);
279+
expect(Parse.EventuallyQueue).toBe(controller);
280+
});
261281
});

src/__tests__/ParseObject-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ const flushPromises = require('./test_helpers/flushPromises');
160160

161161
CoreManager.setLocalDatastore(mockLocalDatastore);
162162
CoreManager.setRESTController(RESTController);
163+
CoreManager.setEventuallyQueue(EventuallyQueue);
163164
CoreManager.setInstallationController({
164165
currentInstallationId() {
165166
return Promise.resolve('iid');

0 commit comments

Comments
 (0)