@metarouter/react-native-sdk 1.5.0 → 1.5.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/README.md CHANGED
@@ -147,6 +147,33 @@ analytics.flush();
147
147
  await analytics.reset();
148
148
  ```
149
149
 
150
+ ### Reconfiguring the Analytics Client
151
+
152
+ ⚠️ **Important**: If you need to change the analytics configuration (e.g., different `writeKey` or `ingestionHost`), you must call `reset()` first:
153
+
154
+ ```js
155
+ // Initial configuration
156
+ const analytics = await createAnalyticsClient({
157
+ writeKey: "key-1",
158
+ ingestionHost: "https://endpoint-1.com",
159
+ });
160
+
161
+ // Later, if you need to change configuration:
162
+ await analytics.reset(); // Must reset first!
163
+
164
+ // Now reconfigure with new options
165
+ const analytics = await createAnalyticsClient({
166
+ writeKey: "key-2",
167
+ ingestionHost: "https://endpoint-2.com",
168
+ });
169
+ ```
170
+
171
+ If you call `createAnalyticsClient()` with different options without resetting first, you'll see this warning:
172
+
173
+ ```
174
+ [MetaRouter] Config changed but client not reset. Call await client.reset() before reinitializing with new options.
175
+ ```
176
+
150
177
  ## API Reference
151
178
 
152
179
  ### createAnalyticsClient(options)
@@ -210,13 +237,13 @@ The analytics client provides the following methods:
210
237
 
211
238
  ## ✅ Compatibility
212
239
 
213
- | Component | Supported Versions |
214
- | --------------------- | ------------------ |
215
- | React Native | >= 0.63 |
216
- | React | >= 16.8 |
217
- | iOS Deployment Target | >= iOS 10 |
218
- | Android Min SDK | >= API 16 |
219
- | Node.js | >= 16 |
240
+ | Component | Supported Versions |
241
+ | -------------------------- | ------------------ |
242
+ | React Native | >= 0.63 |
243
+ | React | >= 16.8 |
244
+ | iOS Deployment Target | >= iOS 13 |
245
+ | Android Min SDK | >= API 21 |
246
+ | Node.js | >= 16 |
220
247
 
221
248
  ## Debugging
222
249
 
@@ -1,6 +1,6 @@
1
- import type { EventPayload } from "./types";
2
- import type CircuitBreaker from "./utils/circuitBreaker";
3
- export type CircuitState = "CLOSED" | "OPEN" | "HALF_OPEN";
1
+ import type { EventPayload } from './types';
2
+ import type CircuitBreaker from './utils/circuitBreaker';
3
+ export type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';
4
4
  export interface DispatcherOptions {
5
5
  maxQueueEvents: number;
6
6
  autoFlushThreshold: number;
@@ -26,6 +26,7 @@ export default class Dispatcher {
26
26
  private nextScheduledAt;
27
27
  private flushInFlight;
28
28
  private maxBatchSize;
29
+ private readonly initialMaxBatchSize;
29
30
  private circuit;
30
31
  constructor(opts: DispatcherOptions);
31
32
  getQueueRef(): EventPayload[];
@@ -1 +1 @@
1
- {"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../../src/analytics/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,cAAc,MAAM,wBAAwB,CAAC;AAEzD,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAE3D,MAAM,WAAW,iBAAiB;IAChC,cAAc,EAAE,MAAM,CAAC;IACvB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,oBAAoB,EAAE,MAAM,CAAC;IAE7B,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;IACnC,gBAAgB,EAAE,CAChB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,WAAW,EACjB,SAAS,EAAE,MAAM,KACd,OAAO,CAAC,QAAQ,CAAC,CAAC;IACvB,OAAO,EAAE,MAAM,OAAO,CAAC;IACvB,aAAa,EAAE,MAAM,OAAO,CAAC;IAC7B,gBAAgB,EAAE,MAAM,OAAO,CAAC;IAChC,aAAa,EAAE,MAAM,cAAc,CAAC;IAEpC,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAC9B,IAAI,EAAE,CAAC,GAA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;IAQb,IAAI,IAAI,IAAI;IAQZ,KAAK,IAAI,IAAI;IAOb,OAAO,CAAC,cAAc;IAQtB,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;IAkBjC,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAuBlC,OAAO,CAAC,UAAU;IAUlB,OAAO,CAAC,eAAe;IAQjB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAiK5B,YAAY;;;;;;;;;;CAYb"}
@@ -6,7 +6,8 @@ export default class Dispatcher {
6
6
  this.nextScheduledAt = null;
7
7
  this.flushInFlight = null;
8
8
  this.opts = opts;
9
- this.maxBatchSize = Math.max(1, opts.maxBatchSize);
9
+ this.initialMaxBatchSize = Math.max(1, opts.maxBatchSize);
10
+ this.maxBatchSize = this.initialMaxBatchSize;
10
11
  this.circuit = opts.createBreaker();
11
12
  }
12
13
  getQueueRef() {
@@ -28,7 +29,7 @@ export default class Dispatcher {
28
29
  this.stop();
29
30
  this.queue.length = 0;
30
31
  this.circuit = this.opts.createBreaker();
31
- this.maxBatchSize = Math.max(1, this.opts.maxBatchSize);
32
+ this.maxBatchSize = this.initialMaxBatchSize;
32
33
  }
33
34
  clearNextTimer() {
34
35
  if (this.nextTimer) {
@@ -58,7 +59,7 @@ export default class Dispatcher {
58
59
  this.queue.shift();
59
60
  this.opts.warn(`[MetaRouter] Queue cap ${this.opts.maxQueueEvents} reached — dropped oldest event`);
60
61
  }
61
- this.opts.log("Enqueuing event", {
62
+ this.opts.log('Enqueuing event', {
62
63
  type: event === null || event === void 0 ? void 0 : event.type,
63
64
  messageId: event === null || event === void 0 ? void 0 : event.messageId,
64
65
  });
@@ -105,15 +106,15 @@ export default class Dispatcher {
105
106
  }
106
107
  const chunk = this.drainBatch();
107
108
  try {
108
- this.opts.log("Making API call to:", this.opts.endpoint("/v1/batch"));
109
+ this.opts.log('Making API call to:', this.opts.endpoint('/v1/batch'));
109
110
  const headers = {
110
- "Content-Type": "application/json"
111
+ 'Content-Type': 'application/json',
111
112
  };
112
113
  if (this.opts.isTracingEnabled()) {
113
- headers["Trace"] = "true";
114
+ headers.Trace = 'true';
114
115
  }
115
- const res = await this.opts.fetchWithTimeout(this.opts.endpoint("/v1/batch"), {
116
- method: "POST",
116
+ const res = await this.opts.fetchWithTimeout(this.opts.endpoint('/v1/batch'), {
117
+ method: 'POST',
117
118
  headers,
118
119
  body: JSON.stringify({ batch: chunk }),
119
120
  }, 8000);
@@ -124,13 +125,13 @@ export default class Dispatcher {
124
125
  this.circuit.onFailure();
125
126
  if (this.opts.isOperational()) {
126
127
  this.queue.unshift(...chunk);
127
- const retryAfter = (_a = this.parseRetryAfter(res.headers.get("retry-after"))) !== null && _a !== void 0 ? _a : 0;
128
+ const retryAfter = (_a = this.parseRetryAfter(res.headers.get('retry-after'))) !== null && _a !== void 0 ? _a : 0;
128
129
  const wait = Math.max(this.circuit.remainingCooldownMs(), retryAfter, 1000);
129
130
  this.opts.warn(`Retryable status ${s}; scheduling retry in ${wait}ms`);
130
131
  this.scheduleFlushIn(wait);
131
132
  }
132
133
  else {
133
- this.opts.warn("Flush aborted during reset/disable — dropping current chunk");
134
+ this.opts.warn('Flush aborted during reset/disable — dropping current chunk');
134
135
  }
135
136
  return;
136
137
  }
@@ -138,14 +139,14 @@ export default class Dispatcher {
138
139
  this.circuit.onFailure();
139
140
  if (this.opts.isOperational()) {
140
141
  this.queue.unshift(...chunk);
141
- const h = (_b = this.parseRetryAfter(res.headers.get("retry-after"))) !== null && _b !== void 0 ? _b : 0;
142
+ const h = (_b = this.parseRetryAfter(res.headers.get('retry-after'))) !== null && _b !== void 0 ? _b : 0;
142
143
  const b = this.circuit.remainingCooldownMs();
143
144
  const wait = Math.max(h, b, 1000);
144
145
  this.opts.warn(`429 throttled; scheduling in ${wait}ms (retry-after=${h}, breaker=${b})`);
145
146
  this.scheduleFlushIn(wait);
146
147
  }
147
148
  else {
148
- this.opts.warn("Throttle received but client resetting/disabled — dropping chunk");
149
+ this.opts.warn('Throttle received but client resetting/disabled — dropping chunk');
149
150
  }
150
151
  return;
151
152
  }
@@ -167,7 +168,7 @@ export default class Dispatcher {
167
168
  else {
168
169
  const ids = chunk
169
170
  .map((e) => e.messageId)
170
- .join(",");
171
+ .join(',');
171
172
  this.opts.warn(`Dropping oversize event(s) after 413 at batchSize=1; messageIds=${ids}`);
172
173
  }
173
174
  return;
@@ -179,23 +180,26 @@ export default class Dispatcher {
179
180
  }
180
181
  // Success
181
182
  this.circuit.onSuccess();
182
- this.opts.log("API call successful");
183
+ if (this.maxBatchSize < this.initialMaxBatchSize) {
184
+ this.maxBatchSize = Math.min(this.maxBatchSize * 2, this.initialMaxBatchSize);
185
+ }
186
+ this.opts.log('API call successful');
183
187
  }
184
188
  catch (err) {
185
189
  this.circuit.onFailure();
186
190
  if (this.opts.isOperational()) {
187
191
  this.queue.unshift(...chunk);
188
192
  const wait = Math.max(this.circuit.remainingCooldownMs(), 1000);
189
- this.opts.warn("Flush attempt failed; scheduling retry in", wait, "ms", err === null || err === void 0 ? void 0 : err.message);
193
+ this.opts.warn('Flush attempt failed; scheduling retry in', wait, 'ms', err === null || err === void 0 ? void 0 : err.message);
190
194
  this.scheduleFlushIn(wait);
191
195
  }
192
196
  else {
193
- this.opts.warn("Flush failed during reset/disable — dropping current chunk");
197
+ this.opts.warn('Flush failed during reset/disable — dropping current chunk');
194
198
  }
195
199
  return;
196
200
  }
197
201
  }
198
- this.opts.log("Flush completed successfully");
202
+ this.opts.log('Flush completed successfully');
199
203
  };
200
204
  this.flushInFlight = doFlush().finally(() => {
201
205
  this.flushInFlight = null;
@@ -1 +1 @@
1
- {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/analytics/init.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAK/D,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,WAAW,GACnB,kBAAkB,CA4BpB"}
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/analytics/init.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAM/D,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,WAAW,GACnB,kBAAkB,CAwCpB"}
@@ -2,8 +2,16 @@ import { MetaRouterAnalyticsClient } from "./MetaRouterAnalyticsClient";
2
2
  import { proxyClient, setRealClient } from "./proxy/proxyClient";
3
3
  // Only one initialization in flight
4
4
  let initPromise = null;
5
+ let currentOptions = null;
5
6
  export function createAnalyticsClient(options) {
7
+ // Check if options have changed - if so, force reset first
8
+ const optionsChanged = currentOptions &&
9
+ JSON.stringify(currentOptions) !== JSON.stringify(options);
10
+ if (optionsChanged && initPromise) {
11
+ console.warn('[MetaRouter] Config changed but client not reset. Call await client.reset() before reinitializing with new options.');
12
+ }
6
13
  if (!initPromise) {
14
+ currentOptions = options;
7
15
  initPromise = (async () => {
8
16
  const instance = new MetaRouterAnalyticsClient(options);
9
17
  await instance.init();
@@ -24,6 +32,7 @@ export function createAnalyticsClient(options) {
24
32
  await instance.reset();
25
33
  setRealClient(null, { dropPending: true });
26
34
  initPromise = null;
35
+ currentOptions = null;
27
36
  },
28
37
  };
29
38
  setRealClient(boundClient);
@@ -1 +1 @@
1
- {"version":3,"file":"proxyClient.d.ts","sourceRoot":"","sources":["../../../src/analytics/proxy/proxyClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AA8InD,eAAO,MAAM,WAAW,EAAE,kBAezB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,kBAAkB,GAAG,IAAI,EACjC,IAAI,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,QAqBjC"}
1
+ {"version":3,"file":"proxyClient.d.ts","sourceRoot":"","sources":["../../../src/analytics/proxy/proxyClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAqInD,eAAO,MAAM,WAAW,EAAE,kBAezB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,aAAa,CAC3B,MAAM,EAAE,kBAAkB,GAAG,IAAI,EACjC,IAAI,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,QAqBjC"}
@@ -21,11 +21,10 @@ let flushInFlight = null;
21
21
  *
22
22
  * - **Pre-bind:**
23
23
  * - Queues the call in `pendingCalls` for replay after binding.
24
- * - For async methods (`flush`, `reset`, `getDebugInfo`), returns a Promise
25
- * that resolves/rejects after replay. Keeps a `reject` handle so
24
+ * - For async methods (`flush`, `getDebugInfo`, `setAdvertisingId`, `clearAdvertisingId`),
25
+ * returns a Promise that resolves/rejects after replay. Keeps a `reject` handle so
26
26
  * `dropPending` can fail them immediately.
27
27
  * - For sync "fire-and-forget" methods, returns `void` immediately.
28
- * - For `getDebugInfo`, returns an immediate proxy snapshot without queuing.
29
28
  * - For `reset`, returns an immediate resolved Promise (no-op).
30
29
  * - If the queue exceeds `MAX_PENDING_CALLS`, drops the oldest call
31
30
  * (rejecting it if async) and logs a warning.
@@ -56,13 +55,6 @@ function handleMethodCall(methodName, ...args) {
56
55
  console.warn(`[MetaRouter] Oldest call dropped (queue cap ${MAX_PENDING_CALLS})`);
57
56
  }
58
57
  // Special-cases while unbound:
59
- if (methodName === "getDebugInfo") {
60
- // Return immediate proxy snapshot instead of queueing
61
- return Promise.resolve({
62
- proxy: true,
63
- pendingCalls: pendingCalls.length,
64
- });
65
- }
66
58
  if (methodName === "reset") {
67
59
  // Nothing to reset pre-bind; resolve immediately
68
60
  return Promise.resolve();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metarouter/react-native-sdk",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "scripts": {
18
18
  "clean": "rm -rf lib",
19
19
  "build": "tsc --project tsconfig.build.json",
20
- "prepare": "npm run build",
20
+ "prepare": "husky",
21
21
  "prepublishOnly": "npm run clean && npm run build",
22
22
  "test": "jest"
23
23
  },
@@ -42,16 +42,30 @@
42
42
  "@react-native-async-storage/async-storage": ">=2.2.0",
43
43
  "react": ">=16.8.0",
44
44
  "react-native": ">=0.63.0",
45
- "react-native-device-info": "^14.0.4"
45
+ "react-native-device-info": ">=14.0.4"
46
46
  },
47
47
  "devDependencies": {
48
+ "@commitlint/cli": "^20.3.0",
49
+ "@commitlint/config-conventional": "^20.3.0",
50
+ "@eslint/compat": "^2.0.0",
51
+ "@eslint/eslintrc": "^3.3.3",
52
+ "@eslint/js": "^9.39.2",
48
53
  "@react-native-async-storage/async-storage": ">=2.2.0",
54
+ "@react-native/eslint-config": "^0.84.0-nightly-20260106-36a0d9ef6",
49
55
  "@types/jest": "^30.0.0",
50
- "@types/react": "^19.1.8",
51
- "jest": "^30.0.4",
56
+ "@types/react": "^19.2.7",
57
+ "eslint": "^9.39.2",
58
+ "eslint-plugin-prettier": "^5.5.4",
59
+ "husky": "^9.1.7",
60
+ "jest": "^30.2.0",
61
+ "lint-staged": "^16.2.7",
52
62
  "metro-react-native-babel-preset": "^0.77.0",
53
- "react-native-device-info": "^14.0.4",
54
- "typescript": "^5.8.3"
63
+ "prettier": "^3.7.4",
64
+ "react-native-device-info": "^15.0.1",
65
+ "typescript": "^5.9.3"
55
66
  },
56
- "react-native": "./lib/index.js"
67
+ "react-native": "./lib/index.js",
68
+ "lint-staged": {
69
+ "*.{ts,js}": "eslint --fix"
70
+ }
57
71
  }