Skip to content

Commit 0f804b8

Browse files
authored
feat: add options to enable polling and set the polling interval; fixes excessive polling (#1419)
1 parent 672cdc9 commit 0f804b8

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/Parse.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const Parse = {
4747
/* eslint-enable no-console */
4848
}
4949
Parse._initialize(applicationId, javaScriptKey);
50-
EventuallyQueue.poll();
5150
},
5251

5352
_initialize(applicationId: string, javaScriptKey: string, masterKey: string) {
@@ -267,12 +266,23 @@ Parse._getInstallationId = function () {
267266
};
268267
/**
269268
* Enable pinning in your application.
270-
* This must be called before your application can use pinning.
269+
* This must be called after `Parse.initialize` in your application.
271270
*
271+
* @param [polling] Allow pinging the server /health endpoint. Default true
272+
* @param [ms] Milliseconds to ping the server. Default 2000ms
272273
* @static
273274
*/
274-
Parse.enableLocalDatastore = function () {
275-
Parse.LocalDatastore.isEnabled = true;
275+
Parse.enableLocalDatastore = function (polling = true, ms: number = 2000) {
276+
if (!Parse.applicationId) {
277+
console.log("'enableLocalDataStore' must be called after 'initialize'");
278+
return;
279+
}
280+
if (!Parse.LocalDatastore.isEnabled) {
281+
Parse.LocalDatastore.isEnabled = true;
282+
if (polling) {
283+
EventuallyQueue.poll(ms);
284+
}
285+
}
276286
};
277287
/**
278288
* Flag that indicates whether Local Datastore is enabled.

src/__tests__/Parse-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,34 @@ describe('Parse module', () => {
114114

115115
it('can enable LocalDatastore', () => {
116116
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
117+
jest.spyOn(EventuallyQueue, 'poll').mockImplementationOnce(() => {});
118+
119+
Parse.initialize(null, null);
120+
Parse.enableLocalDatastore();
121+
expect(console.log).toHaveBeenCalledWith(
122+
"'enableLocalDataStore' must be called after 'initialize'"
123+
);
124+
125+
Parse.initialize('A', 'B');
117126
Parse.LocalDatastore.isEnabled = false;
118127
Parse.enableLocalDatastore();
128+
119129
expect(Parse.LocalDatastore.isEnabled).toBe(true);
120130
expect(Parse.isLocalDatastoreEnabled()).toBe(true);
131+
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(1);
132+
expect(EventuallyQueue.poll).toHaveBeenCalledWith(2000);
133+
134+
EventuallyQueue.poll.mockClear();
135+
const polling = false;
136+
Parse.enableLocalDatastore(polling);
137+
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(0);
121138
});
122139

123140
it('can dump LocalDatastore', async () => {
141+
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
124142
Parse.LocalDatastore.isEnabled = false;
125143
let LDS = await Parse.dumpLocalDatastore();
144+
expect(console.log).toHaveBeenCalledWith('Parse.enableLocalDatastore() must be called first');
126145
expect(LDS).toEqual({});
127146
Parse.LocalDatastore.isEnabled = true;
128147
const controller = {

src/__tests__/browser-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('Browser', () => {
5757
jest.spyOn(console, 'log').mockImplementationOnce(() => {});
5858
jest.spyOn(EventuallyQueue, 'poll').mockImplementationOnce(() => {});
5959
Parse.initialize('A', 'B');
60-
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(1);
60+
expect(EventuallyQueue.poll).toHaveBeenCalledTimes(0);
6161
});
6262

6363
it('load StorageController', () => {

0 commit comments

Comments
 (0)