File tree Expand file tree Collapse file tree 1 file changed +25
-20
lines changed Expand file tree Collapse file tree 1 file changed +25
-20
lines changed Original file line number Diff line number Diff line change @@ -12,28 +12,33 @@ const listenerKeys = new Map([
12
12
13
13
function createSensorObservable ( sensorType ) {
14
14
return Observable . create ( function subscribe ( observer ) {
15
- this . unsubscribeCallback = ( ) => { } ;
16
-
17
- RNSensors . isAvailable ( sensorType ) . then (
18
- ( ) => {
19
- DeviceEventEmitter . addListener ( listenerKeys . get ( sensorType ) , data => {
20
- observer . next ( data ) ;
21
- } ) ;
22
-
23
- // Register the unsubscribe handler
24
- this . unsubscribeCallback = ( ) => {
25
- RNSensors . stop ( sensorType ) ;
26
- } ;
27
-
15
+
16
+ this . isSensorAvailable = false ;
17
+
18
+ this . unsubscribeCallback = ( ) => {
19
+
20
+ if ( ! this . isSensorAvailable ) return ;
21
+
22
+ // stop the sensor
23
+ RNSensors . stop ( sensorType ) ;
24
+ } ;
25
+
26
+ RNSensors . isAvailable ( sensorType ) . then (
27
+ ( ) => {
28
+ DeviceEventEmitter . addListener ( listenerKeys . get ( sensorType ) , data => {
29
+ observer . next ( data ) ;
30
+ } ) ;
31
+ this . isSensorAvailable = true ;
32
+
28
33
// Start the sensor manager
29
34
RNSensors . start ( sensorType ) ;
30
- } ,
31
- ( ) => {
32
- observer . error ( `Sensor ${ sensorType } is not available` ) ;
33
- }
34
- ) ;
35
-
36
- return this . unsubscribeCallback ;
35
+ } ,
36
+ ( ) => {
37
+ observer . error ( `Sensor ${ sensorType } is not available` ) ;
38
+ }
39
+ ) ;
40
+
41
+ return this . unsubscribeCallback ;
37
42
} ) . pipe ( makeSingleton ( ) ) ;
38
43
}
39
44
You can’t perform that action at this time.
0 commit comments