@metarouter/react-native-sdk 1.5.1 → 1.7.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.
Files changed (49) hide show
  1. package/README.md +28 -6
  2. package/android/build.gradle +41 -0
  3. package/android/src/main/AndroidManifest.xml +4 -0
  4. package/android/src/main/java/com/metarouter/reactnative/MetaRouterNetworkMonitorModule.kt +119 -0
  5. package/android/src/main/java/com/metarouter/reactnative/MetaRouterPackage.kt +23 -0
  6. package/android/src/main/java/com/metarouter/reactnative/MetaRouterQueueStorageModule.kt +124 -0
  7. package/ios/MetaRouterNetworkMonitor.m +76 -0
  8. package/ios/MetaRouterQueueStorage.m +127 -0
  9. package/lib/analytics/MetaRouterAnalyticsClient.d.ts +23 -4
  10. package/lib/analytics/MetaRouterAnalyticsClient.d.ts.map +1 -1
  11. package/lib/analytics/MetaRouterAnalyticsClient.js +178 -89
  12. package/lib/analytics/dispatcher.d.ts +57 -1
  13. package/lib/analytics/dispatcher.d.ts.map +1 -1
  14. package/lib/analytics/dispatcher.js +231 -32
  15. package/lib/analytics/init.d.ts +1 -1
  16. package/lib/analytics/init.d.ts.map +1 -1
  17. package/lib/analytics/init.js +3 -2
  18. package/lib/analytics/persistence/NativeQueueStorage.d.ts +5 -0
  19. package/lib/analytics/persistence/NativeQueueStorage.d.ts.map +1 -0
  20. package/lib/analytics/persistence/NativeQueueStorage.js +59 -0
  21. package/lib/analytics/persistence/PersistentEventQueue.d.ts +96 -0
  22. package/lib/analytics/persistence/PersistentEventQueue.d.ts.map +1 -0
  23. package/lib/analytics/persistence/PersistentEventQueue.js +401 -0
  24. package/lib/analytics/persistence/types.d.ts +20 -0
  25. package/lib/analytics/persistence/types.d.ts.map +1 -0
  26. package/lib/analytics/persistence/types.js +10 -0
  27. package/lib/analytics/proxy/proxyClient.d.ts +1 -1
  28. package/lib/analytics/proxy/proxyClient.d.ts.map +1 -1
  29. package/lib/analytics/proxy/proxyClient.js +21 -19
  30. package/lib/analytics/types.d.ts +12 -4
  31. package/lib/analytics/types.d.ts.map +1 -1
  32. package/lib/analytics/utils/circuitBreaker.d.ts +4 -1
  33. package/lib/analytics/utils/circuitBreaker.d.ts.map +1 -1
  34. package/lib/analytics/utils/circuitBreaker.js +23 -14
  35. package/lib/analytics/utils/contextInfo.d.ts +1 -1
  36. package/lib/analytics/utils/contextInfo.d.ts.map +1 -1
  37. package/lib/analytics/utils/contextInfo.js +28 -24
  38. package/lib/analytics/utils/debouncedNetworkMonitor.d.ts +25 -0
  39. package/lib/analytics/utils/debouncedNetworkMonitor.d.ts.map +1 -0
  40. package/lib/analytics/utils/debouncedNetworkMonitor.js +68 -0
  41. package/lib/analytics/utils/networkMonitor.d.ts +30 -0
  42. package/lib/analytics/utils/networkMonitor.d.ts.map +1 -0
  43. package/lib/analytics/utils/networkMonitor.js +90 -0
  44. package/lib/analytics/utils/responseCategory.d.ts +14 -0
  45. package/lib/analytics/utils/responseCategory.d.ts.map +1 -0
  46. package/lib/analytics/utils/responseCategory.js +26 -0
  47. package/metarouter-react-native-sdk.podspec +19 -0
  48. package/package.json +6 -3
  49. package/react-native.config.js +15 -0
@@ -2,7 +2,7 @@ export default class CircuitBreaker {
2
2
  constructor(opts) {
3
3
  var _a, _b, _c, _d;
4
4
  this.consecutiveFailures = 0;
5
- this.state = "CLOSED";
5
+ this.state = 'CLOSED';
6
6
  this.openUntil = 0;
7
7
  this.halfOpenInFlight = 0;
8
8
  /** how many times we've opened (drives exponential backoff) */
@@ -19,15 +19,15 @@ export default class CircuitBreaker {
19
19
  allowRequest() {
20
20
  const t = this.now();
21
21
  // Normalize OPEN→HALF_OPEN when cooldown elapsed
22
- if (this.state === "OPEN" && t >= this.openUntil) {
22
+ if (this.state === 'OPEN' && t >= this.openUntil) {
23
23
  this.halfOpenInFlight = 0;
24
- this.setState("HALF_OPEN");
24
+ this.setState('HALF_OPEN');
25
25
  }
26
- if (this.state === "OPEN") {
26
+ if (this.state === 'OPEN') {
27
27
  // Still cooling down
28
28
  return false;
29
29
  }
30
- if (this.state === "HALF_OPEN") {
30
+ if (this.state === 'HALF_OPEN') {
31
31
  if (this.halfOpenInFlight >= this.halfOpenMaxConcurrent)
32
32
  return false;
33
33
  // Admit a probe
@@ -39,11 +39,11 @@ export default class CircuitBreaker {
39
39
  }
40
40
  /** Call on successful request */
41
41
  onSuccess() {
42
- if (this.state === "HALF_OPEN") {
42
+ if (this.state === 'HALF_OPEN') {
43
43
  // One successful probe closes the circuit
44
44
  this.halfOpenInFlight = 0;
45
45
  this.openCount = 0;
46
- this.setState("CLOSED");
46
+ this.setState('CLOSED');
47
47
  }
48
48
  // Success in CLOSED resets the consecutive failure counter
49
49
  this.consecutiveFailures = 0;
@@ -51,12 +51,12 @@ export default class CircuitBreaker {
51
51
  /** Call on failed request (retryable only: network/timeout/5xx/etc.) */
52
52
  onFailure() {
53
53
  this.consecutiveFailures += 1;
54
- if (this.state === "HALF_OPEN") {
54
+ if (this.state === 'HALF_OPEN') {
55
55
  // failed probe → re-open with backoff
56
56
  this.tripOpen();
57
57
  return;
58
58
  }
59
- if (this.state === "CLOSED" &&
59
+ if (this.state === 'CLOSED' &&
60
60
  this.consecutiveFailures >= this.failureThreshold) {
61
61
  this.tripOpen();
62
62
  }
@@ -69,14 +69,23 @@ export default class CircuitBreaker {
69
69
  /** Time when requests are next allowed (ms since epoch) */
70
70
  nextAllowedAt() {
71
71
  const t = this.now();
72
- if (this.state === "OPEN")
72
+ if (this.state === 'OPEN')
73
73
  return this.openUntil;
74
- if (this.state === "HALF_OPEN") {
74
+ if (this.state === 'HALF_OPEN') {
75
75
  // Capacity-limited in HALF_OPEN; return now (admission depends on allowRequest())
76
76
  return t;
77
77
  }
78
78
  return t; // CLOSED
79
79
  }
80
+ /** Force-reset to CLOSED. Used on offline -> online transition
81
+ * to clear stale backoff from the offline period. */
82
+ reset() {
83
+ this.setState('CLOSED');
84
+ this.consecutiveFailures = 0;
85
+ this.openCount = 0;
86
+ this.openUntil = 0;
87
+ this.halfOpenInFlight = 0;
88
+ }
80
89
  /** How long until next attempt (ms) */
81
90
  remainingCooldownMs() {
82
91
  const t = this.now();
@@ -87,8 +96,8 @@ export default class CircuitBreaker {
87
96
  // Report HALF_OPEN if cooldown has elapsed,
88
97
  // but do not mutate state here — halfOpenInFlight
89
98
  // is reset when allowRequest() actually admits a probe.
90
- if (this.state === "OPEN" && this.now() >= this.openUntil) {
91
- return "HALF_OPEN";
99
+ if (this.state === 'OPEN' && this.now() >= this.openUntil) {
100
+ return 'HALF_OPEN';
92
101
  }
93
102
  return this.state;
94
103
  }
@@ -116,6 +125,6 @@ export default class CircuitBreaker {
116
125
  const jittered = backoff + (Math.random() * 2 - 1) * jitter;
117
126
  const cooldown = Math.max(0, Math.floor(jittered));
118
127
  this.openUntil = this.now() + cooldown;
119
- this.setState("OPEN", { cooldownMs: cooldown });
128
+ this.setState('OPEN', { cooldownMs: cooldown });
120
129
  }
121
130
  }
@@ -1,4 +1,4 @@
1
- import { EventContext } from "../types";
1
+ import { EventContext } from '../types';
2
2
  /**
3
3
  * Clears the cached context information, forcing a refresh on the next getContextInfo call.
4
4
  */
@@ -1 +1 @@
1
- {"version":3,"file":"contextInfo.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/contextInfo.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAYxC;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAGxC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,cAAc,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAyDlF"}
1
+ {"version":3,"file":"contextInfo.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/contextInfo.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAYxC;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAGxC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,cAAc,CAClC,aAAa,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,YAAY,CAAC,CA+DvB"}
@@ -1,11 +1,11 @@
1
- import { Dimensions, PixelRatio, Platform } from "react-native";
2
- import { getTimeZone } from "./timezone";
3
- import pkg from "../../../package.json";
1
+ import { Dimensions, PixelRatio, Platform } from 'react-native';
2
+ import { getTimeZone } from './timezone';
3
+ import pkg from '../../../package.json';
4
4
  let cachedContext = null;
5
- let cachedAdvertisingId = undefined;
5
+ let cachedAdvertisingId;
6
6
  let DeviceInfo = null;
7
7
  try {
8
- DeviceInfo = require("react-native-device-info");
8
+ DeviceInfo = require('react-native-device-info');
9
9
  }
10
10
  catch {
11
11
  DeviceInfo = null;
@@ -29,51 +29,55 @@ export function clearContextCache() {
29
29
  * @returns {Promise<EventContext>} A promise that resolves to the context information object.
30
30
  */
31
31
  export async function getContextInfo(advertisingId) {
32
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
32
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
33
33
  // Return cached context only if it exists AND the advertising ID hasn't changed
34
34
  if (cachedContext && cachedAdvertisingId === advertisingId)
35
35
  return cachedContext;
36
- let locale = "en-US";
37
- if (typeof Intl !== "undefined" &&
38
- typeof Intl.DateTimeFormat === "function") {
36
+ let locale = 'en-US';
37
+ if (typeof Intl !== 'undefined' &&
38
+ typeof Intl.DateTimeFormat === 'function') {
39
39
  const resolved = Intl.DateTimeFormat().resolvedOptions();
40
40
  locale = (_a = resolved.locale) !== null && _a !== void 0 ? _a : locale;
41
41
  }
42
- const { width, height } = Dimensions.get("screen");
42
+ const { width, height } = Dimensions.get('screen');
43
43
  const density = Number(PixelRatio.get().toFixed(2));
44
44
  cachedContext = {
45
45
  library: {
46
- name: "metarouter-react-native-sdk",
47
- version: (_b = pkg.version) !== null && _b !== void 0 ? _b : "0.0.0",
46
+ name: 'metarouter-react-native-sdk',
47
+ version: (_b = pkg.version) !== null && _b !== void 0 ? _b : '0.0.0',
48
48
  },
49
49
  locale,
50
50
  timezone: getTimeZone(),
51
51
  device: {
52
- manufacturer: (_d = (await ((_c = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getManufacturer) === null || _c === void 0 ? void 0 : _c.call(DeviceInfo)))) !== null && _d !== void 0 ? _d : "unknown",
53
- model: (_f = (_e = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getModel) === null || _e === void 0 ? void 0 : _e.call(DeviceInfo)) !== null && _f !== void 0 ? _f : "unknown",
54
- name: (_h = (await ((_g = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getDeviceName) === null || _g === void 0 ? void 0 : _g.call(DeviceInfo)))) !== null && _h !== void 0 ? _h : "unknown",
55
- type: ((_j = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getSystemName) === null || _j === void 0 ? void 0 : _j.call(DeviceInfo)) === "Android" ? "android" : "ios",
52
+ manufacturer: (_d = (await ((_c = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getManufacturer) === null || _c === void 0 ? void 0 : _c.call(DeviceInfo)))) !== null && _d !== void 0 ? _d : 'unknown',
53
+ model: Platform.OS === 'android'
54
+ ? ((_f = (_e = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getModel) === null || _e === void 0 ? void 0 : _e.call(DeviceInfo)) !== null && _f !== void 0 ? _f : 'unknown')
55
+ : ((_h = (_g = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getDeviceId) === null || _g === void 0 ? void 0 : _g.call(DeviceInfo)) !== null && _h !== void 0 ? _h : 'unknown'),
56
+ type: ((_j = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getSystemName) === null || _j === void 0 ? void 0 : _j.call(DeviceInfo)) === 'Android' ? 'android' : 'ios',
57
+ ...(Platform.OS === 'android'
58
+ ? { name: (_l = (await ((_k = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getDevice) === null || _k === void 0 ? void 0 : _k.call(DeviceInfo)))) !== null && _l !== void 0 ? _l : 'unknown' }
59
+ : {}),
56
60
  ...(advertisingId ? { advertisingId } : {}),
57
61
  },
58
62
  os: {
59
- name: (_l = (_k = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getSystemName) === null || _k === void 0 ? void 0 : _k.call(DeviceInfo)) !== null && _l !== void 0 ? _l : "unknown",
60
- version: (_o = (_m = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getSystemVersion) === null || _m === void 0 ? void 0 : _m.call(DeviceInfo)) !== null && _o !== void 0 ? _o : "unknown",
63
+ name: (_o = (_m = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getSystemName) === null || _m === void 0 ? void 0 : _m.call(DeviceInfo)) !== null && _o !== void 0 ? _o : 'unknown',
64
+ version: (_q = (_p = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getSystemVersion) === null || _p === void 0 ? void 0 : _p.call(DeviceInfo)) !== null && _q !== void 0 ? _q : 'unknown',
61
65
  },
62
66
  app: {
63
- name: (_q = (_p = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getApplicationName) === null || _p === void 0 ? void 0 : _p.call(DeviceInfo)) !== null && _q !== void 0 ? _q : "unknown",
64
- version: (_t = (_s = (_r = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getVersion) === null || _r === void 0 ? void 0 : _r.call(DeviceInfo)) !== null && _s !== void 0 ? _s : pkg.version) !== null && _t !== void 0 ? _t : "unknown",
65
- build: (_v = (_u = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getBuildNumber) === null || _u === void 0 ? void 0 : _u.call(DeviceInfo)) !== null && _v !== void 0 ? _v : "unknown",
66
- namespace: (_x = (_w = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getBundleId) === null || _w === void 0 ? void 0 : _w.call(DeviceInfo)) !== null && _x !== void 0 ? _x : "unknown",
67
+ name: (_s = (_r = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getApplicationName) === null || _r === void 0 ? void 0 : _r.call(DeviceInfo)) !== null && _s !== void 0 ? _s : 'unknown',
68
+ version: (_v = (_u = (_t = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getVersion) === null || _t === void 0 ? void 0 : _t.call(DeviceInfo)) !== null && _u !== void 0 ? _u : pkg.version) !== null && _v !== void 0 ? _v : 'unknown',
69
+ build: (_x = (_w = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getBuildNumber) === null || _w === void 0 ? void 0 : _w.call(DeviceInfo)) !== null && _x !== void 0 ? _x : 'unknown',
70
+ namespace: (_z = (_y = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.getBundleId) === null || _y === void 0 ? void 0 : _y.call(DeviceInfo)) !== null && _z !== void 0 ? _z : 'unknown',
67
71
  },
68
72
  screen: {
69
73
  width: Math.round(width),
70
74
  height: Math.round(height),
71
75
  density,
72
76
  },
73
- ...(Platform.OS === "android"
77
+ ...(Platform.OS === 'android'
74
78
  ? {
75
79
  network: {
76
- wifi: (_z = (await ((_y = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.isWifiEnabled) === null || _y === void 0 ? void 0 : _y.call(DeviceInfo)))) !== null && _z !== void 0 ? _z : false,
80
+ wifi: (_1 = (await ((_0 = DeviceInfo === null || DeviceInfo === void 0 ? void 0 : DeviceInfo.isWifiEnabled) === null || _0 === void 0 ? void 0 : _0.call(DeviceInfo)))) !== null && _1 !== void 0 ? _1 : false,
77
81
  },
78
82
  }
79
83
  : {}),
@@ -0,0 +1,25 @@
1
+ import type { NetworkReachability, NetworkStatus } from './networkMonitor';
2
+ /**
3
+ * Wraps a {@link NetworkReachability} with asymmetric debouncing:
4
+ * - **offline** transitions fire immediately (pause HTTP ASAP — no point
5
+ * burning retries on a down network).
6
+ * - **online** transitions wait for {@link ONLINE_DEBOUNCE_MS} of stable
7
+ * connectivity before firing, so flaky WiFi/cellular handoffs don't
8
+ * trigger spurious flush attempts that immediately fail.
9
+ *
10
+ * A pending online debounce is cancelled if offline arrives during the wait.
11
+ */
12
+ export declare class DebouncedNetworkMonitor implements NetworkReachability {
13
+ static readonly ONLINE_DEBOUNCE_MS = 2000;
14
+ private readonly inner;
15
+ private handler;
16
+ private unsubscribeInner;
17
+ private onlineTimer;
18
+ private _currentStatus;
19
+ constructor(inner: NetworkReachability);
20
+ get currentStatus(): NetworkStatus;
21
+ onStatusChange(handler: (status: NetworkStatus) => void): () => void;
22
+ stop(): void;
23
+ private clearOnlineTimer;
24
+ }
25
+ //# sourceMappingURL=debouncedNetworkMonitor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debouncedNetworkMonitor.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/debouncedNetworkMonitor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAE3E;;;;;;;;;GASG;AACH,qBAAa,uBAAwB,YAAW,mBAAmB;IACjE,MAAM,CAAC,QAAQ,CAAC,kBAAkB,QAAQ;IAE1C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAsB;IAC5C,OAAO,CAAC,OAAO,CAAkD;IACjE,OAAO,CAAC,gBAAgB,CAA6B;IACrD,OAAO,CAAC,WAAW,CAA8C;IACjE,OAAO,CAAC,cAAc,CAAgB;gBAE1B,KAAK,EAAE,mBAAmB;IA8BtC,IAAI,aAAa,IAAI,aAAa,CAEjC;IAED,cAAc,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI;IAOpE,IAAI,IAAI,IAAI;IAOZ,OAAO,CAAC,gBAAgB;CAMzB"}
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Wraps a {@link NetworkReachability} with asymmetric debouncing:
3
+ * - **offline** transitions fire immediately (pause HTTP ASAP — no point
4
+ * burning retries on a down network).
5
+ * - **online** transitions wait for {@link ONLINE_DEBOUNCE_MS} of stable
6
+ * connectivity before firing, so flaky WiFi/cellular handoffs don't
7
+ * trigger spurious flush attempts that immediately fail.
8
+ *
9
+ * A pending online debounce is cancelled if offline arrives during the wait.
10
+ */
11
+ export class DebouncedNetworkMonitor {
12
+ constructor(inner) {
13
+ this.handler = null;
14
+ this.unsubscribeInner = null;
15
+ this.onlineTimer = null;
16
+ this.inner = inner;
17
+ this._currentStatus = inner.currentStatus;
18
+ this.unsubscribeInner = inner.onStatusChange((rawStatus) => {
19
+ var _a;
20
+ if (rawStatus === 'disconnected') {
21
+ this.clearOnlineTimer();
22
+ if (this._currentStatus !== 'disconnected') {
23
+ this._currentStatus = 'disconnected';
24
+ (_a = this.handler) === null || _a === void 0 ? void 0 : _a.call(this, 'disconnected');
25
+ }
26
+ return;
27
+ }
28
+ // rawStatus === 'connected': start/reset the debounce window
29
+ this.clearOnlineTimer();
30
+ if (this._currentStatus === 'connected')
31
+ return;
32
+ this.onlineTimer = setTimeout(() => {
33
+ var _a;
34
+ this.onlineTimer = null;
35
+ // Guard against racing with a late offline transition.
36
+ if (this.inner.currentStatus !== 'connected')
37
+ return;
38
+ if (this._currentStatus !== 'connected') {
39
+ this._currentStatus = 'connected';
40
+ (_a = this.handler) === null || _a === void 0 ? void 0 : _a.call(this, 'connected');
41
+ }
42
+ }, DebouncedNetworkMonitor.ONLINE_DEBOUNCE_MS);
43
+ });
44
+ }
45
+ get currentStatus() {
46
+ return this._currentStatus;
47
+ }
48
+ onStatusChange(handler) {
49
+ this.handler = handler;
50
+ return () => {
51
+ this.handler = null;
52
+ };
53
+ }
54
+ stop() {
55
+ var _a;
56
+ this.clearOnlineTimer();
57
+ (_a = this.unsubscribeInner) === null || _a === void 0 ? void 0 : _a.call(this);
58
+ this.unsubscribeInner = null;
59
+ this.handler = null;
60
+ }
61
+ clearOnlineTimer() {
62
+ if (this.onlineTimer) {
63
+ clearTimeout(this.onlineTimer);
64
+ this.onlineTimer = null;
65
+ }
66
+ }
67
+ }
68
+ DebouncedNetworkMonitor.ONLINE_DEBOUNCE_MS = 2000;
@@ -0,0 +1,30 @@
1
+ export type NetworkStatus = 'connected' | 'disconnected';
2
+ export interface NetworkReachability {
3
+ /** Current connectivity snapshot */
4
+ readonly currentStatus: NetworkStatus;
5
+ /** Register callback for status transitions. Returns unsubscribe function. */
6
+ onStatusChange(handler: (status: NetworkStatus) => void): () => void;
7
+ /** Tear down monitoring */
8
+ stop(): void;
9
+ }
10
+ export declare class NetworkMonitor implements NetworkReachability {
11
+ private _currentStatus;
12
+ private handler;
13
+ private unsubscribe;
14
+ private receivedNativeEvent;
15
+ get currentStatus(): NetworkStatus;
16
+ constructor();
17
+ onStatusChange(handler: (status: NetworkStatus) => void): () => void;
18
+ stop(): void;
19
+ }
20
+ export declare class StubNetworkMonitor implements NetworkReachability {
21
+ private _currentStatus;
22
+ private handler;
23
+ get currentStatus(): NetworkStatus;
24
+ constructor(initialStatus?: NetworkStatus);
25
+ onStatusChange(handler: (status: NetworkStatus) => void): () => void;
26
+ stop(): void;
27
+ /** Simulate a network transition from tests */
28
+ simulate(status: NetworkStatus): void;
29
+ }
30
+ //# sourceMappingURL=networkMonitor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"networkMonitor.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/networkMonitor.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,cAAc,CAAC;AAEzD,MAAM,WAAW,mBAAmB;IAClC,oCAAoC;IACpC,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IACtC,8EAA8E;IAC9E,cAAc,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IACrE,2BAA2B;IAC3B,IAAI,IAAI,IAAI,CAAC;CACd;AAED,qBAAa,cAAe,YAAW,mBAAmB;IACxD,OAAO,CAAC,cAAc,CAA8B;IACpD,OAAO,CAAC,OAAO,CAAkD;IACjE,OAAO,CAAC,WAAW,CAA6B;IAChD,OAAO,CAAC,mBAAmB,CAAS;IAEpC,IAAI,aAAa,IAAI,aAAa,CAEjC;;IAqDD,cAAc,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI;IAOpE,IAAI,IAAI,IAAI;CAKb;AAED,qBAAa,kBAAmB,YAAW,mBAAmB;IAC5D,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,OAAO,CAAkD;IAEjE,IAAI,aAAa,IAAI,aAAa,CAEjC;gBAEW,aAAa,GAAE,aAA2B;IAItD,cAAc,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,GAAG,MAAM,IAAI;IAOpE,IAAI,IAAI,IAAI;IAIZ,+CAA+C;IAC/C,QAAQ,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI;CAMtC"}
@@ -0,0 +1,90 @@
1
+ import { warn } from './logger';
2
+ export class NetworkMonitor {
3
+ get currentStatus() {
4
+ return this._currentStatus;
5
+ }
6
+ constructor() {
7
+ this._currentStatus = 'connected';
8
+ this.handler = null;
9
+ this.unsubscribe = null;
10
+ this.receivedNativeEvent = false;
11
+ try {
12
+ const { NativeModules, NativeEventEmitter } = require('react-native');
13
+ const MetaRouterNetworkMonitor = NativeModules.MetaRouterNetworkMonitor;
14
+ if (!MetaRouterNetworkMonitor)
15
+ throw new Error('Native module not available');
16
+ // Get initial state from native — skip if a live event already arrived
17
+ MetaRouterNetworkMonitor.getCurrentStatus()
18
+ .then((connected) => {
19
+ var _a;
20
+ if (this.receivedNativeEvent)
21
+ return;
22
+ const newStatus = connected
23
+ ? 'connected'
24
+ : 'disconnected';
25
+ if (newStatus !== this._currentStatus) {
26
+ this._currentStatus = newStatus;
27
+ (_a = this.handler) === null || _a === void 0 ? void 0 : _a.call(this, newStatus);
28
+ }
29
+ })
30
+ .catch((err) => {
31
+ warn('Failed to get initial network status, defaulting to connected', err);
32
+ });
33
+ // Subscribe to native connectivity events
34
+ const emitter = new NativeEventEmitter(MetaRouterNetworkMonitor);
35
+ const subscription = emitter.addListener('onConnectivityChange', (event) => {
36
+ var _a;
37
+ this.receivedNativeEvent = true;
38
+ const newStatus = event.isConnected
39
+ ? 'connected'
40
+ : 'disconnected';
41
+ if (newStatus !== this._currentStatus) {
42
+ this._currentStatus = newStatus;
43
+ (_a = this.handler) === null || _a === void 0 ? void 0 : _a.call(this, newStatus);
44
+ }
45
+ });
46
+ this.unsubscribe = () => subscription.remove();
47
+ }
48
+ catch (err) {
49
+ warn('Native network monitor not available, defaulting to connected', err);
50
+ }
51
+ }
52
+ onStatusChange(handler) {
53
+ this.handler = handler;
54
+ return () => {
55
+ this.handler = null;
56
+ };
57
+ }
58
+ stop() {
59
+ var _a;
60
+ (_a = this.unsubscribe) === null || _a === void 0 ? void 0 : _a.call(this);
61
+ this.unsubscribe = null;
62
+ this.handler = null;
63
+ }
64
+ }
65
+ export class StubNetworkMonitor {
66
+ get currentStatus() {
67
+ return this._currentStatus;
68
+ }
69
+ constructor(initialStatus = 'connected') {
70
+ this.handler = null;
71
+ this._currentStatus = initialStatus;
72
+ }
73
+ onStatusChange(handler) {
74
+ this.handler = handler;
75
+ return () => {
76
+ this.handler = null;
77
+ };
78
+ }
79
+ stop() {
80
+ this.handler = null;
81
+ }
82
+ /** Simulate a network transition from tests */
83
+ simulate(status) {
84
+ var _a;
85
+ if (status !== this._currentStatus) {
86
+ this._currentStatus = status;
87
+ (_a = this.handler) === null || _a === void 0 ? void 0 : _a.call(this, status);
88
+ }
89
+ }
90
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Shared classification of HTTP status codes used by both
3
+ * the dispatcher and the disk drain.
4
+ */
5
+ export declare enum ResponseCategory {
6
+ SUCCESS = "SUCCESS",
7
+ SERVER_ERROR = "SERVER_ERROR",
8
+ RATE_LIMITED = "RATE_LIMITED",
9
+ PAYLOAD_TOO_LARGE = "PAYLOAD_TOO_LARGE",
10
+ FATAL_CONFIG = "FATAL_CONFIG",
11
+ CLIENT_ERROR = "CLIENT_ERROR"
12
+ }
13
+ export declare function categorizeResponse(statusCode: number): ResponseCategory;
14
+ //# sourceMappingURL=responseCategory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"responseCategory.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/responseCategory.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,oBAAY,gBAAgB;IAC1B,OAAO,YAAY;IACnB,YAAY,iBAAiB;IAC7B,YAAY,iBAAiB;IAC7B,iBAAiB,sBAAsB;IACvC,YAAY,iBAAiB;IAC7B,YAAY,iBAAiB;CAC9B;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,gBAAgB,CASvE"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Shared classification of HTTP status codes used by both
3
+ * the dispatcher and the disk drain.
4
+ */
5
+ export var ResponseCategory;
6
+ (function (ResponseCategory) {
7
+ ResponseCategory["SUCCESS"] = "SUCCESS";
8
+ ResponseCategory["SERVER_ERROR"] = "SERVER_ERROR";
9
+ ResponseCategory["RATE_LIMITED"] = "RATE_LIMITED";
10
+ ResponseCategory["PAYLOAD_TOO_LARGE"] = "PAYLOAD_TOO_LARGE";
11
+ ResponseCategory["FATAL_CONFIG"] = "FATAL_CONFIG";
12
+ ResponseCategory["CLIENT_ERROR"] = "CLIENT_ERROR";
13
+ })(ResponseCategory || (ResponseCategory = {}));
14
+ export function categorizeResponse(statusCode) {
15
+ if (statusCode >= 200 && statusCode < 300)
16
+ return ResponseCategory.SUCCESS;
17
+ if (statusCode >= 500 || statusCode === 408)
18
+ return ResponseCategory.SERVER_ERROR;
19
+ if (statusCode === 429)
20
+ return ResponseCategory.RATE_LIMITED;
21
+ if (statusCode === 413)
22
+ return ResponseCategory.PAYLOAD_TOO_LARGE;
23
+ if (statusCode === 401 || statusCode === 403 || statusCode === 404)
24
+ return ResponseCategory.FATAL_CONFIG;
25
+ return ResponseCategory.CLIENT_ERROR;
26
+ }
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = "metarouter-react-native-sdk"
7
+ s.version = package['version']
8
+ s.summary = "MetaRouter React Native SDK native modules"
9
+ s.homepage = package['repository']['url']
10
+ s.license = package['license']
11
+ s.author = package['author']
12
+ s.source = { :git => package['repository']['url'], :tag => s.version }
13
+
14
+ s.platform = :ios, '13.0'
15
+
16
+ s.source_files = "ios/**/*.{h,m,mm,swift}"
17
+
18
+ s.dependency "React-Core"
19
+ end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metarouter/react-native-sdk",
3
- "version": "1.5.1",
3
+ "version": "1.7.0",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "exports": {
@@ -12,7 +12,10 @@
12
12
  },
13
13
  "files": [
14
14
  "lib",
15
- "react-native.config.js"
15
+ "ios",
16
+ "android",
17
+ "react-native.config.js",
18
+ "metarouter-react-native-sdk.podspec"
16
19
  ],
17
20
  "scripts": {
18
21
  "clean": "rm -rf lib",
@@ -41,7 +44,7 @@
41
44
  "peerDependencies": {
42
45
  "@react-native-async-storage/async-storage": ">=2.2.0",
43
46
  "react": ">=16.8.0",
44
- "react-native": ">=0.63.0",
47
+ "react-native": ">=0.73.0",
45
48
  "react-native-device-info": ">=14.0.4"
46
49
  },
47
50
  "devDependencies": {
@@ -0,0 +1,15 @@
1
+ module.exports = {
2
+ dependency: {
3
+ platforms: {
4
+ ios: {
5
+ podspecPath: './metarouter-react-native-sdk.podspec',
6
+ },
7
+ android: {
8
+ sourceDir: './android',
9
+ packageImportPath:
10
+ 'import com.metarouter.reactnative.MetaRouterPackage;',
11
+ packageInstance: 'new MetaRouterPackage()',
12
+ },
13
+ },
14
+ },
15
+ };