@wemap/providers 12.9.2 → 12.9.3

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.
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "url": "git+https://github.com/wemap/wemap-modules-js.git"
13
13
  },
14
14
  "name": "@wemap/providers",
15
- "version": "12.9.2",
15
+ "version": "12.9.3",
16
16
  "bugs": {
17
17
  "url": "https://github.com/wemap/wemap-modules-js/issues"
18
18
  },
@@ -55,5 +55,5 @@
55
55
  },
56
56
  "./helpers/*": "./helpers/*"
57
57
  },
58
- "gitHead": "184f537812866759dc10dd68c5c90d23ff05db43"
58
+ "gitHead": "6187429610fc17029a4233dffb414350eb3db976"
59
59
  }
@@ -19,20 +19,23 @@ import { AbsolutePosition } from '../../../events/Types.js';
19
19
  */
20
20
  class GnssWifiProvider extends Provider<AbsolutePosition> {
21
21
 
22
- static POSITION_OPTIONS = {
23
- enableHighAccuracy: true,
24
- timeout: Infinity,
25
- maximumAge: 0
26
- };
27
-
28
22
  static DEFAULT_DISCARD_POSITIONS_ABOVE = 50;
29
23
 
30
24
  discardPositionsAbove = GnssWifiProvider.DEFAULT_DISCARD_POSITIONS_ABOVE;
31
25
 
26
+ enableHighAccuracy = true;
27
+
32
28
  geoLocationId?: number;
33
29
 
34
30
  getName = () => 'GnssWifi';
35
31
 
32
+ get positionOptions() {
33
+ return {
34
+ enableHighAccuracy: this.enableHighAccuracy,
35
+ timeout: Infinity,
36
+ maximumAge: 0
37
+ };
38
+ }
36
39
 
37
40
  availability() {
38
41
  return typeof (navigator) === 'object' && navigator.geolocation
@@ -41,13 +44,20 @@ class GnssWifiProvider extends Provider<AbsolutePosition> {
41
44
  }
42
45
 
43
46
  start() {
47
+ // Trigger last known position
48
+ navigator.geolocation.getCurrentPosition(
49
+ this.onNewPosition,
50
+ this.onPositionError,
51
+ this.positionOptions
52
+ );
53
+
44
54
  // Note: added timeout for a workaround on Safari/iOS when user disabled geolocation:
45
55
  // watchPosition does not send the error callback without the "setTimeout"
46
56
  setTimeout(() => {
47
57
  this.geoLocationId = navigator.geolocation.watchPosition(
48
58
  this.onNewPosition,
49
59
  this.onPositionError,
50
- GnssWifiProvider.POSITION_OPTIONS
60
+ this.positionOptions
51
61
  );
52
62
  }, 150);
53
63
  }
@@ -81,7 +91,9 @@ class GnssWifiProvider extends Provider<AbsolutePosition> {
81
91
  coords.accuracy,
82
92
  bearing);
83
93
 
84
- if (typeof this.discardPositionsAbove === 'number'
94
+ if (
95
+ this.enableHighAccuracy &&
96
+ typeof this.discardPositionsAbove === 'number'
85
97
  && coords.accuracy > this.discardPositionsAbove
86
98
  ) {
87
99
  return;
@@ -0,0 +1,90 @@
1
+ import chai from 'chai';
2
+ import sinon from 'sinon';
3
+
4
+ import GnssWifiProvider from './GnssWifiProvider.js';
5
+
6
+ const { expect } = chai;
7
+
8
+ describe('GnssWifiProvider', () => {
9
+ it('onNewPosition - should notify when accuracy is low', async () => {
10
+
11
+ const fakePositionEvent = {
12
+ coords: {
13
+ latitude: 43,
14
+ longitude: 3,
15
+ accuracy: 10,
16
+ altitude: 0,
17
+ altitudeAccuracy: 0,
18
+ heading: 0,
19
+ speed: 0
20
+ },
21
+ timestamp: Date.now()
22
+ };
23
+
24
+ const notifySpy = sinon.spy();
25
+
26
+ const callbackId = GnssWifiProvider.addEventListener(notifySpy);
27
+
28
+ (GnssWifiProvider as any).onNewPosition(fakePositionEvent);
29
+
30
+ expect(notifySpy.callCount).to.equal(1);
31
+
32
+ GnssWifiProvider.removeEventListener(callbackId);
33
+
34
+ });
35
+
36
+ it('onNewPosition - should not notify when accuracy is greater than options', async () => {
37
+
38
+ const fakePositionEvent = {
39
+ coords: {
40
+ latitude: 43,
41
+ longitude: 3,
42
+ accuracy: 100,
43
+ altitude: 0,
44
+ altitudeAccuracy: 0,
45
+ heading: 0,
46
+ speed: 0
47
+ },
48
+ timestamp: Date.now()
49
+ };
50
+
51
+ const notifySpy = sinon.spy();
52
+
53
+ const callbackId = GnssWifiProvider.addEventListener(notifySpy);
54
+
55
+ (GnssWifiProvider as any).onNewPosition(fakePositionEvent);
56
+
57
+ expect(notifySpy.callCount).to.equal(0);
58
+
59
+ GnssWifiProvider.removeEventListener(callbackId);
60
+ });
61
+
62
+ it('onNewPosition - should notify when accuracy is high and enableHighAccuracy false', async () => {
63
+
64
+ const fakePositionEvent = {
65
+ coords: {
66
+ latitude: 43,
67
+ longitude: 3,
68
+ accuracy: 100,
69
+ altitude: 0,
70
+ altitudeAccuracy: 0,
71
+ heading: 0,
72
+ speed: 0
73
+ },
74
+ timestamp: Date.now()
75
+ };
76
+
77
+ const notifySpy = sinon.spy();
78
+ GnssWifiProvider.enableHighAccuracy = false;
79
+
80
+ const callbackId = GnssWifiProvider.addEventListener(notifySpy);
81
+
82
+ (GnssWifiProvider as any).onNewPosition(fakePositionEvent);
83
+
84
+ expect(notifySpy.callCount).to.equal(1);
85
+
86
+ // Reset
87
+ GnssWifiProvider.enableHighAccuracy = true;
88
+ GnssWifiProvider.removeEventListener(callbackId);
89
+ });
90
+ });