hypertrack-sdk-react-native 10.0.3 → 11.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/CONTRIBUTING.md +1 -1
  3. package/README.md +2 -2
  4. package/android/build.gradle +5 -1
  5. package/android/gradle.properties +1 -1
  6. package/android/src/main/java/com/reactnativehypertracksdk/HyperTrackReactNativePlugin.kt +80 -115
  7. package/android/src/main/java/com/reactnativehypertracksdk/ReactNativeSerialization.kt +23 -3
  8. package/android/src/main/java/com/reactnativehypertracksdk/common/HyperTrackSdkWrapper.kt +87 -220
  9. package/android/src/main/java/com/reactnativehypertracksdk/common/SdkMethod.kt +11 -9
  10. package/android/src/main/java/com/reactnativehypertracksdk/common/Serialization.kt +216 -132
  11. package/android/src/main/java/com/reactnativehypertracksdk/common/{Result.kt → WrapperResult.kt} +9 -7
  12. package/hypertrack-sdk-react-native.podspec +1 -1
  13. package/ios/HyperTrackReactNativePlugin.m +13 -11
  14. package/ios/HyperTrackReactNativePlugin.swift +141 -114
  15. package/ios/common/DictionaryDecoder.swift +5 -5
  16. package/ios/common/HyperTrackSDKWrapper.swift +46 -80
  17. package/ios/common/SDKMethod.swift +13 -12
  18. package/ios/common/Serialization.swift +200 -245
  19. package/lib/commonjs/HyperTrack/HyperTrack.js +224 -124
  20. package/lib/commonjs/HyperTrack/HyperTrack.js.map +1 -1
  21. package/lib/commonjs/HyperTrack/data_types/HyperTrackError.js +13 -17
  22. package/lib/commonjs/HyperTrack/data_types/HyperTrackError.js.map +1 -1
  23. package/lib/commonjs/HyperTrack/data_types/internal/HyperTrackErrorInternal.js.map +1 -1
  24. package/lib/commonjs/HyperTrack/data_types/internal/Metadata.js +2 -0
  25. package/lib/commonjs/HyperTrack/data_types/internal/Metadata.js.map +1 -0
  26. package/lib/commonjs/HyperTrack/data_types/internal/Name.js +2 -0
  27. package/lib/commonjs/HyperTrack/data_types/internal/Name.js.map +1 -0
  28. package/lib/commonjs/index.js +0 -7
  29. package/lib/commonjs/index.js.map +1 -1
  30. package/lib/module/HyperTrack/HyperTrack.js +224 -124
  31. package/lib/module/HyperTrack/HyperTrack.js.map +1 -1
  32. package/lib/module/HyperTrack/data_types/HyperTrackError.js +13 -17
  33. package/lib/module/HyperTrack/data_types/HyperTrackError.js.map +1 -1
  34. package/lib/module/HyperTrack/data_types/internal/HyperTrackErrorInternal.js.map +1 -1
  35. package/lib/module/HyperTrack/data_types/internal/Metadata.js +2 -0
  36. package/lib/module/HyperTrack/data_types/internal/Metadata.js.map +1 -0
  37. package/lib/module/HyperTrack/data_types/internal/Name.js +2 -0
  38. package/lib/module/HyperTrack/data_types/internal/Name.js.map +1 -0
  39. package/lib/module/index.js +0 -1
  40. package/lib/module/index.js.map +1 -1
  41. package/lib/typescript/HyperTrack/HyperTrack.d.ts +106 -54
  42. package/lib/typescript/HyperTrack/data_types/HyperTrackError.d.ts +59 -17
  43. package/lib/typescript/HyperTrack/data_types/internal/HyperTrackErrorInternal.d.ts +1 -1
  44. package/lib/typescript/HyperTrack/data_types/internal/Metadata.d.ts +4 -0
  45. package/lib/typescript/HyperTrack/data_types/internal/Name.d.ts +7 -0
  46. package/lib/typescript/index.d.ts +0 -1
  47. package/package.json +1 -1
  48. package/src/HyperTrack/HyperTrack.ts +281 -161
  49. package/src/HyperTrack/data_types/HyperTrackError.ts +73 -18
  50. package/src/HyperTrack/data_types/internal/HyperTrackErrorInternal.ts +3 -3
  51. package/src/HyperTrack/data_types/internal/Metadata.ts +4 -0
  52. package/src/HyperTrack/data_types/internal/Name.ts +7 -0
  53. package/src/index.tsx +0 -1
  54. package/android/src/main/java/com/reactnativehypertracksdk/common/HyperTrackError.kt +0 -28
  55. package/android/src/main/java/com/reactnativehypertracksdk/common/LocationError.kt +0 -9
  56. package/android/src/main/java/com/reactnativehypertracksdk/common/SdkInitParams.kt +0 -44
  57. package/ios/common/HyperTrackError.swift +0 -20
  58. package/ios/common/SDKInitParams.swift +0 -13
  59. package/lib/commonjs/HyperTrack/data_types/SdkInitParams.js +0 -2
  60. package/lib/commonjs/HyperTrack/data_types/SdkInitParams.js.map +0 -1
  61. package/lib/commonjs/HyperTrack/data_types/internal/DeviceName.js +0 -2
  62. package/lib/commonjs/HyperTrack/data_types/internal/DeviceName.js.map +0 -1
  63. package/lib/module/HyperTrack/data_types/SdkInitParams.js +0 -2
  64. package/lib/module/HyperTrack/data_types/SdkInitParams.js.map +0 -1
  65. package/lib/module/HyperTrack/data_types/internal/DeviceName.js +0 -2
  66. package/lib/module/HyperTrack/data_types/internal/DeviceName.js.map +0 -1
  67. package/lib/typescript/HyperTrack/data_types/SdkInitParams.d.ts +0 -25
  68. package/lib/typescript/HyperTrack/data_types/internal/DeviceName.d.ts +0 -4
  69. package/src/HyperTrack/data_types/SdkInitParams.ts +0 -28
  70. package/src/HyperTrack/data_types/internal/DeviceName.ts +0 -4
@@ -1,23 +1,30 @@
1
- import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
1
+ import {
2
+ NativeModules,
3
+ Platform,
4
+ NativeEventEmitter,
5
+ EmitterSubscription,
6
+ } from 'react-native';
2
7
  import { HyperTrackError } from './data_types/HyperTrackError';
3
8
  import type { IsAvailable } from './data_types/internal/IsAvailable';
4
9
  import type { IsTracking } from './data_types/internal/IsTracking';
5
10
  import type { Location } from './data_types/Location';
6
11
  import type { LocationError } from './data_types/LocationError';
7
- import type { SdkInitParams } from './data_types/SdkInitParams';
8
12
  import type { DeviceId } from './data_types/internal/DeviceId';
9
13
  import type { GeotagData } from './data_types/internal/GeotagData';
10
- import type { DeviceName } from './data_types/internal/DeviceName';
14
+ import type { Name } from './data_types/internal/Name';
11
15
  import type { HyperTrackErrorInternal } from './data_types/internal/HyperTrackErrorInternal';
12
- import type { Result } from './data_types/Result';
13
16
  import type { LocationWithDeviation } from './data_types/LocationWithDeviation';
14
17
  import type { LocationErrorInternal } from './data_types/internal/LocationErrorInternal';
18
+ import type { Result } from './data_types/Result';
15
19
  import type { LocationInternal } from './data_types/internal/LocationInternal';
16
20
  import type { LocationWithDeviationInternal } from './data_types/internal/LocationWithDeviationInternal';
21
+ import type { Metadata } from './data_types/internal/Metadata';
17
22
 
18
- const EVENT_TRACKING = 'onTrackingChanged';
19
- const EVENT_AVAILABILITY = 'onAvailabilityChanged';
20
- const EVENT_ERROR = 'onError';
23
+ const EVENT_ERRORS = 'errors';
24
+ const EVENT_IS_AVAILABLE = 'isAvailable';
25
+ const EVENT_IS_TRACKING = 'isTracking';
26
+ const EVENT_LOCATE = 'locate';
27
+ const EVENT_LOCATION = 'location';
21
28
 
22
29
  const LINKING_ERROR =
23
30
  `The package 'hypertrack-sdk-react-native' doesn't seem to be linked. Make sure: \n\n` +
@@ -39,193 +46,234 @@ const HyperTrackSdk = NativeModules.HyperTrackReactNativePlugin
39
46
  const EventEmitter = new NativeEventEmitter(HyperTrackSdk);
40
47
 
41
48
  export default class HyperTrack {
42
- private constructor() {
43
- return this;
44
- }
49
+ private static locateSubscription: EmitterSubscription | undefined;
45
50
 
46
51
  /**
47
- * Creates an SDK instance
52
+ * Adds a new geotag
48
53
  *
49
- * @param publishableKey publishable key provided in HyperTrack’s dashboard setup page
50
- * @param sdkInitParams
51
- * @returns HyperTrack instance
54
+ * @param {Object} data - Geotad data JSON
55
+ * @returns current location if success or LocationError if failure
52
56
  */
53
- static async initialize(
54
- publishableKey: string,
55
- sdkInitParams: SdkInitParams = {}
56
- ): Promise<HyperTrack> {
57
- try {
58
- await HyperTrackSdk.initializeSdk({
59
- publishableKey,
60
- loggingEnabled: sdkInitParams.loggingEnabled ?? false,
61
- allowMockLocations: sdkInitParams.allowMockLocations ?? false,
62
- requireBackgroundTrackingPermission:
63
- sdkInitParams.requireBackgroundTrackingPermission ?? false,
64
- automaticallyRequestPermissions:
65
- sdkInitParams.automaticallyRequestPermissions ?? true,
66
- });
67
- return new HyperTrack();
68
- } catch (error: any) {
69
- throw error;
57
+ static async addGeotag(
58
+ data: Object
59
+ ): Promise<Result<Location, LocationError>>;
60
+
61
+ /**
62
+ * Adds a new geotag with expected location
63
+ *
64
+ * @param {Object} data - Geotad data JSON
65
+ * @param {Location} expectedLocation - Expected location
66
+ * @returns location with deviation if success or LocationError if failure
67
+ */
68
+ static async addGeotag(
69
+ data: Object,
70
+ expectedLocation: Location
71
+ ): Promise<Result<LocationWithDeviation, LocationError>>;
72
+
73
+ static async addGeotag(
74
+ ...args: any[]
75
+ ): Promise<Result<Location | LocationWithDeviation, LocationError>> {
76
+ if (args.length === 1 && typeof args[0] === 'object') {
77
+ return HyperTrackSdk.addGeotag({
78
+ data: args[0],
79
+ expectedLocation: undefined,
80
+ } as GeotagData).then(
81
+ (locationResponse: Result<LocationInternal, LocationErrorInternal>) => {
82
+ return this.deserializeLocationResponse(locationResponse);
83
+ }
84
+ );
85
+ } else if (
86
+ args.length === 2 &&
87
+ typeof args[0] === 'object' &&
88
+ HyperTrack.isLocation(args[1])
89
+ ) {
90
+ let expectedLocation = args[1] as Location;
91
+ return HyperTrackSdk.addGeotag({
92
+ data: args[0],
93
+ expectedLocation: {
94
+ type: 'location',
95
+ value: {
96
+ latitude: expectedLocation.latitude,
97
+ longitude: expectedLocation.longitude,
98
+ },
99
+ } as LocationInternal,
100
+ } as GeotagData).then(
101
+ (
102
+ locationResponse: Result<
103
+ LocationWithDeviationInternal,
104
+ LocationErrorInternal
105
+ >
106
+ ) => {
107
+ return this.deserializeLocationWithDeviationResponse(
108
+ locationResponse
109
+ );
110
+ }
111
+ );
112
+ } else {
113
+ throw new Error('Invalid arguments');
70
114
  }
71
115
  }
72
116
 
73
117
  /**
74
118
  * Returns a string that is used to uniquely identify the device
119
+ *
120
+ * @returns {string} Device ID
75
121
  */
76
- getDeviceId(): Promise<string> {
122
+ static async getDeviceId(): Promise<string> {
77
123
  return HyperTrackSdk.getDeviceId().then(
78
124
  (deviceId: DeviceId) => deviceId.value
79
125
  );
80
126
  }
81
127
 
82
128
  /**
83
- * Reflects availability of the device for the Nearby search
129
+ * Returns a list of errors that blocks SDK from tracking
84
130
  *
85
- * @returns true when is available or false when unavailable
131
+ * @returns {HyperTrackError[]} List of errors
86
132
  */
87
- isAvailable(): Promise<boolean> {
88
- return HyperTrackSdk.isAvailable().then(
89
- (isAvailable: IsAvailable) => isAvailable.value
133
+ static async getErrors(): Promise<HyperTrackError[]> {
134
+ return HyperTrackSdk.getErrors().then(
135
+ (errors: HyperTrackErrorInternal[]) => {
136
+ return this.deserializeHyperTrackErrors(errors);
137
+ }
90
138
  );
91
139
  }
92
140
 
93
141
  /**
94
- * Sets the availability of the device for the Nearby search
142
+ * Reflects availability of the device for the Nearby search
95
143
  *
96
- * @param availability true when is available or false when unavailable
144
+ * @returns true when is available or false when unavailable
97
145
  */
98
- setAvailability(isAvailable: boolean) {
99
- HyperTrackSdk.setAvailability({
100
- type: 'isAvailable',
101
- value: isAvailable,
102
- } as IsAvailable);
146
+ static async getIsAvailable(): Promise<boolean> {
147
+ return HyperTrackSdk.getIsAvailable().then(
148
+ (isAvailable: IsAvailable) => isAvailable.value
149
+ );
103
150
  }
104
151
 
105
152
  /**
106
153
  * Reflects the tracking intent for the device
107
154
  *
108
- * @return {boolean} Whether the user's movement data is getting tracked or not.
155
+ * @returns {boolean} Whether the user's movement data is getting tracked or not.
109
156
  */
110
- isTracking(): Promise<boolean> {
111
- return HyperTrackSdk.isTracking().then(
157
+ static async getIsTracking(): Promise<boolean> {
158
+ return HyperTrackSdk.getIsTracking().then(
112
159
  (isTracking: IsTracking) => isTracking.value
113
160
  );
114
161
  }
115
162
 
116
163
  /**
117
- * Syncs the device state with the HyperTrack servers
164
+ * Reflects the current location of the user or an outage reason
118
165
  */
119
- sync() {
120
- HyperTrackSdk.sync();
166
+ static async getLocation(): Promise<Result<Location, LocationError>> {
167
+ return HyperTrackSdk.getLocation().then(
168
+ (locationResponse: Result<LocationInternal, LocationErrorInternal>) => {
169
+ return this.deserializeLocationResponse(locationResponse);
170
+ }
171
+ );
121
172
  }
122
173
 
123
174
  /**
124
- * Expresses an intent to start location tracking for the device
175
+ * Gets the metadata that is set for the device
176
+ *
177
+ * @returns {Object} Metadata JSON
125
178
  */
126
- startTracking() {
127
- HyperTrackSdk.startTracking();
179
+ static async getMetadata(): Promise<Object> {
180
+ return HyperTrackSdk.getMetadata().then((metadata: Metadata) => {
181
+ return this.deserializeMetadata(metadata);
182
+ });
128
183
  }
129
184
 
130
185
  /**
131
- * Stops location tracking immediately
186
+ * Gets the name that is set for the device
187
+ *
188
+ * @returns {string} Device name
132
189
  */
133
- stopTracking() {
134
- HyperTrackSdk.stopTracking();
190
+ static async getName(): Promise<string> {
191
+ return HyperTrackSdk.getName().then((name: Name) => {
192
+ return this.deserializeName(name);
193
+ });
135
194
  }
136
195
 
137
196
  /**
138
- * Reflects the current location of the user or an outage reason
197
+ * Requests one-time location update and returns the location once it is available, or error.
198
+ *
199
+ * Only one locate subscription can be active at a time. If you re-subscribe, the old EmitterSubscription
200
+ * will be automaticaly removed.
201
+ *
202
+ * This method will start location tracking if called, and will stop it when the location is received or
203
+ * the subscription is cancelled. If any other tracking intent is present (e.g. isAvailable is set to `true`),
204
+ * the tracking will not be stopped.
205
+ *
206
+ * @param callback
207
+ * @returns EmitterSubscription
208
+ * @example ```js
209
+ * const subscription = HyperTrack.locate(location => {
210
+ * ...
211
+ * })
212
+ *
213
+ * // to unsubscribe
214
+ * subscription.remove()
215
+ * ```
139
216
  */
140
- getLocation(): Promise<Result<Location, LocationError>> {
141
- return HyperTrackSdk.getLocation().then(
142
- (locationResponse: Result<LocationInternal, LocationErrorInternal>) => {
143
- return this.deserializeLocationResponse(locationResponse);
217
+ static locate(
218
+ callback: (location: Result<Location, HyperTrackError[]>) => void
219
+ ) {
220
+ this.locateSubscription?.remove();
221
+ this.locateSubscription = EventEmitter.addListener(
222
+ EVENT_LOCATE,
223
+ (location: Result<LocationInternal, HyperTrackErrorInternal[]>) => {
224
+ callback(this.deserializeLocateResponse(location));
144
225
  }
145
226
  );
227
+ HyperTrackSdk.locate();
228
+ return this.locateSubscription;
146
229
  }
147
230
 
148
231
  /**
149
- * Sets the name for the device
150
- * @param {string} name
232
+ * Sets the availability of the device for the Nearby search
233
+ *
234
+ * @param availability true when is available or false when unavailable
151
235
  */
152
- setName(name: string) {
153
- HyperTrackSdk.setName({
154
- type: 'deviceName',
155
- value: name,
156
- } as DeviceName);
236
+ static async setIsAvailable(isAvailable: boolean) {
237
+ HyperTrackSdk.setIsAvailable({
238
+ type: 'isAvailable',
239
+ value: isAvailable,
240
+ } as IsAvailable);
157
241
  }
158
242
 
159
243
  /**
160
- * Sets the metadata for the device
161
- * @param {Object} data - Metadata JSON
244
+ * Sets the tracking intent for the device
245
+ *
246
+ * @param {boolean} isTracking
162
247
  */
163
- setMetadata(data: Object) {
164
- HyperTrackSdk.setMetadata(data);
248
+ static async setIsTracking(isTracking: boolean): Promise<void> {
249
+ HyperTrackSdk.setIsTracking({
250
+ type: 'isTracking',
251
+ value: isTracking,
252
+ } as IsTracking);
165
253
  }
166
254
 
167
255
  /**
168
- * Adds a new geotag
256
+ * Sets the metadata for the device
169
257
  *
170
- * @param {Object} data - Geotad data JSON
171
- * @returns current location if success or LocationError if failure
258
+ * @param {Object} data - Metadata JSON
172
259
  */
173
- async addGeotag(data: Object): Promise<Result<Location, LocationError>>;
260
+ static async setMetadata(data: Object) {
261
+ HyperTrackSdk.setMetadata({
262
+ type: 'metadata',
263
+ value: data,
264
+ });
265
+ }
174
266
 
175
267
  /**
176
- * Adds a new geotag with expected location
268
+ * Sets the name for the device
177
269
  *
178
- * @param {Object} data - Geotad data JSON
179
- * @param {Location} expectedLocation - Expected location
180
- * @returns location with deviation if success or LocationError if failure
270
+ * @param {string} name
181
271
  */
182
- async addGeotag(
183
- data: Object,
184
- expectedLocation: Location
185
- ): Promise<Result<LocationWithDeviation, LocationError>>;
186
-
187
- async addGeotag(
188
- ...args: any[]
189
- ): Promise<Result<Location | LocationWithDeviation, LocationError>> {
190
- if (args.length === 1 && typeof args[0] === 'object') {
191
- return HyperTrackSdk.addGeotag({
192
- data: args[0],
193
- expectedLocation: undefined,
194
- } as GeotagData).then(
195
- (locationResponse: Result<LocationInternal, LocationErrorInternal>) => {
196
- return this.deserializeLocationResponse(locationResponse);
197
- }
198
- );
199
- } else if (
200
- args.length === 2 &&
201
- typeof args[0] === 'object' &&
202
- this.isLocation(args[1])
203
- ) {
204
- let expectedLocation = args[1] as Location;
205
- return HyperTrackSdk.addGeotag({
206
- data: args[0],
207
- expectedLocation: {
208
- type: 'location',
209
- value: {
210
- latitude: expectedLocation.latitude,
211
- longitude: expectedLocation.longitude,
212
- },
213
- } as LocationInternal,
214
- } as GeotagData).then(
215
- (
216
- locationResponse: Result<
217
- LocationWithDeviationInternal,
218
- LocationErrorInternal
219
- >
220
- ) => {
221
- return this.deserializeLocationWithDeviationResponse(
222
- locationResponse
223
- );
224
- }
225
- );
226
- } else {
227
- throw new Error('Invalid arguments');
228
- }
272
+ static async setName(name: string) {
273
+ HyperTrackSdk.setName({
274
+ type: 'name',
275
+ value: name,
276
+ } as Name);
229
277
  }
230
278
 
231
279
  /**
@@ -245,15 +293,45 @@ export default class HyperTrack {
245
293
  * subscription.remove()
246
294
  * ```
247
295
  */
248
- subscribeToErrors(listener: (errors: HyperTrackError[]) => void) {
296
+ static subscribeToErrors(
297
+ listener: (errors: HyperTrackError[]) => void
298
+ ): EmitterSubscription {
249
299
  return EventEmitter.addListener(
250
- EVENT_ERROR,
300
+ EVENT_ERRORS,
251
301
  (rawErrors: HyperTrackErrorInternal[]) => {
252
302
  listener(this.deserializeHyperTrackErrors(rawErrors));
253
303
  }
254
304
  );
255
305
  }
256
306
 
307
+ /**
308
+ * Subscribe to availability changes
309
+ *
310
+ * @param listener
311
+ * @returns EmitterSubscription
312
+ * @example
313
+ * ```js
314
+ * const subscription = HyperTrack.subscribeToIsAvailable(isAvailable => {
315
+ * if (isAvailable) {
316
+ * // ... ready to go
317
+ * }
318
+ * })
319
+ *
320
+ * // later, to stop listening
321
+ * subscription.remove()
322
+ * ```
323
+ */
324
+ static subscribeToIsAvailable(
325
+ listener: (isAvailable: boolean) => void
326
+ ): EmitterSubscription {
327
+ return EventEmitter.addListener(
328
+ EVENT_IS_AVAILABLE,
329
+ (isAvailable: IsAvailable) => {
330
+ listener(isAvailable.value);
331
+ }
332
+ );
333
+ }
334
+
257
335
  /**
258
336
  * Subscribe to tracking intent changes
259
337
  *
@@ -261,7 +339,7 @@ export default class HyperTrack {
261
339
  * @returns EmitterSubscription
262
340
  * @example
263
341
  * ```js
264
- * const subscription = HyperTrack.subscribeToTracking(isTracking => {
342
+ * const subscription = HyperTrack.subscribeToIsTracking(isTracking => {
265
343
  * if (isTracking) {
266
344
  * // ... ready to go
267
345
  * }
@@ -271,9 +349,11 @@ export default class HyperTrack {
271
349
  * subscription.remove()
272
350
  * ```
273
351
  */
274
- subscribeToTracking(listener: (isTracking: boolean) => void) {
352
+ static subscribeToIsTracking(
353
+ listener: (isTracking: boolean) => void
354
+ ): EmitterSubscription {
275
355
  return EventEmitter.addListener(
276
- EVENT_TRACKING,
356
+ EVENT_IS_TRACKING,
277
357
  (isTracking: IsTracking) => {
278
358
  listener(isTracking.value);
279
359
  }
@@ -281,43 +361,83 @@ export default class HyperTrack {
281
361
  }
282
362
 
283
363
  /**
284
- * Subscribe to availability changes
364
+ * Subscribe to location changes
285
365
  *
286
366
  * @param listener
287
367
  * @returns EmitterSubscription
288
368
  * @example
289
369
  * ```js
290
- * const subscription = HyperTrack.subscribeToAvailability(isAvailable => {
291
- * if (isAvailable) {
292
- * // ... ready to go
293
- * }
370
+ * const subscription = HyperTrack.subscribeToLocation(location => {
371
+ * ...
294
372
  * })
295
373
  *
296
374
  * // later, to stop listening
297
375
  * subscription.remove()
298
376
  * ```
299
377
  */
300
- subscribeToAvailability(listener: (isAvailable: boolean) => void) {
378
+ static subscribeToLocation(
379
+ listener: (location: Result<Location, LocationError>) => void
380
+ ) {
301
381
  return EventEmitter.addListener(
302
- EVENT_AVAILABILITY,
303
- (isAvailable: IsAvailable) => {
304
- listener(isAvailable.value);
382
+ EVENT_LOCATION,
383
+ (location: Result<LocationInternal, LocationErrorInternal>) => {
384
+ listener(this.deserializeLocationResponse(location));
305
385
  }
306
386
  );
307
387
  }
308
388
 
309
389
  /** @ignore */
310
- private deserializeHyperTrackErrors(
390
+ private static deserializeHyperTrackErrors(
311
391
  errors: HyperTrackErrorInternal[]
312
392
  ): HyperTrackError[] {
313
- return errors.map(
314
- (error: HyperTrackErrorInternal) =>
315
- HyperTrackError[error.value as keyof typeof HyperTrackError]
316
- );
393
+ let res = errors.map((error: HyperTrackErrorInternal) => {
394
+ if (error.type != 'error') {
395
+ throw new Error('Invalid error type');
396
+ }
397
+ return Object.keys(HyperTrackError).find(
398
+ (key) =>
399
+ HyperTrackError[key as keyof typeof HyperTrackError] === error.value
400
+ ) as HyperTrackError;
401
+ });
402
+ return res;
317
403
  }
318
404
 
319
405
  /** @ignore */
320
- private deserializeLocationResponse(
406
+ private static deserializeLocateResponse(
407
+ response: Result<LocationInternal, HyperTrackErrorInternal[]>
408
+ ): Result<Location, HyperTrackError[]> {
409
+ switch (response.type) {
410
+ case 'success':
411
+ return {
412
+ type: 'success',
413
+ value: response.value.value,
414
+ };
415
+ case 'failure':
416
+ return {
417
+ type: 'failure',
418
+ value: this.deserializeHyperTrackErrors(response.value),
419
+ };
420
+ }
421
+ }
422
+
423
+ /** @ignore */
424
+ private static deserializeLocationError(
425
+ locationError: LocationErrorInternal
426
+ ): LocationError {
427
+ switch (locationError.type) {
428
+ case 'notRunning':
429
+ case 'starting':
430
+ return locationError;
431
+ case 'errors':
432
+ return {
433
+ type: 'errors',
434
+ value: this.deserializeHyperTrackErrors(locationError.value),
435
+ };
436
+ }
437
+ }
438
+
439
+ /** @ignore */
440
+ private static deserializeLocationResponse(
321
441
  response: Result<LocationInternal, LocationErrorInternal>
322
442
  ): Result<Location, LocationError> {
323
443
  switch (response.type) {
@@ -335,7 +455,7 @@ export default class HyperTrack {
335
455
  }
336
456
 
337
457
  /** @ignore */
338
- private deserializeLocationWithDeviationResponse(
458
+ private static deserializeLocationWithDeviationResponse(
339
459
  response: Result<LocationWithDeviationInternal, LocationErrorInternal>
340
460
  ): Result<LocationWithDeviation, LocationError> {
341
461
  switch (response.type) {
@@ -361,23 +481,23 @@ export default class HyperTrack {
361
481
  }
362
482
 
363
483
  /** @ignore */
364
- private deserializeLocationError(
365
- locationError: LocationErrorInternal
366
- ): LocationError {
367
- switch (locationError.type) {
368
- case 'notRunning':
369
- case 'starting':
370
- return locationError;
371
- case 'errors':
372
- return {
373
- type: 'errors',
374
- value: this.deserializeHyperTrackErrors(locationError.value),
375
- };
484
+ private static deserializeMetadata(metadata: Metadata): Object {
485
+ if (metadata.type != 'metadata') {
486
+ throw new Error(`Invalid metadata: ${JSON.stringify(metadata)}`);
487
+ }
488
+ return metadata.value;
489
+ }
490
+
491
+ /** @ignore */
492
+ private static deserializeName(name: Name): string {
493
+ if (name.type != 'name') {
494
+ throw new Error(`Invalid name: ${JSON.stringify(name)}`);
376
495
  }
496
+ return name.value;
377
497
  }
378
498
 
379
499
  /** @ignore */
380
- private isLocation(obj: Location): obj is Location {
500
+ private static isLocation(obj: Location): obj is Location {
381
501
  return (
382
502
  'latitude' in obj &&
383
503
  typeof obj.latitude == 'number' &&