react-native-asleep 1.0.18 → 1.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.
@@ -5,6 +5,7 @@ public class AsleepModule: Module {
5
5
  private var trackingManager: Asleep.SleepTrackingManager?
6
6
  private var reportManager: Asleep.Reports?
7
7
  private(set) var config: Asleep.Config?
8
+ private var isLogEnabled = false
8
9
 
9
10
  public func definition() -> ModuleDefinition {
10
11
  Name("Asleep")
@@ -27,6 +28,7 @@ public class AsleepModule: Module {
27
28
  Events("onAnalysisResult")
28
29
 
29
30
  Function("setup") { (apiKey: String, baseUrl: String?, callbackUrl: String?, service: String?, enableODA: Bool?) in
31
+ Asleep.setLogger(self)
30
32
  Asleep.setup(apiKey: apiKey,
31
33
  baseUrl: URL(string: baseUrl ?? ""),
32
34
  callbackUrl: URL(string: callbackUrl ?? ""),
@@ -36,6 +38,7 @@ public class AsleepModule: Module {
36
38
  }
37
39
 
38
40
  Function("initAsleepConfig") { (apiKey: String, userId: String?, baseUrl: String?, callbackUrl: String?) in
41
+ Asleep.setLogger(self)
39
42
  Asleep.initAsleepConfig(apiKey: apiKey,
40
43
  userId: userId,
41
44
  baseUrl: URL(string: baseUrl ?? ""),
@@ -43,6 +46,13 @@ public class AsleepModule: Module {
43
46
  delegate: self)
44
47
  }
45
48
 
49
+ // Gates SDK log forwarding natively so disabled logs never cross the
50
+ // bridge — every forwarded line lands in addLog, and `log` is returned
51
+ // from useAsleep(), so unconditional forwarding re-renders consumers.
52
+ Function("enableLog") { (enabled: Bool) in
53
+ self.isLogEnabled = enabled
54
+ }
55
+
46
56
  AsyncFunction("isSleepTrackingAlive") { () -> Bool in
47
57
  // iOS doesn't have a persistent foreground service like Android
48
58
  // Always return false to maintain consistent API behavior
@@ -91,6 +101,13 @@ public class AsleepModule: Module {
91
101
  trackingManager.stopTracking()
92
102
  }
93
103
 
104
+ AsyncFunction("resumeTracking") { () -> Void in
105
+ guard let trackingManager = self.trackingManager else {
106
+ throw NSError(domain: "AsleepModule", code: 1, userInfo: [NSLocalizedDescriptionKey: "Tracking manager not initialized"])
107
+ }
108
+ trackingManager.resumeTracking()
109
+ }
110
+
94
111
  AsyncFunction("getReport") { (sessionId: String) -> [String: Any] in
95
112
  sendEvent("onDebugLog", ["message": "getReport"])
96
113
 
@@ -217,7 +234,8 @@ extension AsleepModule: AsleepSetupDelegate {
217
234
  sendEvent("onSetupDidFail", [
218
235
  "error": error.localizedDescription,
219
236
  "caseName": String(describing: error),
220
- "code": (error as NSError).code
237
+ "code": (error as NSError).code,
238
+ "sdkCode": error.errorCode.code
221
239
  ])
222
240
  }
223
241
 
@@ -238,7 +256,8 @@ extension AsleepModule: AsleepConfigDelegate {
238
256
  sendEvent("onUserJoinFailed", [
239
257
  "error": error.localizedDescription,
240
258
  "caseName": String(describing: error),
241
- "code": (error as NSError).code
259
+ "code": (error as NSError).code,
260
+ "sdkCode": error.errorCode.code
242
261
  ])
243
262
  }
244
263
 
@@ -270,11 +289,23 @@ extension AsleepModule: AsleepSleepTrackingManagerDelegate {
270
289
  errorInfo["code"] = "INTERRUPTION_RECOVERY_FAILED"
271
290
  errorInfo["message"] = "Failed to recover from audio interruption after \(attemptsCount) attempts"
272
291
  errorInfo["attemptsCount"] = attemptsCount
292
+ case .audioInitializationFailed:
293
+ errorInfo["code"] = "AUDIO_INITIALIZATION_FAILED"
294
+ errorInfo["message"] = "Audio recording could not be initialized and the recording engine has been torn down. resumeTracking() will not help; the session must be stopped and restarted."
295
+ case .cannotActivateInBackground:
296
+ errorInfo["code"] = "CANNOT_ACTIVATE_IN_BACKGROUND"
297
+ errorInfo["message"] = "Recording could not resume in background. resumeTracking() must be called in foreground."
273
298
  default:
274
299
  errorInfo["code"] = "UNKNOWN_ERROR"
275
300
  errorInfo["caseName"] = String(describing: error)
276
301
  }
302
+ // (error as NSError).code is the Swift enum declaration ordinal, not the
303
+ // documented Asleep error code; kept for backward compatibility only.
277
304
  errorInfo["errorCode"] = (error as NSError).code
305
+ // Documented numeric code (10000-34999) via the SDK's errorCode computed
306
+ // property — the v3.2.0+ recommended API (Asleep.Interface.swift). This is
307
+ // what JS uses to classify errors, including cases in the default: arm.
308
+ errorInfo["sdkCode"] = error.errorCode.code
278
309
 
279
310
  sendEvent("onTrackingFailed", errorInfo)
280
311
  }
@@ -331,10 +362,35 @@ extension AsleepModule: AsleepSleepTrackingManagerDelegate {
331
362
  }
332
363
  }
333
364
 
334
- extension AsleepModule: AsleepDebugLoggerDelegate {
335
- public func didPrint(message: String) {
365
+ // AsleepDebugLoggerDelegate is deprecated in 3.2.0 and removed in 3.3.0.
366
+ extension AsleepModule: AsleepLogger {
367
+ private func sendLoggerEvent(level: String, tag: LogTag, msg: String, error: Error?) {
368
+ var message = "[\(level)][\(tag.value)] \(msg)"
369
+ if let error = error {
370
+ message += " | \(error.localizedDescription)"
371
+ }
336
372
  sendEvent("onDebugLog", ["message": message])
337
373
  }
374
+
375
+ public func d(tag: LogTag, msg: String, error: Error?) {
376
+ guard isLogEnabled else { return }
377
+ sendLoggerEvent(level: "D", tag: tag, msg: msg, error: error)
378
+ }
379
+
380
+ public func e(tag: LogTag, msg: String, error: Error?) {
381
+ guard isLogEnabled else { return }
382
+ sendLoggerEvent(level: "E", tag: tag, msg: msg, error: error)
383
+ }
384
+
385
+ public func i(tag: LogTag, msg: String, error: Error?) {
386
+ guard isLogEnabled else { return }
387
+ sendLoggerEvent(level: "I", tag: tag, msg: msg, error: error)
388
+ }
389
+
390
+ public func w(tag: LogTag, msg: String, error: Error?) {
391
+ guard isLogEnabled else { return }
392
+ sendLoggerEvent(level: "W", tag: tag, msg: msg, error: error)
393
+ }
338
394
  }
339
395
 
340
396
  extension Encodable {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-asleep",
3
- "version": "1.0.18",
3
+ "version": "1.1.0",
4
4
  "description": "Asleep SDK for React Native",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -159,6 +159,14 @@ export type AsleepAnalysisResult = {
159
159
  snoringStages?: number[];
160
160
  };
161
161
 
162
+ // iOS resolves requestAnalysis with an ack while the real payload arrives via the
163
+ // onAnalysisResult event. Android resolves with the full AsleepAnalysisResult and also
164
+ // fires the event. Consumers reading the promise return value should narrow on `status`.
165
+ export type AsleepAnalysisAck = {
166
+ status: "requested";
167
+ timestamp: number;
168
+ };
169
+
162
170
  export type AudioSessionOption = "duckOthers" | "allowAirPlay" | "allowBluetooth" | "allowBluetoothA2DP";
163
171
 
164
172
  export type TrackingConfig = {
@@ -175,6 +183,36 @@ export type TrackingConfig = {
175
183
  };
176
184
  };
177
185
 
186
+ /**
187
+ * Coarse classification of a tracking-runtime failure (onTrackingFailed),
188
+ * derived from the native error code. The library reports the objective fact;
189
+ * the app decides log severity. Recommended mapping:
190
+ * - "terminal" / "recordingDead" → error (session or recorder is gone)
191
+ * - "recoveryRequired" / "transient" → warning (tracking is still alive and a
192
+ * defined recovery path exists)
193
+ * - "unknown" → error (unclassified; do not assume it is benign)
194
+ */
195
+ export type AsleepErrorCategory = "terminal" | "recordingDead" | "recoveryRequired" | "transient" | "unknown";
196
+
197
+ /**
198
+ * Structured view of the last onTrackingFailed event, exposed as
199
+ * useAsleep().errorInfo alongside the legacy `error` JSON string. Cleared to
200
+ * null wherever `error` is cleared.
201
+ */
202
+ export type AsleepErrorInfo = {
203
+ /** Stable string code from the native module (e.g. "UPLOAD_TRACKING_TERMINATED"). */
204
+ code: string;
205
+ category: AsleepErrorCategory;
206
+ /**
207
+ * Numeric code documented by the native Asleep SDKs (range 10000-34999):
208
+ * iOS `AsleepError.errorCode.code` (v3.2.0+), Android listener `errorCode`.
209
+ */
210
+ sdkCode?: number;
211
+ message?: string;
212
+ /** iOS: Swift enum case description for cases without an explicit code mapping. */
213
+ caseName?: string;
214
+ };
215
+
178
216
  export type AsleepEventType = {
179
217
  onTrackingCreated: { sessionId?: string };
180
218
  onTrackingUploaded: { sequence: number };
@@ -183,7 +221,13 @@ export type AsleepEventType = {
183
221
  error: string;
184
222
  code: string;
185
223
  message?: string;
224
+ /**
225
+ * @deprecated On iOS this is the Swift enum declaration ordinal from
226
+ * NSError bridging, not the documented Asleep error code. Use `sdkCode`.
227
+ */
186
228
  errorCode?: number;
229
+ /** Numeric code documented by the native Asleep SDKs (both platforms). */
230
+ sdkCode?: number;
187
231
  caseName?: string;
188
232
  };
189
233
  onTrackingInterrupted: undefined;
@@ -196,11 +240,17 @@ export type AsleepEventType = {
196
240
  detail?: string;
197
241
  caseName?: string;
198
242
  code?: number;
243
+ /** Numeric code documented by the native Asleep SDKs (both platforms). */
244
+ sdkCode?: number;
199
245
  };
200
246
  onUserDeleted: { userId: string };
201
247
  onDebugLog: { message: string };
202
248
  onSetupDidComplete: undefined;
203
- onSetupDidFail: { error: string };
249
+ onSetupDidFail: {
250
+ error: string;
251
+ /** Numeric code documented by the native Asleep SDKs (both platforms). */
252
+ sdkCode?: number;
253
+ };
204
254
  onSetupInProgress: { progress: number };
205
255
  onAnalysisResult: AsleepAnalysisResult;
206
256
  };