@kafitra/react-native-live-tracking 0.1.0
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/LICENSE +21 -0
- package/README.md +396 -0
- package/android/build.gradle +71 -0
- package/android/gradle.properties +7 -0
- package/android/src/main/AndroidManifest.xml +40 -0
- package/android/src/main/java/com/livetracking/LiveTrackingModuleImpl.kt +728 -0
- package/android/src/main/java/com/livetracking/LiveTrackingPackage.kt +16 -0
- package/android/src/main/java/com/livetracking/location/LocationEngine.kt +93 -0
- package/android/src/main/java/com/livetracking/network/NetworkListener.kt +127 -0
- package/android/src/main/java/com/livetracking/optimizer/ActivityRecognitionHandler.kt +248 -0
- package/android/src/main/java/com/livetracking/optimizer/MotionSleepManager.kt +130 -0
- package/android/src/main/java/com/livetracking/permissions/PermissionHandler.kt +145 -0
- package/android/src/main/java/com/livetracking/queue/QueueEngine.kt +167 -0
- package/android/src/main/java/com/livetracking/queue/QueuedLocation.kt +16 -0
- package/android/src/main/java/com/livetracking/queue/TrackingDatabase.kt +239 -0
- package/android/src/main/java/com/livetracking/receiver/BootReceiver.kt +53 -0
- package/android/src/main/java/com/livetracking/service/TrackingForegroundService.kt +145 -0
- package/android/src/main/java/com/livetracking/sync/FirebaseSyncEngine.kt +277 -0
- package/android/src/main/java/com/livetracking/sync/LocationDataPoint.kt +31 -0
- package/android/src/main/java/com/livetracking/sync/SyncEngineController.kt +220 -0
- package/android/src/main/java/com/livetracking/sync/SyncTargetConfig.kt +20 -0
- package/android/src/main/java/com/livetracking/sync/TargetHandler.kt +601 -0
- package/android/src/newarch/java/com/livetracking/LiveTrackingModule.kt +64 -0
- package/android/src/oldarch/java/com/livetracking/LiveTrackingModule.kt +70 -0
- package/android/src/test/java/com/livetracking/BackoffCalculationTest.kt +216 -0
- package/android/src/test/java/com/livetracking/BatchAccumulatorTest.kt +391 -0
- package/android/src/test/java/com/livetracking/BootReceiverTest.kt +247 -0
- package/android/src/test/java/com/livetracking/FirebaseSyncEngineTest.kt +337 -0
- package/android/src/test/java/com/livetracking/LocationEngineTest.kt +202 -0
- package/android/src/test/java/com/livetracking/MotionSleepManagerTest.kt +420 -0
- package/android/src/test/java/com/livetracking/OfflineQueueTest.kt +462 -0
- package/android/src/test/java/com/livetracking/PermissionHandlerTest.kt +200 -0
- package/android/src/test/java/com/livetracking/QueueEngineTest.kt +335 -0
- package/android/src/test/java/com/livetracking/SyncEngineControllerTest.kt +855 -0
- package/ios/ActivityRecognitionHandler.swift +196 -0
- package/ios/BackgroundModeHelper.swift +132 -0
- package/ios/FirebaseSyncEngine.swift +276 -0
- package/ios/LiveTracking-Bridging-Header.h +2 -0
- package/ios/LiveTracking.m +37 -0
- package/ios/LiveTracking.swift +773 -0
- package/ios/LocationDataPoint.swift +56 -0
- package/ios/LocationEngine.swift +160 -0
- package/ios/MotionSleepManager.swift +151 -0
- package/ios/NetworkListener.swift +105 -0
- package/ios/OfflineQueueManager.swift +503 -0
- package/ios/PermissionHandler.swift +148 -0
- package/ios/QueueEngine.swift +249 -0
- package/ios/SyncEngineController.swift +396 -0
- package/ios/SyncTargetConfig.swift +36 -0
- package/ios/TargetHandler.swift +715 -0
- package/ios/Tests/ActivityRecognitionHandlerTests.swift +259 -0
- package/ios/Tests/FirebaseSyncEngineTests.swift +303 -0
- package/ios/Tests/LocationEngineTests.swift +244 -0
- package/ios/Tests/MotionSleepManagerTests.swift +355 -0
- package/ios/Tests/NetworkListenerTests.swift +188 -0
- package/ios/Tests/OfflineQueueFlushTests.swift +375 -0
- package/ios/Tests/PermissionHandlerTests.swift +238 -0
- package/ios/Tests/QueueEngineTests.swift +346 -0
- package/ios/TrackingCleanup.swift +93 -0
- package/ios/TrackingNotificationManager.swift +187 -0
- package/lib/commonjs/EventEmitter.js +113 -0
- package/lib/commonjs/EventEmitter.js.map +1 -0
- package/lib/commonjs/LiveTracking.js +134 -0
- package/lib/commonjs/LiveTracking.js.map +1 -0
- package/lib/commonjs/NativeLiveTracking.js +21 -0
- package/lib/commonjs/NativeLiveTracking.js.map +1 -0
- package/lib/commonjs/filters/distanceTimeFilter.js +63 -0
- package/lib/commonjs/filters/distanceTimeFilter.js.map +1 -0
- package/lib/commonjs/index.js +103 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/serialization/locationSerializer.js +51 -0
- package/lib/commonjs/serialization/locationSerializer.js.map +1 -0
- package/lib/commonjs/types.js +77 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/utils/distance.js +63 -0
- package/lib/commonjs/utils/distance.js.map +1 -0
- package/lib/commonjs/utils/retry.js +80 -0
- package/lib/commonjs/utils/retry.js.map +1 -0
- package/lib/commonjs/validation.js +463 -0
- package/lib/commonjs/validation.js.map +1 -0
- package/lib/module/EventEmitter.js +105 -0
- package/lib/module/EventEmitter.js.map +1 -0
- package/lib/module/LiveTracking.js +127 -0
- package/lib/module/LiveTracking.js.map +1 -0
- package/lib/module/NativeLiveTracking.js +16 -0
- package/lib/module/NativeLiveTracking.js.map +1 -0
- package/lib/module/filters/distanceTimeFilter.js +58 -0
- package/lib/module/filters/distanceTimeFilter.js.map +1 -0
- package/lib/module/index.js +32 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/serialization/locationSerializer.js +45 -0
- package/lib/module/serialization/locationSerializer.js.map +1 -0
- package/lib/module/types.js +94 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/distance.js +56 -0
- package/lib/module/utils/distance.js.map +1 -0
- package/lib/module/utils/retry.js +72 -0
- package/lib/module/utils/retry.js.map +1 -0
- package/lib/module/validation.js +456 -0
- package/lib/module/validation.js.map +1 -0
- package/lib/typescript/EventEmitter.d.ts +65 -0
- package/lib/typescript/EventEmitter.d.ts.map +1 -0
- package/lib/typescript/LiveTracking.d.ts +23 -0
- package/lib/typescript/LiveTracking.d.ts.map +1 -0
- package/lib/typescript/NativeLiveTracking.d.ts +25 -0
- package/lib/typescript/NativeLiveTracking.d.ts.map +1 -0
- package/lib/typescript/filters/distanceTimeFilter.d.ts +44 -0
- package/lib/typescript/filters/distanceTimeFilter.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +21 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/serialization/locationSerializer.d.ts +39 -0
- package/lib/typescript/serialization/locationSerializer.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +217 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/utils/distance.d.ts +38 -0
- package/lib/typescript/utils/distance.d.ts.map +1 -0
- package/lib/typescript/utils/retry.d.ts +60 -0
- package/lib/typescript/utils/retry.d.ts.map +1 -0
- package/lib/typescript/validation.d.ts +26 -0
- package/lib/typescript/validation.d.ts.map +1 -0
- package/package.json +126 -0
- package/react-native-live-tracking.podspec +47 -0
- package/src/EventEmitter.ts +118 -0
- package/src/LiveTracking.ts +159 -0
- package/src/NativeLiveTracking.ts +29 -0
- package/src/filters/distanceTimeFilter.ts +75 -0
- package/src/index.ts +51 -0
- package/src/serialization/locationSerializer.ts +57 -0
- package/src/types.ts +252 -0
- package/src/utils/distance.ts +68 -0
- package/src/utils/retry.ts +75 -0
- package/src/validation.ts +552 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.serializeLocationForTarget = serializeLocationForTarget;
|
|
7
|
+
/**
|
|
8
|
+
* Location serialization for Firebase sync targets.
|
|
9
|
+
*
|
|
10
|
+
* Transforms LocationData into a generic payload suitable for any
|
|
11
|
+
* sync target, regardless of write method (set, push, update).
|
|
12
|
+
*
|
|
13
|
+
* @packageDocumentation
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// ─── Payload Interface ───────────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Serialized location payload for any sync target.
|
|
20
|
+
*
|
|
21
|
+
* Contains required fields (latitude, longitude, timestamp, accuracy)
|
|
22
|
+
* and optional sensor fields (speed, heading, altitude) which are
|
|
23
|
+
* included as `null` when unavailable from the device sensor.
|
|
24
|
+
* Placeholder values (0, -1) are never used for unavailable fields.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
// ─── Serialization Function ──────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Serialize a LocationData object for a generic Firebase sync target.
|
|
31
|
+
*
|
|
32
|
+
* Produces a payload containing all required location fields and optional
|
|
33
|
+
* sensor fields. When optional fields (speed, heading/bearing, altitude)
|
|
34
|
+
* are unavailable from the device sensor, they are included as `null`.
|
|
35
|
+
* Default placeholder values (0, -1) are never written for unavailable fields.
|
|
36
|
+
*
|
|
37
|
+
* @param location - The location data to serialize
|
|
38
|
+
* @returns Serialized payload for the sync target
|
|
39
|
+
*/
|
|
40
|
+
function serializeLocationForTarget(location) {
|
|
41
|
+
return {
|
|
42
|
+
latitude: location.latitude,
|
|
43
|
+
longitude: location.longitude,
|
|
44
|
+
timestamp: location.timestamp,
|
|
45
|
+
accuracy: location.accuracy,
|
|
46
|
+
speed: location.speed,
|
|
47
|
+
heading: location.bearing,
|
|
48
|
+
altitude: location.altitude
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=locationSerializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["serializeLocationForTarget","location","latitude","longitude","timestamp","accuracy","speed","heading","bearing","altitude"],"sourceRoot":"../../../src","sources":["serialization/locationSerializer.ts"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAIA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAWA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,0BAA0BA,CACxCC,QAAsB,EACC;EACvB,OAAO;IACLC,QAAQ,EAAED,QAAQ,CAACC,QAAQ;IAC3BC,SAAS,EAAEF,QAAQ,CAACE,SAAS;IAC7BC,SAAS,EAAEH,QAAQ,CAACG,SAAS;IAC7BC,QAAQ,EAAEJ,QAAQ,CAACI,QAAQ;IAC3BC,KAAK,EAAEL,QAAQ,CAACK,KAAK;IACrBC,OAAO,EAAEN,QAAQ,CAACO,OAAO;IACzBC,QAAQ,EAAER,QAAQ,CAACQ;EACrB,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.TrackingState = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* TypeScript type definitions for react-native-live-tracking.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
// ─── Enums ───────────────────────────────────────────────────────────────────
|
|
13
|
+
/**
|
|
14
|
+
* Internal state machine for tracking lifecycle.
|
|
15
|
+
*/
|
|
16
|
+
let TrackingState = exports.TrackingState = /*#__PURE__*/function (TrackingState) {
|
|
17
|
+
TrackingState["IDLE"] = "idle";
|
|
18
|
+
TrackingState["CONFIGURED"] = "configured";
|
|
19
|
+
TrackingState["TRACKING"] = "tracking";
|
|
20
|
+
TrackingState["MOTION_SLEEP"] = "motion_sleep";
|
|
21
|
+
TrackingState["PAUSED_GPS"] = "paused_gps";
|
|
22
|
+
return TrackingState;
|
|
23
|
+
}({}); // ─── Configuration Interfaces ────────────────────────────────────────────────
|
|
24
|
+
/**
|
|
25
|
+
* Strategy used by the distance/time filter.
|
|
26
|
+
* - 'interval': accept when enough time has elapsed
|
|
27
|
+
* - 'distance': accept when enough distance has been covered
|
|
28
|
+
* - 'both': accept only when both conditions are met (default)
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* Optimization parameters for location tracking.
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* Android foreground service notification configuration.
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* iOS persistent notification configuration.
|
|
38
|
+
* Shows a local notification while tracking is active (similar to Android's foreground service notification).
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* A user-defined sync target specifying a Firebase path, write method,
|
|
42
|
+
* and optional batching/offline queue settings.
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Firebase connection and sync target configuration.
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* Main configuration object passed to `configure()`.
|
|
49
|
+
*/
|
|
50
|
+
// ─── Data Interfaces ─────────────────────────────────────────────────────────
|
|
51
|
+
/**
|
|
52
|
+
* Location data emitted by the library on each valid location update.
|
|
53
|
+
*/
|
|
54
|
+
// ─── Error & Status Interfaces ───────────────────────────────────────────────
|
|
55
|
+
/**
|
|
56
|
+
* Error object emitted by the library when tracking issues occur.
|
|
57
|
+
*/
|
|
58
|
+
/**
|
|
59
|
+
* Current tracking status snapshot.
|
|
60
|
+
*/
|
|
61
|
+
// ─── Validation Interfaces ───────────────────────────────────────────────────
|
|
62
|
+
/**
|
|
63
|
+
* Single configuration validation error.
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Result of configuration validation.
|
|
67
|
+
*/
|
|
68
|
+
// ─── Utility Types ───────────────────────────────────────────────────────────
|
|
69
|
+
/**
|
|
70
|
+
* Subscription handle returned by event listener registrations.
|
|
71
|
+
* Call `remove()` to unsubscribe from the event.
|
|
72
|
+
*/
|
|
73
|
+
// ─── Module Interface ────────────────────────────────────────────────────────
|
|
74
|
+
/**
|
|
75
|
+
* Main public API interface for the react-native-live-tracking library.
|
|
76
|
+
*/
|
|
77
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TrackingState","exports"],"sourceRoot":"../../src","sources":["types.ts"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AAEA;AAEA;AACA;AACA;AAFA,IAGYA,aAAa,GAAAC,OAAA,CAAAD,aAAA,0BAAbA,aAAa;EAAbA,aAAa;EAAbA,aAAa;EAAbA,aAAa;EAAbA,aAAa;EAAbA,aAAa;EAAA,OAAbA,aAAa;AAAA,OAQzB;AAEA;AACA;AACA;AACA;AACA;AACA;AAGA;AACA;AACA;AAYA;AACA;AACA;AAgBA;AACA;AACA;AACA;AAUA;AACA;AACA;AACA;AAYA;AACA;AACA;AAQA;AACA;AACA;AAYA;AAEA;AACA;AACA;AAkBA;AAEA;AACA;AACA;AAYA;AACA;AACA;AAcA;AAEA;AACA;AACA;AAUA;AACA;AACA;AAQA;AAEA;AACA;AACA;AACA;AAMA;AAEA;AACA;AACA","ignoreList":[]}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.EARTH_RADIUS_METERS = void 0;
|
|
7
|
+
exports.calculateDistance = calculateDistance;
|
|
8
|
+
/**
|
|
9
|
+
* Distance calculation utility using the Haversine formula.
|
|
10
|
+
*
|
|
11
|
+
* The Haversine formula determines the great-circle distance between two points
|
|
12
|
+
* on a sphere given their longitudes and latitudes. This is used by the
|
|
13
|
+
* Distance/Time Matrix filter to determine if a new location update has moved
|
|
14
|
+
* far enough from the last known position.
|
|
15
|
+
*
|
|
16
|
+
* @packageDocumentation
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Earth's mean radius in meters.
|
|
21
|
+
* Used as the sphere radius in the Haversine formula.
|
|
22
|
+
*/
|
|
23
|
+
const EARTH_RADIUS_METERS = exports.EARTH_RADIUS_METERS = 6371000;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Converts degrees to radians.
|
|
27
|
+
*
|
|
28
|
+
* @param degrees - Angle in degrees
|
|
29
|
+
* @returns Angle in radians
|
|
30
|
+
*/
|
|
31
|
+
function toRadians(degrees) {
|
|
32
|
+
return degrees * (Math.PI / 180);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Calculates the distance in meters between two geographic coordinates
|
|
37
|
+
* using the Haversine formula.
|
|
38
|
+
*
|
|
39
|
+
* The Haversine formula accounts for the curvature of the Earth and provides
|
|
40
|
+
* accurate results for short and medium distances. It assumes a spherical Earth
|
|
41
|
+
* with a mean radius of 6,371,000 meters.
|
|
42
|
+
*
|
|
43
|
+
* @param lat1 - Latitude of the first point in degrees (-90 to 90)
|
|
44
|
+
* @param lng1 - Longitude of the first point in degrees (-180 to 180)
|
|
45
|
+
* @param lat2 - Latitude of the second point in degrees (-90 to 90)
|
|
46
|
+
* @param lng2 - Longitude of the second point in degrees (-180 to 180)
|
|
47
|
+
* @returns Distance between the two points in meters
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* // Distance between Jakarta and Bandung (approximately 120 km)
|
|
52
|
+
* const distance = calculateDistance(-6.2088, 106.8456, -6.9175, 107.6191);
|
|
53
|
+
* console.log(distance); // ~120,000 meters
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
function calculateDistance(lat1, lng1, lat2, lng2) {
|
|
57
|
+
const dLat = toRadians(lat2 - lat1);
|
|
58
|
+
const dLng = toRadians(lng2 - lng1);
|
|
59
|
+
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
|
60
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
61
|
+
return EARTH_RADIUS_METERS * c;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=distance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["EARTH_RADIUS_METERS","exports","toRadians","degrees","Math","PI","calculateDistance","lat1","lng1","lat2","lng2","dLat","dLng","a","sin","cos","c","atan2","sqrt"],"sourceRoot":"../../../src","sources":["utils/distance.ts"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACO,MAAMA,mBAAmB,GAAAC,OAAA,CAAAD,mBAAA,GAAG,OAAO;;AAE1C;AACA;AACA;AACA;AACA;AACA;AACA,SAASE,SAASA,CAACC,OAAe,EAAU;EAC1C,OAAOA,OAAO,IAAIC,IAAI,CAACC,EAAE,GAAG,GAAG,CAAC;AAClC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,iBAAiBA,CAC/BC,IAAY,EACZC,IAAY,EACZC,IAAY,EACZC,IAAY,EACJ;EACR,MAAMC,IAAI,GAAGT,SAAS,CAACO,IAAI,GAAGF,IAAI,CAAC;EACnC,MAAMK,IAAI,GAAGV,SAAS,CAACQ,IAAI,GAAGF,IAAI,CAAC;EAEnC,MAAMK,CAAC,GACLT,IAAI,CAACU,GAAG,CAACH,IAAI,GAAG,CAAC,CAAC,GAAGP,IAAI,CAACU,GAAG,CAACH,IAAI,GAAG,CAAC,CAAC,GACvCP,IAAI,CAACW,GAAG,CAACb,SAAS,CAACK,IAAI,CAAC,CAAC,GACvBH,IAAI,CAACW,GAAG,CAACb,SAAS,CAACO,IAAI,CAAC,CAAC,GACzBL,IAAI,CAACU,GAAG,CAACF,IAAI,GAAG,CAAC,CAAC,GAClBR,IAAI,CAACU,GAAG,CAACF,IAAI,GAAG,CAAC,CAAC;EAEtB,MAAMI,CAAC,GAAG,CAAC,GAAGZ,IAAI,CAACa,KAAK,CAACb,IAAI,CAACc,IAAI,CAACL,CAAC,CAAC,EAAET,IAAI,CAACc,IAAI,CAAC,CAAC,GAAGL,CAAC,CAAC,CAAC;EAExD,OAAOb,mBAAmB,GAAGgB,CAAC;AAChC","ignoreList":[]}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.MAX_RETRIES_SET_UPDATE = exports.MAX_RETRIES_PUSH = exports.DEFAULT_MAX_JITTER = exports.DEFAULT_BASE_DELAY = void 0;
|
|
7
|
+
exports.calculateBackoffDelay = calculateBackoffDelay;
|
|
8
|
+
exports.shouldRetry = shouldRetry;
|
|
9
|
+
/**
|
|
10
|
+
* Exponential backoff retry utilities for Firebase write operations.
|
|
11
|
+
*
|
|
12
|
+
* Used by the Firebase Sync Engine to determine delay between retry attempts
|
|
13
|
+
* when writes fail due to network issues or transient errors.
|
|
14
|
+
*
|
|
15
|
+
* @module utils/retry
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** Default base delay in milliseconds for exponential backoff */
|
|
19
|
+
const DEFAULT_BASE_DELAY = exports.DEFAULT_BASE_DELAY = 1000;
|
|
20
|
+
|
|
21
|
+
/** Default maximum jitter in milliseconds (±) to avoid thundering herd */
|
|
22
|
+
const DEFAULT_MAX_JITTER = exports.DEFAULT_MAX_JITTER = 200;
|
|
23
|
+
|
|
24
|
+
/** Maximum retry attempts for sync targets using 'set' or 'update' write methods */
|
|
25
|
+
const MAX_RETRIES_SET_UPDATE = exports.MAX_RETRIES_SET_UPDATE = 3;
|
|
26
|
+
|
|
27
|
+
/** Maximum retry attempts for sync targets using 'push' write method */
|
|
28
|
+
const MAX_RETRIES_PUSH = exports.MAX_RETRIES_PUSH = 5;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Calculates the delay before the next retry attempt using exponential backoff
|
|
32
|
+
* with random jitter.
|
|
33
|
+
*
|
|
34
|
+
* Formula: delay = baseDelay × 2^(attempt - 1) + random(-maxJitter, +maxJitter)
|
|
35
|
+
*
|
|
36
|
+
* The result is clamped to a minimum of 0 to prevent negative delays.
|
|
37
|
+
*
|
|
38
|
+
* @param attempt - The retry attempt number (1-indexed, first retry = 1)
|
|
39
|
+
* @param baseDelay - The base delay in milliseconds (default: 1000ms)
|
|
40
|
+
* @param maxJitter - The maximum jitter offset in milliseconds (default: 200ms).
|
|
41
|
+
* A random value between -maxJitter and +maxJitter is added to the delay.
|
|
42
|
+
* @returns The calculated delay in milliseconds (never negative)
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // First retry: ~1000ms ± 200ms
|
|
47
|
+
* const delay1 = calculateBackoffDelay(1);
|
|
48
|
+
*
|
|
49
|
+
* // Second retry: ~2000ms ± 200ms
|
|
50
|
+
* const delay2 = calculateBackoffDelay(2);
|
|
51
|
+
*
|
|
52
|
+
* // Third retry: ~4000ms ± 200ms
|
|
53
|
+
* const delay3 = calculateBackoffDelay(3);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
function calculateBackoffDelay(attempt, baseDelay = DEFAULT_BASE_DELAY, maxJitter = DEFAULT_MAX_JITTER) {
|
|
57
|
+
const exponentialDelay = baseDelay * Math.pow(2, attempt - 1);
|
|
58
|
+
const jitter = (Math.random() * 2 - 1) * maxJitter;
|
|
59
|
+
return Math.max(0, exponentialDelay + jitter);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Determines whether a retry should be attempted based on the current attempt
|
|
64
|
+
* number and the maximum allowed retries.
|
|
65
|
+
*
|
|
66
|
+
* @param attempt - The current attempt number (1-indexed)
|
|
67
|
+
* @param maxRetries - The maximum number of retries allowed
|
|
68
|
+
* @returns `true` if the attempt is within the allowed retry limit, `false` otherwise
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* shouldRetry(1, 3); // true - first retry is allowed
|
|
73
|
+
* shouldRetry(3, 3); // true - third retry is allowed
|
|
74
|
+
* shouldRetry(4, 3); // false - exceeds max retries
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function shouldRetry(attempt, maxRetries) {
|
|
78
|
+
return attempt <= maxRetries;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["DEFAULT_BASE_DELAY","exports","DEFAULT_MAX_JITTER","MAX_RETRIES_SET_UPDATE","MAX_RETRIES_PUSH","calculateBackoffDelay","attempt","baseDelay","maxJitter","exponentialDelay","Math","pow","jitter","random","max","shouldRetry","maxRetries"],"sourceRoot":"../../../src","sources":["utils/retry.ts"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACO,MAAMA,kBAAkB,GAAAC,OAAA,CAAAD,kBAAA,GAAG,IAAI;;AAEtC;AACO,MAAME,kBAAkB,GAAAD,OAAA,CAAAC,kBAAA,GAAG,GAAG;;AAErC;AACO,MAAMC,sBAAsB,GAAAF,OAAA,CAAAE,sBAAA,GAAG,CAAC;;AAEvC;AACO,MAAMC,gBAAgB,GAAAH,OAAA,CAAAG,gBAAA,GAAG,CAAC;;AAEjC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,qBAAqBA,CACnCC,OAAe,EACfC,SAAiB,GAAGP,kBAAkB,EACtCQ,SAAiB,GAAGN,kBAAkB,EAC9B;EACR,MAAMO,gBAAgB,GAAGF,SAAS,GAAGG,IAAI,CAACC,GAAG,CAAC,CAAC,EAAEL,OAAO,GAAG,CAAC,CAAC;EAC7D,MAAMM,MAAM,GAAG,CAACF,IAAI,CAACG,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,IAAIL,SAAS;EAClD,OAAOE,IAAI,CAACI,GAAG,CAAC,CAAC,EAAEL,gBAAgB,GAAGG,MAAM,CAAC;AAC/C;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASG,WAAWA,CAACT,OAAe,EAAEU,UAAkB,EAAW;EACxE,OAAOV,OAAO,IAAIU,UAAU;AAC9B","ignoreList":[]}
|