Skip to content

Commit

Permalink
feat: add options to enable polling and set the polling interval; fix…
Browse files Browse the repository at this point in the history
…es excessive polling (#1419)
  • Loading branch information
dplewis authored Oct 27, 2021
1 parent 672cdc9 commit 0f804b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/Parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const Parse = {
/* eslint-enable no-console */
}
Parse._initialize(applicationId, javaScriptKey);
EventuallyQueue.poll();
},

_initialize(applicationId: string, javaScriptKey: string, masterKey: string) {
Expand Down Expand Up @@ -267,12 +266,23 @@ Parse._getInstallationId = function () {
};
/**
* Enable pinning in your application.
* This must be called before your application can use pinning.
* This must be called after `Parse.initialize` in your application.
*
* @param [polling] Allow pinging the server /health endpoint. Default true
* @param [ms] Milliseconds to ping the server. Default 2000ms
* @static
*/
Parse.enableLocalDatastore = function () {
Parse.LocalDatastore.isEnabled = true;
Parse.enableLocalDatastore = function (polling = true, ms: number = 2000) {
if (!Parse.applicationId) {
console.log("'enableLocalDataStore' must be called after 'initialize'");
return;
}
if (!Parse.LocalDatastore.isEnabled) {
Parse.LocalDatastore.isEnabled = true;
if (polling) {
EventuallyQueue.poll(ms);
}
}
};
/**
* Flag that indicates whether Local Datastore is enabled.
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/Parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,34 @@ describe('Parse module', () => {

it('can enable LocalDatastore', () => {
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
jest.spyOn(EventuallyQueue, 'poll').mockImplementationOnce(() => {});

Parse.initialize(null, null);
Parse.enableLocalDatastore();
expect(console.log).toHaveBeenCalledWith(
"'enableLocalDataStore' must be called after 'initialize'"
);

Parse.initialize('A', 'B');
Parse.LocalDatastore.isEnabled = false;
Parse.enableLocalDatastore();

expect(Parse.LocalDatastore.isEnabled).toBe(true);
expect(Parse.isLocalDatastoreEnabled()).toBe(true);
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(1);
expect(EventuallyQueue.poll).toHaveBeenCalledWith(2000);

EventuallyQueue.poll.mockClear();
const polling = false;
Parse.enableLocalDatastore(polling);
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(0);
});

it('can dump LocalDatastore', async () => {
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
Parse.LocalDatastore.isEnabled = false;
let LDS = await Parse.dumpLocalDatastore();
expect(console.log).toHaveBeenCalledWith('Parse.enableLocalDatastore() must be called first');
expect(LDS).toEqual({});
Parse.LocalDatastore.isEnabled = true;
const controller = {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/browser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Browser', () => {
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
jest.spyOn(EventuallyQueue, 'poll').mockImplementationOnce(() => {});
Parse.initialize('A', 'B');
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(1);
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(0);
});

it('load StorageController', () => {
Expand Down

0 comments on commit 0f804b8

Please sign in to comment.