@wemap/providers 3.3.0 → 4.0.1
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/debug/src/AbsoluteAttitudeComponent.jsx +12 -8
- package/debug/src/NavigationConfig.js +7 -8
- package/debug/src/RelativeAttitudeComponent.jsx +3 -3
- package/debug/src/Utils.js +32 -41
- package/index.js +6 -8
- package/package.json +8 -9
- package/src/Providers.js +26 -79
- package/src/ProvidersInterface.js +12 -9
- package/src/errors/AskImuOnDesktopError.js +4 -3
- package/src/errors/ContainsIgnoredProviderError.js +4 -3
- package/src/errors/GeolocationApiMissingError.js +4 -3
- package/src/errors/GeolocationPermissionDeniedError.js +4 -3
- package/src/errors/GeolocationPositionUnavailableError.js +4 -3
- package/src/errors/IpResolveServerError.js +4 -3
- package/src/errors/MissingAccelerometerError.js +3 -3
- package/src/errors/MissingArCoreError.js +4 -3
- package/src/errors/MissingGyroscopeError.js +3 -3
- package/src/errors/MissingNativeInterfaceError.js +4 -3
- package/src/errors/MissingSensorError.js +4 -3
- package/src/errors/NoProviderFoundError.js +4 -3
- package/src/events/ProviderEvent.js +0 -2
- package/src/events/ProvidersLoggerOld.js +35 -34
- package/src/mapmatching/MapMatchingHandler.js +138 -0
- package/src/providers/FakeProvider.spec.js +15 -17
- package/src/providers/Provider.js +5 -27
- package/src/providers/Provider.spec.js +18 -28
- package/src/providers/attitude/absolute/{AbsoluteAttitudeProvider.js → AbsoluteAttitude.js} +49 -18
- package/src/providers/attitude/absolute/{AbsoluteAttitudeFromBrowserProvider.js → AbsoluteAttitudeFromBrowser.js} +4 -5
- package/src/providers/attitude/absolute/{AbsoluteAttitudeFromRelAttProvider.js → AbsoluteAttitudeFromRelAtt.js} +8 -7
- package/src/providers/attitude/absolute/AbsoluteAttitudeFused.js +166 -0
- package/src/providers/attitude/relative/{RelativeAttitudeProvider.js → RelativeAttitude.js} +4 -5
- package/src/providers/attitude/relative/{RelativeAttitudeFromBrowserProvider.js → RelativeAttitudeFromBrowser.js} +6 -8
- package/src/providers/attitude/relative/{RelativeAttitudeFromEkfProvider.js → RelativeAttitudeFromEkf.js} +11 -11
- package/src/providers/attitude/relative/{RelativeAttitudeFromInertialProvider.js → RelativeAttitudeFromInertial.js} +5 -5
- package/src/providers/imu/{AccelerometerProvider.js → Accelerometer.js} +3 -3
- package/src/providers/imu/{GyroscopeProvider.js → Gyroscope.js} +3 -3
- package/src/providers/imu/{ImuProvider.js → Imu.js} +11 -5
- package/src/providers/inclination/{InclinationProvider.js → Inclination.js} +4 -5
- package/src/providers/inclination/{InclinationFromAccProvider.js → InclinationFromAcc.js} +5 -5
- package/src/providers/inclination/{InclinationFromRelativeAttitudeProvider.js → InclinationFromRelativeAttitude.js} +4 -4
- package/src/providers/legacy/helpers/ThugDetector.js +7 -7
- package/src/providers/others/{BarcodeProvider.js → Barcode.js} +3 -3
- package/src/providers/others/{CameraNativeProvider.js → CameraNative.js} +2 -2
- package/src/providers/others/{CameraProjectionMatrixProvider.js → CameraProjectionMatrix.js} +3 -3
- package/src/providers/position/absolute/AbsolutePosition.js +187 -0
- package/src/providers/position/absolute/{AbsolutePositionFromRelProvider.js → AbsolutePositionFromRel.js} +6 -8
- package/src/providers/position/absolute/{GnssWifiProvider.js → GnssWifi.js} +10 -9
- package/src/providers/position/absolute/{IpProvider.js → Ip.js} +4 -6
- package/src/providers/position/relative/{ArCoreProvider.js → ArCore.js} +29 -31
- package/src/providers/position/relative/{GeoRelativePositionProvider.js → GeoRelativePosition.js} +5 -5
- package/src/providers/position/relative/{GeoRelativePositionFromArCoreProvider.js → GeoRelativePositionFromArCore.js} +4 -6
- package/src/providers/position/relative/{PdrProvider.js → Pdr.js} +6 -7
- package/src/providers/steps/{StepDetectionProvider.js → StepDetection.js} +10 -9
- package/src/providers/steps/StepDetectionLadetto.js +12 -13
- package/src/providers/steps/StepDetectionMinMaxPeaks.js +19 -17
- package/src/providers/steps/StepDetectionMinMaxPeaks2.js +17 -18
- package/src/smoothers/AttitudeSmoother.js +100 -0
- package/src/providers/position/absolute/AbsolutePositionProvider.js +0 -295
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
const DEFAULT_MESSAGE = 'Native interface is missing';
|
|
2
|
-
|
|
3
1
|
class MissingNativeInterfaceError extends Error {
|
|
2
|
+
|
|
3
|
+
static DEFAULT_MESSAGE = 'Native interface is missing';
|
|
4
|
+
|
|
4
5
|
constructor(message) {
|
|
5
|
-
super(message || DEFAULT_MESSAGE);
|
|
6
|
+
super(message || MissingNativeInterfaceError.DEFAULT_MESSAGE);
|
|
6
7
|
}
|
|
7
8
|
}
|
|
8
9
|
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
const DEFAULT_MESSAGE = 'Impossible to retrieve events, a sensor is missing';
|
|
2
|
-
|
|
3
1
|
class MissingSensorError extends Error {
|
|
2
|
+
|
|
3
|
+
static DEFAULT_MESSAGE = 'Impossible to retrieve events, a sensor is missing';
|
|
4
|
+
|
|
4
5
|
constructor(message) {
|
|
5
|
-
super(message || DEFAULT_MESSAGE);
|
|
6
|
+
super(message || MissingSensorError.DEFAULT_MESSAGE);
|
|
6
7
|
}
|
|
7
8
|
|
|
8
9
|
from(fromMessage) {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
const DEFAULT_MESSAGE = 'Unable to find a provider with your given parameters';
|
|
2
|
-
|
|
3
1
|
class NoProviderFoundError extends Error {
|
|
2
|
+
|
|
3
|
+
static DEFAULT_MESSAGE = 'Unable to find a provider with your given parameters';
|
|
4
|
+
|
|
4
5
|
constructor(message) {
|
|
5
|
-
super(message || DEFAULT_MESSAGE);
|
|
6
|
+
super(message || NoProviderFoundError.DEFAULT_MESSAGE);
|
|
6
7
|
}
|
|
7
8
|
}
|
|
8
9
|
|
|
@@ -6,7 +6,6 @@ import EventType from './EventType.js';
|
|
|
6
6
|
*/
|
|
7
7
|
class ProviderEvent {
|
|
8
8
|
|
|
9
|
-
timestamp = -1;
|
|
10
9
|
dataType = EventType.Unknown;
|
|
11
10
|
providersStack = [];
|
|
12
11
|
data = null;
|
|
@@ -23,7 +22,6 @@ class ProviderEvent {
|
|
|
23
22
|
|
|
24
23
|
clone() {
|
|
25
24
|
const evt = new ProviderEvent(this.dataType, this.data);
|
|
26
|
-
evt.timestamp = this.timestamp;
|
|
27
25
|
evt.providersStack = this.providersStack.slice(0);
|
|
28
26
|
return evt;
|
|
29
27
|
}
|
|
@@ -1,83 +1,84 @@
|
|
|
1
1
|
import Logger from '@wemap/logger';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
const objectsIdMap = new WeakMap();
|
|
3
|
+
class ProvidersLoggerOld {
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
let pushEventsRef = {};
|
|
5
|
+
_enabled = false;
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
currentId = 0;
|
|
8
|
+
objectsIdMap = new WeakMap();
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
pushEvents = {};
|
|
11
|
+
pushEventsRef = {};
|
|
12
|
+
|
|
13
|
+
interval;
|
|
14
|
+
initDate;
|
|
13
15
|
|
|
14
|
-
static _enabled = false;
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
get enabled() {
|
|
17
18
|
return this._enabled;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
set enabled(_newVal) {
|
|
21
22
|
this._enabled = _newVal;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
initializeInterval() {
|
|
25
26
|
|
|
26
|
-
if (interval) {
|
|
27
|
+
if (this.interval) {
|
|
27
28
|
return;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
interval = setInterval(() => {
|
|
31
|
+
this.interval = setInterval(() => {
|
|
31
32
|
|
|
32
|
-
for (const [key, value] of Object.entries(pushEvents)) {
|
|
33
|
-
Logger.debug('Received ' + value + ' notifications from ' + pushEventsRef[key].pname + ' last second');
|
|
33
|
+
for (const [key, value] of Object.entries(this.pushEvents)) {
|
|
34
|
+
Logger.debug('Received ' + value + ' notifications from ' + this.pushEventsRef[key].pname + ' last second');
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
pushEvents = {};
|
|
37
|
-
pushEventsRef = {};
|
|
37
|
+
this.pushEvents = {};
|
|
38
|
+
this.pushEventsRef = {};
|
|
38
39
|
}, 1000);
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
if (!objectsIdMap.has(object)) {
|
|
43
|
-
objectsIdMap.set(object, ++currentId);
|
|
42
|
+
getObjectId(object) {
|
|
43
|
+
if (!this.objectsIdMap.has(object)) {
|
|
44
|
+
this.objectsIdMap.set(object, ++this.currentId);
|
|
44
45
|
}
|
|
45
|
-
return objectsIdMap.get(object);
|
|
46
|
+
return this.objectsIdMap.get(object);
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
|
|
49
|
+
addEvent(object, method) {
|
|
49
50
|
|
|
50
|
-
if (!
|
|
51
|
+
if (!this.enabled) {
|
|
51
52
|
return;
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
if (!initDate) {
|
|
55
|
-
initDate = Date.now();
|
|
55
|
+
if (!this.initDate) {
|
|
56
|
+
this.initDate = Date.now();
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
this.initializeInterval();
|
|
59
60
|
|
|
60
|
-
const objectId =
|
|
61
|
+
const objectId = this.getObjectId(object);
|
|
61
62
|
const objectClassName = object.pname;
|
|
62
63
|
|
|
63
64
|
Logger.debug(objectClassName + '[' + objectId + '].' + method);
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
|
|
67
|
+
incrementNotifications(object) {
|
|
67
68
|
|
|
68
|
-
if (!
|
|
69
|
+
if (!this.enabled) {
|
|
69
70
|
return;
|
|
70
71
|
}
|
|
71
72
|
|
|
72
|
-
const objectId =
|
|
73
|
+
const objectId = this.getObjectId(object);
|
|
73
74
|
|
|
74
|
-
let counter = pushEvents[objectId];
|
|
75
|
+
let counter = this.pushEvents[objectId];
|
|
75
76
|
if (!counter) {
|
|
76
77
|
counter = 0;
|
|
77
|
-
pushEventsRef[objectId] = object;
|
|
78
|
+
this.pushEventsRef[objectId] = object;
|
|
78
79
|
}
|
|
79
|
-
pushEvents[objectId] = counter + 1;
|
|
80
|
+
this.pushEvents[objectId] = counter + 1;
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
}
|
|
83
|
-
export default ProvidersLoggerOld;
|
|
84
|
+
export default new ProvidersLoggerOld();
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MapMatching, Network, UserPosition
|
|
3
|
+
} from '@wemap/geo';
|
|
4
|
+
import { deg2rad } from '@wemap/maths';
|
|
5
|
+
|
|
6
|
+
import AbsoluteAttitudeFused from '../providers/attitude/absolute/AbsoluteAttitudeFused.js';
|
|
7
|
+
import AbsolutePosition from '../providers/position/absolute/AbsolutePosition.js';
|
|
8
|
+
import ProvidersOptions from '../ProvidersOptions.js';
|
|
9
|
+
|
|
10
|
+
class MapMatchingHandler {
|
|
11
|
+
|
|
12
|
+
static MM_MAX_ANGLE = deg2rad(20);
|
|
13
|
+
static MM_MAX_DIST = 30;
|
|
14
|
+
static MM_MIN_DIST = 0;
|
|
15
|
+
|
|
16
|
+
/** @type {MapMatching} */
|
|
17
|
+
_mapMatching;
|
|
18
|
+
|
|
19
|
+
/** @type {number} */
|
|
20
|
+
_mapMatchingMinDistance;
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
|
|
25
|
+
if (this.constructor._instance) {
|
|
26
|
+
throw new Error(`Cannot instantiate ${this.name} twice`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this._mapMatching = new MapMatching();
|
|
30
|
+
this._mapMatching.maxDistance = MapMatchingHandler.MM_MAX_DIST;
|
|
31
|
+
this._mapMatching.maxAngleBearing = MapMatchingHandler.MM_MAX_ANGLE;
|
|
32
|
+
this._mapMatchingMinDistance = MapMatchingHandler.MM_MIN_DIST;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get enabled() {
|
|
36
|
+
return ProvidersOptions.useMapMatching;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {Network} network
|
|
41
|
+
*/
|
|
42
|
+
set network(network) {
|
|
43
|
+
this._mapMatching.network = network;
|
|
44
|
+
|
|
45
|
+
if (!this.enabled) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const lastEvent = AbsolutePosition.lastEvent;
|
|
50
|
+
if (network !== null && lastEvent) {
|
|
51
|
+
const projectedPosition = this.calcProjection(lastEvent.data, true);
|
|
52
|
+
if (projectedPosition) {
|
|
53
|
+
const newEvent = lastEvent.clone();
|
|
54
|
+
newEvent.data = projectedPosition;
|
|
55
|
+
AbsolutePosition.notify(newEvent);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {UserPosition} position
|
|
62
|
+
* @param {boolean} shouldTryMmOnNodes
|
|
63
|
+
* @returns {UserPosition}
|
|
64
|
+
*/
|
|
65
|
+
calcProjection(position, shouldTryMmOnNodes = false) {
|
|
66
|
+
|
|
67
|
+
let projection = null;
|
|
68
|
+
let bearingUsedForProjection = false;
|
|
69
|
+
|
|
70
|
+
if (!this.enabled || !this._mapMatching.network) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Firstly, if new position bearing is known, try to use map-matching with bearing
|
|
75
|
+
// That is not always the case, for e.g. the first position returned by the GnssWifi provider.
|
|
76
|
+
if (position.bearing !== null) {
|
|
77
|
+
projection = this._mapMatching.getProjection(position, true, true);
|
|
78
|
+
bearingUsedForProjection = true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Secondly, if map-matching with bearing did not work, try MM on nodes (without bearing).
|
|
82
|
+
// Note: this is not allowed for positions from AbsolutePositionFromRel to allow
|
|
83
|
+
// user to leave the network.
|
|
84
|
+
if (!projection && shouldTryMmOnNodes) {
|
|
85
|
+
projection = this._mapMatching.getProjection(position, true, false);
|
|
86
|
+
bearingUsedForProjection = false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// No projection found
|
|
90
|
+
if (!projection) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (bearingUsedForProjection) {
|
|
95
|
+
AbsoluteAttitudeFused.mapMatching(projection);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Do not use projection if it too close from itinerary,
|
|
99
|
+
// this allows left/right movements (ie with ArCore)
|
|
100
|
+
if (projection.distanceFromNearestElement < this._mapMatchingMinDistance) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Do not use projection.projection directly, because position has some specific properties like time, bearing and altitude
|
|
105
|
+
const projectedPosition = position.clone();
|
|
106
|
+
projectedPosition.lat = projection.projection.lat;
|
|
107
|
+
projectedPosition.lng = projection.projection.lng;
|
|
108
|
+
projectedPosition.level = projection.projection.level;
|
|
109
|
+
|
|
110
|
+
return projectedPosition;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get maxDistance() {
|
|
114
|
+
return this._mapMatching.maxDistance;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
set maxDistance(maxDistance) {
|
|
118
|
+
this._mapMatching.maxDistance = maxDistance;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
get minDistance() {
|
|
122
|
+
return this._mapMatchingMinDistance;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
set minDistance(minDistance) {
|
|
126
|
+
this._mapMatchingMinDistance = minDistance;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get maxAngleBearing() {
|
|
130
|
+
return this._mapMatching.maxAngleBearing;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
set maxAngleBearing(maxAngleBearing) {
|
|
134
|
+
this._mapMatching.maxAngleBearing = maxAngleBearing;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export default new MapMatchingHandler();
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { UserPosition } from '@wemap/geo';
|
|
2
|
-
import { TimeUtils } from '@wemap/utils';
|
|
3
2
|
|
|
4
3
|
import Provider from './Provider.js';
|
|
5
4
|
import EventType from '../events/EventType.js';
|
|
6
5
|
|
|
7
|
-
class
|
|
6
|
+
class FakeProvider1Clazz extends Provider {
|
|
8
7
|
static get pname() {
|
|
9
8
|
return 'FakeProvider1';
|
|
10
9
|
}
|
|
11
10
|
}
|
|
11
|
+
const FakeProvider1 = new FakeProvider1Clazz();
|
|
12
|
+
export { FakeProvider1 };
|
|
12
13
|
|
|
13
|
-
class
|
|
14
|
+
class FakeProvider2Clazz extends Provider {
|
|
14
15
|
static get pname() {
|
|
15
16
|
return 'FakeProvider2';
|
|
16
17
|
}
|
|
@@ -20,8 +21,10 @@ class FakeProvider2 extends Provider {
|
|
|
20
21
|
start() { }
|
|
21
22
|
stop() { }
|
|
22
23
|
}
|
|
24
|
+
const FakeProvider2 = new FakeProvider2Clazz();
|
|
25
|
+
export { FakeProvider2 };
|
|
23
26
|
|
|
24
|
-
class
|
|
27
|
+
class FakeProvider3Clazz extends Provider {
|
|
25
28
|
static get pname() {
|
|
26
29
|
return 'FakeProvider3';
|
|
27
30
|
}
|
|
@@ -29,38 +32,33 @@ class FakeProvider3 extends Provider {
|
|
|
29
32
|
this.intervalId = setInterval(() => {
|
|
30
33
|
const fakePosition = new UserPosition(Math.random() * 45, Math.random() * 180);
|
|
31
34
|
this.notify(this.createEvent(EventType.AbsolutePosition, fakePosition));
|
|
32
|
-
this.notify(this.createEvent(EventType.AbsolutePosition, fakePosition, TimeUtils.preciseTime));
|
|
33
35
|
}, 1);
|
|
34
36
|
}
|
|
35
37
|
stop() {
|
|
36
38
|
clearInterval(this.intervalId);
|
|
37
39
|
}
|
|
38
40
|
}
|
|
41
|
+
const FakeProvider3 = new FakeProvider3Clazz();
|
|
42
|
+
export { FakeProvider3 };
|
|
39
43
|
|
|
40
|
-
class
|
|
44
|
+
class FakeProvider4Clazz extends Provider {
|
|
41
45
|
|
|
42
46
|
static get pname() {
|
|
43
47
|
return 'FakeProvider4';
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
constructor() {
|
|
47
|
-
super();
|
|
48
|
-
this.fp3 = FakeProvider3.instance;
|
|
49
|
-
}
|
|
50
50
|
get _availability() {
|
|
51
|
-
return
|
|
51
|
+
return FakeProvider3.availability;
|
|
52
52
|
}
|
|
53
53
|
start() {
|
|
54
|
-
this.fp3id =
|
|
54
|
+
this.fp3id = FakeProvider3.addEventListener(
|
|
55
55
|
events => this.notify(events[0].clone()),
|
|
56
56
|
this.notifyError.bind(this)
|
|
57
57
|
);
|
|
58
58
|
}
|
|
59
59
|
stop() {
|
|
60
|
-
|
|
60
|
+
FakeProvider3.removeEventListener(this.fp3id);
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
|
|
64
|
-
export {
|
|
65
|
-
FakeProvider1, FakeProvider2, FakeProvider3, FakeProvider4
|
|
66
|
-
};
|
|
63
|
+
const FakeProvider4 = new FakeProvider4Clazz();
|
|
64
|
+
export { FakeProvider4 };
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import noop from 'lodash.noop';
|
|
2
2
|
|
|
3
|
-
import { TimeUtils } from '@wemap/utils';
|
|
4
|
-
|
|
5
3
|
import EventType from '../events/EventType.js';
|
|
6
4
|
import ProviderEvent from '../events/ProviderEvent.js';
|
|
7
5
|
import ProvidersLoggerOld from '../events/ProvidersLoggerOld.js';
|
|
@@ -9,7 +7,6 @@ import ContainsIgnoredProviderError from '../errors/ContainsIgnoredProviderError
|
|
|
9
7
|
import ProvidersOptions from '../ProvidersOptions.js';
|
|
10
8
|
import ProviderState from './ProviderState.js';
|
|
11
9
|
|
|
12
|
-
let uniqueId = 1;
|
|
13
10
|
|
|
14
11
|
/**
|
|
15
12
|
* A provider is a meta class to define an entity which can be
|
|
@@ -18,25 +15,13 @@ let uniqueId = 1;
|
|
|
18
15
|
class Provider {
|
|
19
16
|
|
|
20
17
|
static _callbackUniqueId = 0;
|
|
18
|
+
static _uniqueId = 1;
|
|
21
19
|
|
|
22
20
|
state = ProviderState.STOPPPED;
|
|
23
21
|
|
|
24
22
|
_eventsCallbacks = [];
|
|
25
23
|
_monitoringCallbacks = [];
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Singleton pattern using reflection.
|
|
30
|
-
* @returns {Provider}
|
|
31
|
-
*/
|
|
32
|
-
static get instance() {
|
|
33
|
-
if (!this._instance) {
|
|
34
|
-
this._instance = Reflect.construct(this, []);
|
|
35
|
-
}
|
|
36
|
-
return this._instance;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
25
|
/**
|
|
41
26
|
* Provider constructor
|
|
42
27
|
*/
|
|
@@ -46,11 +31,7 @@ class Provider {
|
|
|
46
31
|
throw new Error('Can\'t instantiate Provider directly');
|
|
47
32
|
}
|
|
48
33
|
|
|
49
|
-
|
|
50
|
-
throw new Error(`Cannot instantiate ${this.pname} twice`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this.id = uniqueId++;
|
|
34
|
+
this.id = Provider._uniqueId++;
|
|
54
35
|
|
|
55
36
|
ProvidersLoggerOld.addEvent(this, 'constructor');
|
|
56
37
|
}
|
|
@@ -96,13 +77,11 @@ class Provider {
|
|
|
96
77
|
* Create an event from current provider
|
|
97
78
|
* @param {ProviderEvent#DataType} dataType type of ProviderEvent
|
|
98
79
|
* @param {Object} data data exported to ProviderEvent
|
|
99
|
-
* @param {Number} timestamp event timestamp
|
|
100
80
|
* @returns {ProviderEvent}
|
|
101
81
|
* @protected
|
|
102
82
|
*/
|
|
103
|
-
static createEvent(dataType, data,
|
|
83
|
+
static createEvent(dataType, data, fromEvents = []) {
|
|
104
84
|
const event = new ProviderEvent(dataType, data);
|
|
105
|
-
event.timestamp = typeof timestamp === 'number' ? timestamp : TimeUtils.preciseTime / 1e3;
|
|
106
85
|
const newStack = fromEvents.reduce((acc, _event) => acc.concat(_event.providersStack), []);
|
|
107
86
|
// Remove duplicates and keep lasts
|
|
108
87
|
event.providersStack = [...new Set(newStack.reverse())].reverse();
|
|
@@ -113,13 +92,12 @@ class Provider {
|
|
|
113
92
|
* Create an event from current provider
|
|
114
93
|
* @param {EventType} dataType type of ProviderEvent
|
|
115
94
|
* @param {Object} data data exported to ProviderEvent
|
|
116
|
-
* @param {Number} timestamp event timestamp
|
|
117
95
|
* @param {ProviderEvent[]} fromEvents events used for the creation of the new one
|
|
118
96
|
* @returns {ProviderEvent}
|
|
119
97
|
* @protected
|
|
120
98
|
*/
|
|
121
|
-
createEvent(dataType, data,
|
|
122
|
-
return this.constructor.createEvent(dataType, data,
|
|
99
|
+
createEvent(dataType, data, fromEvents) {
|
|
100
|
+
return this.constructor.createEvent(dataType, data, fromEvents);
|
|
123
101
|
}
|
|
124
102
|
|
|
125
103
|
get hasNativeInterface() {
|
|
@@ -24,18 +24,11 @@ describe('Provider', () => {
|
|
|
24
24
|
|
|
25
25
|
it('creation/singleton', () => {
|
|
26
26
|
expect(() => new Provider()).throw(Error);
|
|
27
|
-
expect(() => Provider.instance).throw(Error);
|
|
28
|
-
expect(() => FakeProvider1.instance).not.throw(Error);
|
|
29
|
-
expect(() => FakeProvider2.instance).not.throw(Error);
|
|
30
|
-
expect(() => FakeProvider3.instance).not.throw(Error);
|
|
31
|
-
expect(() => FakeProvider4.instance).not.throw(Error);
|
|
32
|
-
expect(FakeProvider1.instance).equals(FakeProvider1.instance);
|
|
33
|
-
expect(FakeProvider4.instance).equals(FakeProvider4.instance);
|
|
34
27
|
});
|
|
35
28
|
|
|
36
29
|
|
|
37
30
|
it('default getter', () => {
|
|
38
|
-
const provider = FakeProvider1
|
|
31
|
+
const provider = FakeProvider1;
|
|
39
32
|
expect(provider.pname).equals(FakeProvider1.pname);
|
|
40
33
|
expect(provider.eventsType).is.empty;
|
|
41
34
|
expect(provider.hasNativeInterface).is.false;
|
|
@@ -45,33 +38,30 @@ describe('Provider', () => {
|
|
|
45
38
|
|
|
46
39
|
it('availability', async () => {
|
|
47
40
|
|
|
48
|
-
|
|
49
|
-
await expect(fakeProvider1.availability).to.be.fulfilled;
|
|
41
|
+
await expect(FakeProvider1.availability).to.be.fulfilled;
|
|
50
42
|
ProvidersOptions.ignoreProviders.push(FakeProvider1.pname);
|
|
51
|
-
await expect(
|
|
43
|
+
await expect(FakeProvider1.availability).to.be.rejectedWith(ContainsIgnoredProviderError);
|
|
52
44
|
ProvidersOptions.ignoreProviders = [];
|
|
53
45
|
|
|
54
|
-
await expect(FakeProvider2.
|
|
46
|
+
await expect(FakeProvider2.availability).to.be.rejectedWith(Error);
|
|
55
47
|
|
|
56
|
-
await expect(FakeProvider3.
|
|
57
|
-
await expect(FakeProvider4.
|
|
48
|
+
await expect(FakeProvider3.availability).to.be.fulfilled;
|
|
49
|
+
await expect(FakeProvider4.availability).to.be.fulfilled;
|
|
58
50
|
});
|
|
59
51
|
|
|
60
52
|
it('checkAvailability on start', async () => {
|
|
61
53
|
|
|
62
|
-
const fp2 = FakeProvider2.instance;
|
|
63
|
-
|
|
64
54
|
/**
|
|
65
55
|
* checkAvailabilityOnStart = true
|
|
66
56
|
*/
|
|
67
57
|
let errored = false;
|
|
68
|
-
|
|
58
|
+
FakeProvider2.addEventListener(noop, () => (errored = true));
|
|
69
59
|
|
|
70
60
|
await sleep(2);
|
|
71
61
|
|
|
72
62
|
expect(errored).is.true;
|
|
73
|
-
expect(
|
|
74
|
-
expect(
|
|
63
|
+
expect(FakeProvider2.state).equals(ProviderState.STOPPPED);
|
|
64
|
+
expect(FakeProvider2._eventsCallbacks.length).equals(0);
|
|
75
65
|
|
|
76
66
|
/**
|
|
77
67
|
* checkAvailabilityOnStart = false
|
|
@@ -79,16 +69,16 @@ describe('Provider', () => {
|
|
|
79
69
|
ProvidersOptions.checkAvailabilityOnStart = false;
|
|
80
70
|
|
|
81
71
|
errored = false;
|
|
82
|
-
const id =
|
|
72
|
+
const id = FakeProvider2.addEventListener(noop, () => (errored = true));
|
|
83
73
|
|
|
84
74
|
await sleep(2);
|
|
85
75
|
|
|
86
76
|
expect(errored).is.false;
|
|
87
|
-
expect(
|
|
77
|
+
expect(FakeProvider2.state).equals(ProviderState.STARTED);
|
|
88
78
|
|
|
89
|
-
|
|
90
|
-
expect(
|
|
91
|
-
expect(
|
|
79
|
+
FakeProvider2.removeEventListener(id);
|
|
80
|
+
expect(FakeProvider2.state).equals(ProviderState.STOPPPED);
|
|
81
|
+
expect(FakeProvider2._eventsCallbacks.length).equals(0);
|
|
92
82
|
|
|
93
83
|
ProvidersOptions.checkAvailabilityOnStart = true;
|
|
94
84
|
});
|
|
@@ -122,13 +112,13 @@ describe('Provider', () => {
|
|
|
122
112
|
|
|
123
113
|
it('start/stop', async () => {
|
|
124
114
|
let errored = false;
|
|
125
|
-
FakeProvider1.
|
|
115
|
+
FakeProvider1.addEventListener(noop, (errored = true));
|
|
126
116
|
await sleep(2);
|
|
127
117
|
expect(errored).is.true;
|
|
128
118
|
|
|
129
|
-
await expect(startStop(FakeProvider2
|
|
130
|
-
await expect(startStop(FakeProvider3
|
|
131
|
-
await expect(startStop(FakeProvider4
|
|
119
|
+
await expect(startStop(FakeProvider2)).to.be.fulfilled;
|
|
120
|
+
await expect(startStop(FakeProvider3)).to.be.fulfilled;
|
|
121
|
+
await expect(startStop(FakeProvider4)).to.be.fulfilled;
|
|
132
122
|
|
|
133
123
|
});
|
|
134
124
|
});
|