Skip to content

feat: add options to enable polling and set the polling interval; fixes excessive polling #1419

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

Merged
merged 6 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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