react-native-asleep 1.0.14 → 1.0.17
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/CHANGELOG.md +8 -0
- package/README.md +65 -14
- package/android/build.gradle +1 -1
- package/android/src/main/java/ai/asleep/reactnative/AsleepModule.kt +54 -50
- package/build/Asleep.types.d.ts +25 -4
- package/build/Asleep.types.d.ts.map +1 -1
- package/build/Asleep.types.js.map +1 -1
- package/build/AsleepStore.d.ts.map +1 -1
- package/build/AsleepStore.js +56 -29
- package/build/AsleepStore.js.map +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -1
- package/build/index.js.map +1 -1
- package/ios/Asleep.podspec +1 -1
- package/ios/AsleepModule.swift +28 -7
- package/package.json +46 -15
- package/src/Asleep.types.ts +25 -7
- package/src/AsleepStore.ts +77 -79
- package/src/index.ts +15 -41
- package/.eslintrc.js +0 -5
- package/.releaserc.js +0 -58
- package/AGENTS.md +0 -107
- package/docs/PUBLISHING.md +0 -36
- package/ios/AVAudioSession+Swizzle.swift +0 -45
- package/tsconfig.json +0 -9
- /package/{License.md → LICENSE.md} +0 -0
package/src/AsleepStore.ts
CHANGED
|
@@ -16,6 +16,15 @@ import AsleepModule from "./AsleepModule";
|
|
|
16
16
|
|
|
17
17
|
const emitter = new EventEmitter(AsleepModule);
|
|
18
18
|
|
|
19
|
+
// Error codes emitted on onTrackingFailed for which the native SDK has already
|
|
20
|
+
// terminated the session and will not deliver onTrackingClosed. JS must clear
|
|
21
|
+
// tracking state itself when one of these arrives. See AGENTS.md "Native Behavior
|
|
22
|
+
// Compensations" for the upstream behavior these guard against.
|
|
23
|
+
const TERMINAL_TRACKING_ERROR_CODES = new Set<string>([
|
|
24
|
+
"UPLOAD_TRACKING_TERMINATED", // iOS 403/429 closeSessionSilently; Android errorCode 23499
|
|
25
|
+
"INTERRUPTION_RECOVERY_FAILED", // iOS 3 failed auto-resume attempts (3.2.0+)
|
|
26
|
+
]);
|
|
27
|
+
|
|
19
28
|
export interface AsleepState {
|
|
20
29
|
didClose: boolean;
|
|
21
30
|
isTracking: boolean;
|
|
@@ -59,7 +68,7 @@ export interface AsleepState {
|
|
|
59
68
|
requestAnalysis: () => Promise<AsleepAnalysisResult | null>;
|
|
60
69
|
addEventListener: <K extends keyof AsleepEventType>(
|
|
61
70
|
eventType: K,
|
|
62
|
-
listener: (data: AsleepEventType[K]) => void
|
|
71
|
+
listener: (data: AsleepEventType[K]) => void,
|
|
63
72
|
) => () => void;
|
|
64
73
|
getTrackingDurationMinutes: () => number;
|
|
65
74
|
|
|
@@ -126,30 +135,20 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
126
135
|
|
|
127
136
|
// Prevent duplicate execution if setup is already in progress
|
|
128
137
|
if (isSetupInProgress) {
|
|
129
|
-
addLog(
|
|
130
|
-
"[setup] Setup is already in progress. Please try again later."
|
|
131
|
-
);
|
|
138
|
+
addLog("[setup] Setup is already in progress. Please try again later.");
|
|
132
139
|
throw new Error("Setup is already in progress.");
|
|
133
140
|
}
|
|
134
141
|
|
|
135
142
|
// Block setup execution if tracking is in progress
|
|
136
143
|
if (isTracking) {
|
|
137
144
|
addLog("[setup] Cannot execute setup while tracking is in progress.");
|
|
138
|
-
throw new Error(
|
|
139
|
-
"Cannot execute setup while tracking is in progress."
|
|
140
|
-
);
|
|
145
|
+
throw new Error("Cannot execute setup while tracking is in progress.");
|
|
141
146
|
}
|
|
142
147
|
|
|
143
148
|
addLog("[setup] Start");
|
|
144
149
|
set({ isSetupInProgress: true, error: null });
|
|
145
150
|
|
|
146
|
-
await AsleepModule.setup(
|
|
147
|
-
config.apiKey,
|
|
148
|
-
config.baseUrl,
|
|
149
|
-
config.callbackUrl,
|
|
150
|
-
config.service,
|
|
151
|
-
config.enableODA
|
|
152
|
-
);
|
|
151
|
+
await AsleepModule.setup(config.apiKey, config.baseUrl, config.callbackUrl, config.service, config.enableODA);
|
|
153
152
|
|
|
154
153
|
// Store ODA enabled state
|
|
155
154
|
set({
|
|
@@ -175,7 +174,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
175
174
|
config.apiKey,
|
|
176
175
|
config.userId,
|
|
177
176
|
config.baseUrl,
|
|
178
|
-
config.callbackUrl
|
|
177
|
+
config.callbackUrl,
|
|
179
178
|
);
|
|
180
179
|
|
|
181
180
|
set({ isInitialized: true });
|
|
@@ -197,7 +196,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
197
196
|
const isAlive = await AsleepModule.isSleepTrackingAlive();
|
|
198
197
|
|
|
199
198
|
set({
|
|
200
|
-
hasCheckedStatus: true
|
|
199
|
+
hasCheckedStatus: true,
|
|
201
200
|
});
|
|
202
201
|
|
|
203
202
|
// If service is alive on Android, restore connection to it
|
|
@@ -215,7 +214,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
215
214
|
|
|
216
215
|
addLog(`[checkAndRestoreTracking] Complete - hasActiveSession: ${isAlive}`);
|
|
217
216
|
return {
|
|
218
|
-
hasActiveSession: isAlive
|
|
217
|
+
hasActiveSession: isAlive,
|
|
219
218
|
};
|
|
220
219
|
} catch (error: any) {
|
|
221
220
|
console.error("checkAndRestoreTracking error:", error);
|
|
@@ -244,9 +243,9 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
244
243
|
// Mark that check was performed (required for startTracking)
|
|
245
244
|
set({ hasCheckedBatteryOptimization: true });
|
|
246
245
|
|
|
247
|
-
if (Platform.OS ===
|
|
246
|
+
if (Platform.OS === "ios") {
|
|
248
247
|
addLog("[checkBatteryOptimization] iOS - not applicable");
|
|
249
|
-
return { exempted: true, platform:
|
|
248
|
+
return { exempted: true, platform: "ios" };
|
|
250
249
|
}
|
|
251
250
|
|
|
252
251
|
// Android: Check current status
|
|
@@ -255,10 +254,10 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
255
254
|
|
|
256
255
|
return {
|
|
257
256
|
exempted,
|
|
258
|
-
platform:
|
|
259
|
-
message: exempted
|
|
260
|
-
"Battery optimization disabled - ready for tracking"
|
|
261
|
-
"Battery optimization must be disabled for reliable tracking"
|
|
257
|
+
platform: "android",
|
|
258
|
+
message: exempted
|
|
259
|
+
? "Battery optimization disabled - ready for tracking"
|
|
260
|
+
: "Battery optimization must be disabled for reliable tracking",
|
|
262
261
|
};
|
|
263
262
|
},
|
|
264
263
|
|
|
@@ -274,9 +273,9 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
274
273
|
requestBatteryOptimizationExemption: async () => {
|
|
275
274
|
const { addLog } = get();
|
|
276
275
|
|
|
277
|
-
if (Platform.OS ===
|
|
276
|
+
if (Platform.OS === "ios") {
|
|
278
277
|
addLog("[requestBatteryOptimizationExemption] iOS - not applicable");
|
|
279
|
-
return true;
|
|
278
|
+
return true; // Not applicable on iOS
|
|
280
279
|
}
|
|
281
280
|
|
|
282
281
|
addLog("[requestBatteryOptimizationExemption] Opening battery settings...");
|
|
@@ -298,13 +297,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
298
297
|
*/
|
|
299
298
|
startTracking: async (config?: TrackingConfig) => {
|
|
300
299
|
try {
|
|
301
|
-
const {
|
|
302
|
-
requestRequiredPermissions,
|
|
303
|
-
addLog,
|
|
304
|
-
isODAEnabled,
|
|
305
|
-
isSetupInProgress,
|
|
306
|
-
hasCheckedStatus,
|
|
307
|
-
} = get();
|
|
300
|
+
const { requestRequiredPermissions, addLog, isODAEnabled, isSetupInProgress, hasCheckedStatus } = get();
|
|
308
301
|
|
|
309
302
|
// Enforce that checkAndRestoreTracking must be called first
|
|
310
303
|
if (!hasCheckedStatus) {
|
|
@@ -318,15 +311,13 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
318
311
|
addLog("[startTracking] ERROR: Must check battery optimization first");
|
|
319
312
|
throw new Error(
|
|
320
313
|
"Must call checkBatteryOptimization() before starting tracking. " +
|
|
321
|
-
|
|
314
|
+
"This check is required on both iOS and Android to ensure cross-platform consistency.",
|
|
322
315
|
);
|
|
323
316
|
}
|
|
324
317
|
|
|
325
318
|
// Block startTracking execution if setup is in progress
|
|
326
319
|
if (isSetupInProgress) {
|
|
327
|
-
addLog(
|
|
328
|
-
"[startTracking] Cannot start tracking while setup is in progress."
|
|
329
|
-
);
|
|
320
|
+
addLog("[startTracking] Cannot start tracking while setup is in progress.");
|
|
330
321
|
throw new Error("Cannot start tracking while setup is in progress.");
|
|
331
322
|
}
|
|
332
323
|
|
|
@@ -341,7 +332,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
341
332
|
const permission = await requestRequiredPermissions();
|
|
342
333
|
if (!permission) {
|
|
343
334
|
// SDK shouldn't show UI directly - throw specific error message
|
|
344
|
-
if (Platform.OS ===
|
|
335
|
+
if (Platform.OS === "android" && Platform.Version >= 33) {
|
|
345
336
|
throw new Error("Microphone and notification permissions are required for sleep tracking");
|
|
346
337
|
} else {
|
|
347
338
|
throw new Error("Microphone permission is required for sleep tracking");
|
|
@@ -349,13 +340,13 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
349
340
|
}
|
|
350
341
|
|
|
351
342
|
// Always verify CURRENT exemption status
|
|
352
|
-
if (Platform.OS ===
|
|
343
|
+
if (Platform.OS === "android") {
|
|
353
344
|
const batteryExempted = await AsleepModule.isBatteryOptimizationExempted();
|
|
354
345
|
if (!batteryExempted) {
|
|
355
346
|
addLog("[startTracking] ERROR: Battery optimization not disabled");
|
|
356
347
|
throw new Error(
|
|
357
348
|
"Battery optimization must be disabled for reliable sleep tracking. " +
|
|
358
|
-
|
|
349
|
+
"Call requestBatteryOptimizationExemption() to guide user to settings.",
|
|
359
350
|
);
|
|
360
351
|
}
|
|
361
352
|
addLog("[startTracking] Battery optimization exempted - OK");
|
|
@@ -370,9 +361,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
370
361
|
await AsleepModule.startTracking(config);
|
|
371
362
|
|
|
372
363
|
if (isODAEnabled) {
|
|
373
|
-
addLog(
|
|
374
|
-
"[startTracking] ODA enabled - real-time analysis will start automatically"
|
|
375
|
-
);
|
|
364
|
+
addLog("[startTracking] ODA enabled - real-time analysis will start automatically");
|
|
376
365
|
} else {
|
|
377
366
|
addLog("[startTracking] ODA not enabled");
|
|
378
367
|
}
|
|
@@ -395,16 +384,16 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
395
384
|
const { addLog } = get();
|
|
396
385
|
addLog("[stopTracking] Start");
|
|
397
386
|
|
|
398
|
-
const
|
|
387
|
+
const result = await AsleepModule.stopTracking();
|
|
399
388
|
set({
|
|
400
389
|
didClose: true,
|
|
401
|
-
sessionId,
|
|
390
|
+
...(result ? { sessionId: result } : {}),
|
|
402
391
|
isTracking: false,
|
|
403
392
|
isAnalyzing: false,
|
|
404
393
|
trackingStartTime: null,
|
|
405
394
|
});
|
|
406
395
|
|
|
407
|
-
addLog(`[stopTracking] Success -
|
|
396
|
+
addLog(`[stopTracking] Success - result: ${result}`);
|
|
408
397
|
} catch (error: any) {
|
|
409
398
|
console.error("stopTracking error:", error);
|
|
410
399
|
set({ error: error.message });
|
|
@@ -521,13 +510,13 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
521
510
|
*/
|
|
522
511
|
requestMicrophonePermission: async () => {
|
|
523
512
|
console.warn(
|
|
524
|
-
|
|
513
|
+
"[AsleepSDK] requestMicrophonePermission is deprecated. Please use requestRequiredPermissions instead.",
|
|
525
514
|
);
|
|
526
515
|
return get().requestRequiredPermissions();
|
|
527
516
|
},
|
|
528
517
|
|
|
529
518
|
requestRequiredPermissions: async () => {
|
|
530
|
-
if (Platform.OS ===
|
|
519
|
+
if (Platform.OS === "android") {
|
|
531
520
|
try {
|
|
532
521
|
// Prepare permissions array with RECORD_AUDIO
|
|
533
522
|
const permissions = [PermissionsAndroid.PERMISSIONS.RECORD_AUDIO];
|
|
@@ -541,17 +530,18 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
541
530
|
const results = await PermissionsAndroid.requestMultiple(permissions);
|
|
542
531
|
|
|
543
532
|
// Check if all required permissions are granted
|
|
544
|
-
const audioGranted =
|
|
545
|
-
PermissionsAndroid.RESULTS.GRANTED;
|
|
533
|
+
const audioGranted =
|
|
534
|
+
results[PermissionsAndroid.PERMISSIONS.RECORD_AUDIO] === PermissionsAndroid.RESULTS.GRANTED;
|
|
546
535
|
|
|
547
|
-
const notificationGranted =
|
|
548
|
-
|
|
549
|
-
|
|
536
|
+
const notificationGranted =
|
|
537
|
+
Platform.Version >= 33
|
|
538
|
+
? results[PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS] === PermissionsAndroid.RESULTS.GRANTED
|
|
539
|
+
: true;
|
|
550
540
|
|
|
551
541
|
// Both must be granted for tracking to work properly
|
|
552
542
|
return audioGranted && notificationGranted;
|
|
553
543
|
} catch (err) {
|
|
554
|
-
console.warn(
|
|
544
|
+
console.warn("Permission request error:", err);
|
|
555
545
|
return false;
|
|
556
546
|
}
|
|
557
547
|
}
|
|
@@ -590,9 +580,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
590
580
|
}
|
|
591
581
|
// For iOS, isAnalyzing will be set to false when onAnalysisResult event fires
|
|
592
582
|
|
|
593
|
-
addLog(
|
|
594
|
-
`[requestAnalysis] Request sent - ${JSON.stringify(convertedResult)}`
|
|
595
|
-
);
|
|
583
|
+
addLog(`[requestAnalysis] Request sent - ${JSON.stringify(convertedResult)}`);
|
|
596
584
|
|
|
597
585
|
return convertedResult;
|
|
598
586
|
} catch (error: any) {
|
|
@@ -602,10 +590,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
602
590
|
}
|
|
603
591
|
},
|
|
604
592
|
|
|
605
|
-
addEventListener: <K extends keyof AsleepEventType>(
|
|
606
|
-
eventType: K,
|
|
607
|
-
listener: (data: AsleepEventType[K]) => void
|
|
608
|
-
) => {
|
|
593
|
+
addEventListener: <K extends keyof AsleepEventType>(eventType: K, listener: (data: AsleepEventType[K]) => void) => {
|
|
609
594
|
const subscription = emitter.addListener(eventType, listener);
|
|
610
595
|
return () => subscription.remove();
|
|
611
596
|
},
|
|
@@ -613,9 +598,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
613
598
|
getTrackingDurationMinutes: () => {
|
|
614
599
|
const { trackingStartTime } = get();
|
|
615
600
|
if (!trackingStartTime) return 0;
|
|
616
|
-
return Math.floor(
|
|
617
|
-
(Date.now() - trackingStartTime.getTime()) / (1000 * 60)
|
|
618
|
-
);
|
|
601
|
+
return Math.floor((Date.now() - trackingStartTime.getTime()) / (1000 * 60));
|
|
619
602
|
},
|
|
620
603
|
|
|
621
604
|
// internal actions
|
|
@@ -630,8 +613,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
630
613
|
setIsAnalyzing: (isAnalyzing) => set({ isAnalyzing }),
|
|
631
614
|
setTrackingStartTime: (time) => set({ trackingStartTime: time }),
|
|
632
615
|
setIsInitialized: (initialized) => set({ isInitialized: initialized }),
|
|
633
|
-
setIsSetupInProgress: (inProgress) =>
|
|
634
|
-
set({ isSetupInProgress: inProgress }),
|
|
616
|
+
setIsSetupInProgress: (inProgress) => set({ isSetupInProgress: inProgress }),
|
|
635
617
|
setIsSetupComplete: (complete) => set({ isSetupComplete: complete }),
|
|
636
618
|
setHasCheckedStatus: (checked) => set({ hasCheckedStatus: checked }),
|
|
637
619
|
setHasCheckedBatteryOptimization: (checked) => set({ hasCheckedBatteryOptimization: checked }),
|
|
@@ -652,7 +634,7 @@ export const useAsleepStore = create<AsleepState>()(
|
|
|
652
634
|
|
|
653
635
|
set({ log: formattedLog });
|
|
654
636
|
},
|
|
655
|
-
}))
|
|
637
|
+
})),
|
|
656
638
|
);
|
|
657
639
|
|
|
658
640
|
// Global flag to prevent multiple listener registrations
|
|
@@ -674,13 +656,25 @@ export const initializeAsleepListeners = () => {
|
|
|
674
656
|
setIsTracking,
|
|
675
657
|
setIsTrackingPaused,
|
|
676
658
|
setError,
|
|
677
|
-
setDidClose,
|
|
678
659
|
setAnalysisResult,
|
|
679
660
|
setIsAnalyzing,
|
|
680
661
|
setIsSetupInProgress,
|
|
681
662
|
setIsSetupComplete,
|
|
682
663
|
} = store;
|
|
683
664
|
|
|
665
|
+
// Clears session tracking state. Called from both onTrackingClosed (clean
|
|
666
|
+
// close) and the terminal branch of onTrackingFailed (native SDK tore down
|
|
667
|
+
// the session without firing onTrackingClosed). Single setState so
|
|
668
|
+
// subscribers are notified once instead of four times.
|
|
669
|
+
const clearTrackingState = () => {
|
|
670
|
+
useAsleepStore.setState({
|
|
671
|
+
isTracking: false,
|
|
672
|
+
isAnalyzing: false,
|
|
673
|
+
didClose: true,
|
|
674
|
+
trackingStartTime: null,
|
|
675
|
+
});
|
|
676
|
+
};
|
|
677
|
+
|
|
684
678
|
// event handlers
|
|
685
679
|
const handlers = {
|
|
686
680
|
// Connected: iOS userDidJoin, Android onSuccess (AsleepConfigListener)
|
|
@@ -705,10 +699,7 @@ export const initializeAsleepListeners = () => {
|
|
|
705
699
|
if (data && data.sessionId) {
|
|
706
700
|
setSessionId(data.sessionId);
|
|
707
701
|
}
|
|
708
|
-
addLog(
|
|
709
|
-
`[onTrackingCreated]${data?.sessionId ? ` sessionId: ${data.sessionId}` : ""
|
|
710
|
-
}`
|
|
711
|
-
);
|
|
702
|
+
addLog(`[onTrackingCreated]${data?.sessionId ? ` sessionId: ${data.sessionId}` : ""}`);
|
|
712
703
|
},
|
|
713
704
|
// Connected: iOS didUpload, Android onPerform (AsleepTrackingListener)
|
|
714
705
|
// Triggers automatic analysis requests in both ODA and non-ODA modes
|
|
@@ -722,8 +713,7 @@ export const initializeAsleepListeners = () => {
|
|
|
722
713
|
addLog(`[onTrackingUploaded] Auto analysis failed: ${error.message}`);
|
|
723
714
|
state.setIsAnalyzing(false);
|
|
724
715
|
});
|
|
725
|
-
}
|
|
726
|
-
else if (!state.isODAEnabled && state.isTracking) {
|
|
716
|
+
} else if (!state.isODAEnabled && state.isTracking) {
|
|
727
717
|
if (data.sequence >= 10 && data.sequence % 10 === 1) {
|
|
728
718
|
state.setIsAnalyzing(true);
|
|
729
719
|
state.requestAnalysis().catch((error) => {
|
|
@@ -736,18 +726,17 @@ export const initializeAsleepListeners = () => {
|
|
|
736
726
|
// Connected: iOS didClose, Android onFinish (AsleepTrackingListener)
|
|
737
727
|
onTrackingClosed: (data: { sessionId: string }) => {
|
|
738
728
|
setSessionId(data.sessionId);
|
|
739
|
-
|
|
740
|
-
setIsTracking(false);
|
|
741
|
-
setIsAnalyzing(false);
|
|
742
|
-
store.setTrackingStartTime(null);
|
|
729
|
+
clearTrackingState();
|
|
743
730
|
addLog(`[onTrackingClosed] sessionId: ${data.sessionId}`);
|
|
744
731
|
},
|
|
745
732
|
// Connected: iOS didFail, Android onFail (AsleepTrackingListener)
|
|
746
|
-
// Note: This is a transient error event - tracking continues, only logs the error
|
|
747
733
|
onTrackingFailed: (error: any) => {
|
|
748
734
|
const errorString = JSON.stringify(error);
|
|
749
735
|
setError(errorString);
|
|
750
736
|
addLog(`[onTrackingError] error: ${errorString}`);
|
|
737
|
+
if (error && TERMINAL_TRACKING_ERROR_CODES.has(error.code)) {
|
|
738
|
+
clearTrackingState();
|
|
739
|
+
}
|
|
751
740
|
},
|
|
752
741
|
// Connected: iOS didInterrupt, Android NOT IMPLEMENTED
|
|
753
742
|
onTrackingInterrupted: () => {
|
|
@@ -755,7 +744,16 @@ export const initializeAsleepListeners = () => {
|
|
|
755
744
|
addLog(`[onTrackingInterrupted]`);
|
|
756
745
|
},
|
|
757
746
|
// Connected: iOS didResume, Android NOT IMPLEMENTED
|
|
747
|
+
// Always clear the error: some iOS recovery paths (e.g. cannotActivateInBackground
|
|
748
|
+
// retry) reach this handler without a preceding didInterrupt, and the stale error
|
|
749
|
+
// should still be cleared. Only the paused-state mutation is gated, to dedup the
|
|
750
|
+
// iOS 3.2.1+ double fire from handleRecovering -> handleResumed.
|
|
758
751
|
onTrackingResumed: () => {
|
|
752
|
+
setError(null);
|
|
753
|
+
if (!useAsleepStore.getState().isTrackingPaused) {
|
|
754
|
+
addLog(`[onTrackingResumed] (no prior pause; error cleared)`);
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
759
757
|
setIsTrackingPaused(false);
|
|
760
758
|
addLog(`[onTrackingResumed]`);
|
|
761
759
|
},
|
|
@@ -789,7 +787,7 @@ export const initializeAsleepListeners = () => {
|
|
|
789
787
|
// Connected: iOS analyzing (session), Android onSleepDataReceived (AsleepSleepDataListener)
|
|
790
788
|
onAnalysisResult: (data: any) => {
|
|
791
789
|
setAnalysisResult(data);
|
|
792
|
-
setIsAnalyzing(false);
|
|
790
|
+
setIsAnalyzing(false); // Analysis is complete, so set to false
|
|
793
791
|
addLog(`[onAnalysisResult] ${JSON.stringify(data)}`);
|
|
794
792
|
},
|
|
795
793
|
};
|
package/src/index.ts
CHANGED
|
@@ -22,13 +22,7 @@ class Asleep {
|
|
|
22
22
|
|
|
23
23
|
setup = async (config: AsleepSetupConfig): Promise<void> => {
|
|
24
24
|
try {
|
|
25
|
-
await AsleepModule.setup(
|
|
26
|
-
config.apiKey,
|
|
27
|
-
config.baseUrl,
|
|
28
|
-
config.callbackUrl,
|
|
29
|
-
config.service,
|
|
30
|
-
config.enableODA
|
|
31
|
-
);
|
|
25
|
+
await AsleepModule.setup(config.apiKey, config.baseUrl, config.callbackUrl, config.service, config.enableODA);
|
|
32
26
|
} catch (error) {
|
|
33
27
|
console.error("setup error:", error);
|
|
34
28
|
throw error;
|
|
@@ -41,7 +35,7 @@ class Asleep {
|
|
|
41
35
|
config.apiKey,
|
|
42
36
|
config.userId,
|
|
43
37
|
config.baseUrl,
|
|
44
|
-
config.callbackUrl
|
|
38
|
+
config.callbackUrl,
|
|
45
39
|
);
|
|
46
40
|
return result;
|
|
47
41
|
} catch (error) {
|
|
@@ -73,10 +67,7 @@ class Asleep {
|
|
|
73
67
|
return this.convertKeysToCamelCase(report);
|
|
74
68
|
};
|
|
75
69
|
|
|
76
|
-
getReportList = async (
|
|
77
|
-
fromDate: string,
|
|
78
|
-
toDate: string
|
|
79
|
-
): Promise<AsleepSession[]> => {
|
|
70
|
+
getReportList = async (fromDate: string, toDate: string): Promise<AsleepSession[]> => {
|
|
80
71
|
const reportList = await AsleepModule.getReportList(fromDate, toDate);
|
|
81
72
|
return reportList.map((session: any) => {
|
|
82
73
|
const converted = this.convertKeysToCamelCase(session);
|
|
@@ -98,10 +89,7 @@ class Asleep {
|
|
|
98
89
|
return AsleepModule.deleteSession(sessionId);
|
|
99
90
|
};
|
|
100
91
|
|
|
101
|
-
getAverageReport = async (
|
|
102
|
-
fromDate: string,
|
|
103
|
-
toDate: string
|
|
104
|
-
): Promise<AsleepAverageReport> => {
|
|
92
|
+
getAverageReport = async (fromDate: string, toDate: string): Promise<AsleepAverageReport> => {
|
|
105
93
|
const averageReport = await AsleepModule.getAverageReport(fromDate, toDate);
|
|
106
94
|
return this.convertKeysToCamelCase(averageReport);
|
|
107
95
|
};
|
|
@@ -111,7 +99,7 @@ class Asleep {
|
|
|
111
99
|
*/
|
|
112
100
|
requestMicrophonePermission = async (): Promise<boolean> => {
|
|
113
101
|
console.warn(
|
|
114
|
-
|
|
102
|
+
"[AsleepSDK] requestMicrophonePermission is deprecated. Please use requestRequiredPermissions instead.",
|
|
115
103
|
);
|
|
116
104
|
return this.requestRequiredPermissions();
|
|
117
105
|
};
|
|
@@ -120,10 +108,7 @@ class Asleep {
|
|
|
120
108
|
return AsleepModule.requestRequiredPermissions();
|
|
121
109
|
};
|
|
122
110
|
|
|
123
|
-
setCustomNotification = async (
|
|
124
|
-
title: string,
|
|
125
|
-
text: string
|
|
126
|
-
): Promise<void> => {
|
|
111
|
+
setCustomNotification = async (title: string, text: string): Promise<void> => {
|
|
127
112
|
if (Platform.OS === "android") {
|
|
128
113
|
return AsleepModule.setCustomNotification(title, text);
|
|
129
114
|
} else {
|
|
@@ -144,10 +129,7 @@ class Asleep {
|
|
|
144
129
|
return obj;
|
|
145
130
|
};
|
|
146
131
|
|
|
147
|
-
addEventListener<K extends keyof AsleepEventType>(
|
|
148
|
-
eventType: K,
|
|
149
|
-
listener: (data: AsleepEventType[K]) => void
|
|
150
|
-
) {
|
|
132
|
+
addEventListener<K extends keyof AsleepEventType>(eventType: K, listener: (data: AsleepEventType[K]) => void) {
|
|
151
133
|
if (!this.listeners[eventType]) {
|
|
152
134
|
this.listeners[eventType] = [];
|
|
153
135
|
}
|
|
@@ -250,8 +232,7 @@ export const asleepStore = useAsleepStore;
|
|
|
250
232
|
export const AsleepSDK = {
|
|
251
233
|
setup: (config: AsleepSetupConfig) => useAsleepStore.getState().setup(config),
|
|
252
234
|
|
|
253
|
-
initAsleepConfig: (config: AsleepConfig) =>
|
|
254
|
-
useAsleepStore.getState().initAsleepConfig(config),
|
|
235
|
+
initAsleepConfig: (config: AsleepConfig) => useAsleepStore.getState().initAsleepConfig(config),
|
|
255
236
|
|
|
256
237
|
checkAndRestoreTracking: () => useAsleepStore.getState().checkAndRestoreTracking(),
|
|
257
238
|
|
|
@@ -263,23 +244,17 @@ export const AsleepSDK = {
|
|
|
263
244
|
|
|
264
245
|
stopTracking: () => useAsleepStore.getState().stopTracking(),
|
|
265
246
|
|
|
266
|
-
getReport: (sessionId: string) =>
|
|
267
|
-
useAsleepStore.getState().getReport(sessionId),
|
|
247
|
+
getReport: (sessionId: string) => useAsleepStore.getState().getReport(sessionId),
|
|
268
248
|
|
|
269
|
-
getReportList: (fromDate: string, toDate: string) =>
|
|
270
|
-
useAsleepStore.getState().getReportList(fromDate, toDate),
|
|
249
|
+
getReportList: (fromDate: string, toDate: string) => useAsleepStore.getState().getReportList(fromDate, toDate),
|
|
271
250
|
|
|
272
|
-
getAverageReport: (fromDate: string, toDate: string) =>
|
|
273
|
-
useAsleepStore.getState().getAverageReport(fromDate, toDate),
|
|
251
|
+
getAverageReport: (fromDate: string, toDate: string) => useAsleepStore.getState().getAverageReport(fromDate, toDate),
|
|
274
252
|
|
|
275
|
-
deleteSession: (sessionId: string) =>
|
|
276
|
-
useAsleepStore.getState().deleteSession(sessionId),
|
|
253
|
+
deleteSession: (sessionId: string) => useAsleepStore.getState().deleteSession(sessionId),
|
|
277
254
|
|
|
278
|
-
requestMicrophonePermission: () =>
|
|
279
|
-
useAsleepStore.getState().requestMicrophonePermission(),
|
|
255
|
+
requestMicrophonePermission: () => useAsleepStore.getState().requestMicrophonePermission(),
|
|
280
256
|
|
|
281
|
-
requestRequiredPermissions: () =>
|
|
282
|
-
useAsleepStore.getState().requestRequiredPermissions(),
|
|
257
|
+
requestRequiredPermissions: () => useAsleepStore.getState().requestRequiredPermissions(),
|
|
283
258
|
|
|
284
259
|
requestAnalysis: () => useAsleepStore.getState().requestAnalysis(),
|
|
285
260
|
|
|
@@ -301,8 +276,7 @@ export const AsleepSDK = {
|
|
|
301
276
|
|
|
302
277
|
enableLog: (print: boolean) => useAsleepStore.getState().enableLog(print),
|
|
303
278
|
|
|
304
|
-
setCustomNotification: (title: string, text: string) =>
|
|
305
|
-
useAsleepStore.getState().setCustomNotification(title, text),
|
|
279
|
+
setCustomNotification: (title: string, text: string) => useAsleepStore.getState().setCustomNotification(title, text),
|
|
306
280
|
|
|
307
281
|
clearError: () => useAsleepStore.getState().clearError(),
|
|
308
282
|
|
package/.eslintrc.js
DELETED
package/.releaserc.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
branches: ["main"],
|
|
3
|
-
plugins: [
|
|
4
|
-
[
|
|
5
|
-
"@semantic-release/commit-analyzer",
|
|
6
|
-
{
|
|
7
|
-
// Configure parser to recognize entire commit message as title
|
|
8
|
-
parserOpts: {
|
|
9
|
-
headerPattern: /^(.*)$/,
|
|
10
|
-
headerCorrespondence: ["subject"],
|
|
11
|
-
},
|
|
12
|
-
// Set rules to make any commit trigger a 'patch' release
|
|
13
|
-
releaseRules: [{ release: "patch" }],
|
|
14
|
-
},
|
|
15
|
-
],
|
|
16
|
-
[
|
|
17
|
-
"@semantic-release/release-notes-generator",
|
|
18
|
-
{
|
|
19
|
-
// Configure parser to recognize entire commit message as title
|
|
20
|
-
parserOpts: {
|
|
21
|
-
headerPattern: /^(.*)$/,
|
|
22
|
-
headerCorrespondence: ["subject"],
|
|
23
|
-
},
|
|
24
|
-
// Customize release notes generator
|
|
25
|
-
writerOpts: {
|
|
26
|
-
// Function to transform commit objects
|
|
27
|
-
transform: (commit) => {
|
|
28
|
-
// Since commit objects cannot be modified directly, create a copy to modify
|
|
29
|
-
const newCommit = { ...commit };
|
|
30
|
-
|
|
31
|
-
// Force assign 'fix' type recognized by release notes generator
|
|
32
|
-
// so that all commits are included in release notes.
|
|
33
|
-
// This will display all commits under the "Bug Fixes" section.
|
|
34
|
-
newCommit.type = "fix";
|
|
35
|
-
|
|
36
|
-
return newCommit;
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
],
|
|
41
|
-
"@semantic-release/changelog",
|
|
42
|
-
[
|
|
43
|
-
"@semantic-release/npm",
|
|
44
|
-
{
|
|
45
|
-
npmPublish: true,
|
|
46
|
-
},
|
|
47
|
-
],
|
|
48
|
-
[
|
|
49
|
-
"@semantic-release/git",
|
|
50
|
-
{
|
|
51
|
-
assets: ["package.json", "yarn.lock", "CHANGELOG.md"],
|
|
52
|
-
message:
|
|
53
|
-
"chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}",
|
|
54
|
-
},
|
|
55
|
-
],
|
|
56
|
-
"@semantic-release/github",
|
|
57
|
-
],
|
|
58
|
-
};
|