Skip to content

Commit

Permalink
feat: Allow setting custom queue for handling offline operations via …
Browse files Browse the repository at this point in the history
…`Parse.EventuallyQueue` (#2106)
  • Loading branch information
mortenmo authored Apr 25, 2024
1 parent 94e9948 commit f92e4d4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
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 @@ -123,7 +123,6 @@ const Parse: ParseType = {
CoreManager: CoreManager,
Config: Config,
Error: ParseError,
EventuallyQueue: EventuallyQueue,
FacebookUtils: FacebookUtils,
File: File,
GeoPoint: GeoPoint,
Expand Down Expand Up @@ -153,6 +152,18 @@ const Parse: ParseType = {
Hooks: undefined,
Parse: undefined,

/**
* @member {EventuallyQueue} Parse.EventuallyQueue
* @static
*/
set EventuallyQueue(queue: EventuallyQueue) {
CoreManager.setEventuallyQueue(queue);
},

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

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

CoreManager.setIfNeeded('EventuallyQueue', EventuallyQueue);

if (process.env.PARSE_BUILD === 'browser') {
Parse.IndexedDB = CoreManager.setIfNeeded('IndexedDBStorageController', IndexedDBStorageController);
}
Expand Down Expand Up @@ -395,7 +408,7 @@ const Parse: ParseType = {
if (!this.LocalDatastore.isEnabled) {
this.LocalDatastore.isEnabled = true;
if (polling) {
EventuallyQueue.poll(ms);
CoreManager.getEventuallyQueue().poll(ms);
}
}
},
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
20 changes: 20 additions & 0 deletions src/__tests__/Parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ describe('Parse module', () => {
expect(CoreManager.getLocalDatastoreController()).toBe(controller);
});

it('cannot set EventuallyQueue controller with missing functions', () => {
const controller = {
};
expect(() => Parse.EventuallyQueue = controller).toThrow(
'EventuallyQueue must implement poll()'
);
});

it('can set AsyncStorage', () => {
const controller = {
getItem: function () {},
Expand Down Expand Up @@ -258,4 +266,16 @@ describe('Parse module', () => {
process.env.PARSE_BUILD = 'node';
});
});

it('can set EventuallyQueue', () => {
const controller = {
poll: function () {},
save: function () {},
destroy: function () {},
};

Parse.EventuallyQueue = controller;
expect(CoreManager.getEventuallyQueue()).toBe(controller);
expect(Parse.EventuallyQueue).toBe(controller);
});
});
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

0 comments on commit f92e4d4

Please sign in to comment.