@dhis2/app-service-offline 3.9.2 → 3.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,8 @@ exports.useDhis2ConnectionStatus = exports.getLastConnectedKey = exports.Dhis2Co
7
7
 
8
8
  var _appServiceConfig = require("@dhis2/app-service-config");
9
9
 
10
+ var _lodash = require("lodash");
11
+
10
12
  var _propTypes = _interopRequireDefault(require("prop-types"));
11
13
 
12
14
  var _react = _interopRequireWildcard(require("react"));
@@ -160,24 +162,27 @@ const Dhis2ConnectionStatusProvider = (_ref) => {
160
162
 
161
163
  const handleBlur = () => smartInterval.pause();
162
164
 
163
- const handleFocus = () => smartInterval.resume(); // On offline event, ping immediately to test server connection.
164
- // Only do this when going offline -- it's theoretically no-cost
165
- // for both online and offline servers. Pinging when going online
166
- // can be costly for clients connecting over the internet to online
167
- // servers.
165
+ const handleFocus = () => smartInterval.resume(); // Pinging when going offline should be low/no-cost in both online and
166
+ // local servers
167
+
168
168
 
169
+ const handleOffline = () => smartInterval.invokeCallbackImmediately(); // Pinging when going online has a cost but improves responsiveness of
170
+ // the connection status -- only do it once every 15 seconds at most
169
171
 
170
- const handleOffline = () => smartInterval.invokeCallbackImmediately();
171
172
 
173
+ const handleOnline = (0, _lodash.throttle)(() => smartInterval.invokeCallbackImmediately(), 15000);
172
174
  window.addEventListener('blur', handleBlur);
173
175
  window.addEventListener('focus', handleFocus);
174
176
  window.addEventListener('offline', handleOffline);
177
+ window.addEventListener('online', handleOnline);
175
178
  return () => {
176
179
  window.removeEventListener('blur', handleBlur);
177
180
  window.removeEventListener('focus', handleFocus);
178
- window.removeEventListener('offline', handleOffline); // clean up smart interval
181
+ window.removeEventListener('offline', handleOffline);
182
+ window.removeEventListener('online', handleOnline); // clean up smart interval and throttled function
179
183
 
180
184
  smartInterval.clear();
185
+ handleOnline.cancel();
181
186
  };
182
187
  }, [pingAndHandleStatus, serverVersion]);
183
188
  (0, _react.useEffect)(() => {
@@ -515,6 +515,30 @@ describe('it pings when an offline event is detected', () => {
515
515
  });
516
516
  });
517
517
  });
518
+ describe('it pings when an online event is detected', () => {
519
+ test('if the app is focused, it pings immediately', () => {
520
+ (0, _reactHooks.renderHook)(() => (0, _dhis2ConnectionStatus.useDhis2ConnectionStatus)(), {
521
+ wrapper
522
+ });
523
+ window.dispatchEvent(new Event('offline'));
524
+ expect(mockPing).toHaveBeenCalledTimes(1);
525
+ window.dispatchEvent(new Event('online'));
526
+ expect(mockPing).toHaveBeenCalledTimes(2);
527
+ });
528
+ test('pings are throttled', () => {
529
+ (0, _reactHooks.renderHook)(() => (0, _dhis2ConnectionStatus.useDhis2ConnectionStatus)(), {
530
+ wrapper
531
+ });
532
+ window.dispatchEvent(new Event('offline'));
533
+ window.dispatchEvent(new Event('online'));
534
+ window.dispatchEvent(new Event('offline'));
535
+ expect(mockPing).toHaveBeenCalledTimes(3);
536
+ window.dispatchEvent(new Event('online')); // Another ping should NOT be sent immediately after this latest
537
+ // online event
538
+
539
+ expect(mockPing).toHaveBeenCalledTimes(3); // (Not testing throttle time here because further pings may interfere)
540
+ });
541
+ });
518
542
  describe('lastConnected status', () => {
519
543
  test('it sets lastConnected in localStorage when it becomes disconnected', async () => {
520
544
  const {
@@ -9,7 +9,7 @@ exports.isPingAvailable = isPingAvailable;
9
9
  * Checks the server version to see if the /api/ping endpoint is available for
10
10
  * this version.
11
11
  *
12
- * The endpoint was released for versions 2.37.10, 2.38.3, 2.39.2, and 2.40.0
12
+ * The endpoint was released for versions 2.37.10, 2.38.4, 2.39.2, and 2.40.0
13
13
  * (see DHIS2-14531). All versions below that are considered unsupported
14
14
  *
15
15
  * If the patchVersion is undefined, it's assumed to be a dev server that's
@@ -32,7 +32,7 @@ function isPingAvailable(serverVersion) {
32
32
  return patch === undefined || patch >= 2;
33
33
 
34
34
  case 38:
35
- return patch === undefined || patch >= 3;
35
+ return patch === undefined || patch >= 4;
36
36
 
37
37
  case 37:
38
38
  return patch === undefined || patch >= 10;
@@ -16,7 +16,7 @@ const fixedVersions = [{
16
16
  full: 'unimportant',
17
17
  major: 2,
18
18
  minor: 38,
19
- patch: 3
19
+ patch: 4
20
20
  }, {
21
21
  full: 'unimportant',
22
22
  major: 2,
@@ -1,4 +1,5 @@
1
1
  import { useConfig } from '@dhis2/app-service-config';
2
+ import { throttle } from 'lodash';
2
3
  import PropTypes from 'prop-types';
3
4
  import React, { useCallback, useState, useRef, useMemo, useEffect, useContext } from 'react';
4
5
  import { useOfflineInterface } from '../offline-interface';
@@ -135,24 +136,27 @@ export const Dhis2ConnectionStatusProvider = (_ref) => {
135
136
 
136
137
  const handleBlur = () => smartInterval.pause();
137
138
 
138
- const handleFocus = () => smartInterval.resume(); // On offline event, ping immediately to test server connection.
139
- // Only do this when going offline -- it's theoretically no-cost
140
- // for both online and offline servers. Pinging when going online
141
- // can be costly for clients connecting over the internet to online
142
- // servers.
139
+ const handleFocus = () => smartInterval.resume(); // Pinging when going offline should be low/no-cost in both online and
140
+ // local servers
143
141
 
144
142
 
145
- const handleOffline = () => smartInterval.invokeCallbackImmediately();
143
+ const handleOffline = () => smartInterval.invokeCallbackImmediately(); // Pinging when going online has a cost but improves responsiveness of
144
+ // the connection status -- only do it once every 15 seconds at most
146
145
 
146
+
147
+ const handleOnline = throttle(() => smartInterval.invokeCallbackImmediately(), 15000);
147
148
  window.addEventListener('blur', handleBlur);
148
149
  window.addEventListener('focus', handleFocus);
149
150
  window.addEventListener('offline', handleOffline);
151
+ window.addEventListener('online', handleOnline);
150
152
  return () => {
151
153
  window.removeEventListener('blur', handleBlur);
152
154
  window.removeEventListener('focus', handleFocus);
153
- window.removeEventListener('offline', handleOffline); // clean up smart interval
155
+ window.removeEventListener('offline', handleOffline);
156
+ window.removeEventListener('online', handleOnline); // clean up smart interval and throttled function
154
157
 
155
158
  smartInterval.clear();
159
+ handleOnline.cancel();
156
160
  };
157
161
  }, [pingAndHandleStatus, serverVersion]);
158
162
  useEffect(() => {
@@ -505,6 +505,30 @@ describe('it pings when an offline event is detected', () => {
505
505
  });
506
506
  });
507
507
  });
508
+ describe('it pings when an online event is detected', () => {
509
+ test('if the app is focused, it pings immediately', () => {
510
+ renderHook(() => useDhis2ConnectionStatus(), {
511
+ wrapper
512
+ });
513
+ window.dispatchEvent(new Event('offline'));
514
+ expect(mockPing).toHaveBeenCalledTimes(1);
515
+ window.dispatchEvent(new Event('online'));
516
+ expect(mockPing).toHaveBeenCalledTimes(2);
517
+ });
518
+ test('pings are throttled', () => {
519
+ renderHook(() => useDhis2ConnectionStatus(), {
520
+ wrapper
521
+ });
522
+ window.dispatchEvent(new Event('offline'));
523
+ window.dispatchEvent(new Event('online'));
524
+ window.dispatchEvent(new Event('offline'));
525
+ expect(mockPing).toHaveBeenCalledTimes(3);
526
+ window.dispatchEvent(new Event('online')); // Another ping should NOT be sent immediately after this latest
527
+ // online event
528
+
529
+ expect(mockPing).toHaveBeenCalledTimes(3); // (Not testing throttle time here because further pings may interfere)
530
+ });
531
+ });
508
532
  describe('lastConnected status', () => {
509
533
  test('it sets lastConnected in localStorage when it becomes disconnected', async () => {
510
534
  const {
@@ -2,7 +2,7 @@
2
2
  * Checks the server version to see if the /api/ping endpoint is available for
3
3
  * this version.
4
4
  *
5
- * The endpoint was released for versions 2.37.10, 2.38.3, 2.39.2, and 2.40.0
5
+ * The endpoint was released for versions 2.37.10, 2.38.4, 2.39.2, and 2.40.0
6
6
  * (see DHIS2-14531). All versions below that are considered unsupported
7
7
  *
8
8
  * If the patchVersion is undefined, it's assumed to be a dev server that's
@@ -25,7 +25,7 @@ export function isPingAvailable(serverVersion) {
25
25
  return patch === undefined || patch >= 2;
26
26
 
27
27
  case 38:
28
- return patch === undefined || patch >= 3;
28
+ return patch === undefined || patch >= 4;
29
29
 
30
30
  case 37:
31
31
  return patch === undefined || patch >= 10;
@@ -13,7 +13,7 @@ const fixedVersions = [{
13
13
  full: 'unimportant',
14
14
  major: 2,
15
15
  minor: 38,
16
- patch: 3
16
+ patch: 4
17
17
  }, {
18
18
  full: 'unimportant',
19
19
  major: 2,
@@ -3,7 +3,7 @@ import { Version } from '@dhis2/app-service-config';
3
3
  * Checks the server version to see if the /api/ping endpoint is available for
4
4
  * this version.
5
5
  *
6
- * The endpoint was released for versions 2.37.10, 2.38.3, 2.39.2, and 2.40.0
6
+ * The endpoint was released for versions 2.37.10, 2.38.4, 2.39.2, and 2.40.0
7
7
  * (see DHIS2-14531). All versions below that are considered unsupported
8
8
  *
9
9
  * If the patchVersion is undefined, it's assumed to be a dev server that's
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dhis2/app-service-offline",
3
3
  "description": "A runtime service for online/offline detection and offline caching",
4
- "version": "3.9.2",
4
+ "version": "3.9.4",
5
5
  "main": "./build/cjs/index.js",
6
6
  "module": "./build/es/index.js",
7
7
  "types": "build/types/index.d.ts",
@@ -33,7 +33,7 @@
33
33
  "coverage": "yarn test --coverage"
34
34
  },
35
35
  "peerDependencies": {
36
- "@dhis2/app-service-config": "3.9.2",
36
+ "@dhis2/app-service-config": "3.9.4",
37
37
  "prop-types": "^15.7.2",
38
38
  "react": "^16.8.6",
39
39
  "react-dom": "^16.8.6"