Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting custom queue for handling offline operations via Parse.EventuallyQueue #2106

Merged
Merged
16 changes: 15 additions & 1 deletion src/CoreManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { AttributeMap, ObjectCache, OpsMap, State } from './ObjectStateMuta
import type ParseFile from './ParseFile';
import type { FileSource } from './ParseFile';
import type { Op } from './ParseOp';
import type ParseObject from './ParseObject';
import type ParseObject, {SaveOptions} from './ParseObject';
import type { QueryJSON } from './ParseQuery';
import type ParseUser from './ParseUser';
import type { AuthData } from './ParseUser';
Expand Down Expand Up @@ -73,6 +73,11 @@ type QueryController = {
find: (className: string, params: QueryJSON, options: RequestOptions) => Promise,
aggregate: (className: string, params: any, options: RequestOptions) => Promise,
};
type EventuallyQueue = {
save: (object: ParseObject, serverOptions: SaveOptions) => Promise,
destroy: (object: ParseObject, serverOptions: RequestOptions) => Promise,
poll: (ms: number) => void
};
type RESTController = {
request: (method: string, path: string, data: mixed, options: RequestOptions) => Promise,
ajax: (method: string, url: string, data: any, headers?: any, options: FullOptions) => Promise,
Expand Down Expand Up @@ -363,6 +368,15 @@ const CoreManager = {
return config['RESTController'];
},

setEventuallyQueue(controller: EventuallyQueue) {
requireMethods('EventuallyQueue', ['poll', 'save', 'destroy'], controller);
config['EventuallyQueue'] = controller;
},

getEventuallyQueue(): EventuallyQueue {
return config['EventuallyQueue'];
},

setSchemaController(controller: SchemaController) {
requireMethods(
'SchemaController',
Expand Down
17 changes: 15 additions & 2 deletions src/Parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const Parse: ParseType = {
CoreManager: CoreManager,
Config: Config,
Error: ParseError,
EventuallyQueue: EventuallyQueue,
FacebookUtils: FacebookUtils,
File: File,
GeoPoint: GeoPoint,
Expand Down Expand Up @@ -151,6 +150,18 @@ const Parse: ParseType = {
Hooks: undefined,
Parse: undefined,

/**
* @member {EventuallyQueue} Parse.EventuallyQueue
* @static
*/
set EventuallyQueue(queue: EventuallyQueue) {
mortenmo marked this conversation as resolved.
Show resolved Hide resolved
CoreManager.setEventuallyQueue(queue);
},

get EventuallyQueue() {
return CoreManager.getEventuallyQueue();
},

/**
* Call this method first to set up your authentication tokens for Parse.
*
Expand Down Expand Up @@ -185,6 +196,8 @@ const Parse: ParseType = {
Parse.LiveQuery = new LiveQuery();
CoreManager.setIfNeeded('LiveQuery', Parse.LiveQuery);

CoreManager.setIfNeeded('EventuallyQueue', EventuallyQueue);

if (process.env.PARSE_BUILD === 'browser') {
Parse.IndexedDB = CoreManager.setIfNeeded('IndexedDBStorageController', IndexedDBStorageController);
}
Expand Down Expand Up @@ -380,7 +393,7 @@ const Parse: ParseType = {
if (!this.LocalDatastore.isEnabled) {
this.LocalDatastore.isEnabled = true;
if (polling) {
EventuallyQueue.poll(ms);
CoreManager.getEventuallyQueue().poll(ms);
mortenmo marked this conversation as resolved.
Show resolved Hide resolved
}
}
},
Expand Down
9 changes: 4 additions & 5 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import canBeSerialized from './canBeSerialized';
import decode from './decode';
import encode from './encode';
import escape from './escape';
import EventuallyQueue from './EventuallyQueue';
import ParseACL from './ParseACL';
import parseDate from './parseDate';
import ParseError from './ParseError';
Expand Down Expand Up @@ -1220,8 +1219,8 @@ class ParseObject {
await this.save(null, options);
} catch (e) {
if (e.code === ParseError.CONNECTION_FAILED) {
await EventuallyQueue.save(this, options);
EventuallyQueue.poll();
await CoreManager.getEventuallyQueue().save(this, options);
CoreManager.getEventuallyQueue().poll();
}
}
return this;
Expand Down Expand Up @@ -1366,8 +1365,8 @@ class ParseObject {
await this.destroy(options);
} catch (e) {
if (e.code === ParseError.CONNECTION_FAILED) {
await EventuallyQueue.destroy(this, options);
EventuallyQueue.poll();
await CoreManager.getEventuallyQueue().destroy(this, options);
CoreManager.getEventuallyQueue().poll();
}
}
return this;
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const flushPromises = require('./test_helpers/flushPromises');

CoreManager.setLocalDatastore(mockLocalDatastore);
CoreManager.setRESTController(RESTController);
CoreManager.setEventuallyQueue(EventuallyQueue);
CoreManager.setInstallationController({
currentInstallationId() {
return Promise.resolve('iid');
Expand Down
Loading