@@ -23,10 +23,9 @@ import type {
23
23
24
24
const { RNCGeolocation, GeolocationEventEmitter } = GeolocationNativeInterface ;
25
25
26
- let subscriptions : (
27
- | [ EmitterSubscription , EmitterSubscription | null ]
28
- | undefined
29
- ) [ ] = [ ] ;
26
+ let subscriptions : {
27
+ [ key : number ] : [ EmitterSubscription , EmitterSubscription | null ] ;
28
+ } = { } ;
30
29
let updatesEnabled = false ;
31
30
32
31
/**
@@ -100,13 +99,13 @@ export function watchPosition(
100
99
RNCGeolocation . startObserving ( options ) ;
101
100
updatesEnabled = true ;
102
101
}
103
- const watchID = subscriptions . length ;
104
- subscriptions . push ( [
102
+ const watchID = Object . keys ( subscriptions ) . length + 1000 ;
103
+ subscriptions [ watchID ] = [
105
104
GeolocationEventEmitter . addListener ( 'geolocationDidChange' , success ) ,
106
105
error
107
106
? GeolocationEventEmitter . addListener ( 'geolocationError' , error )
108
107
: null ,
109
- ] ) ;
108
+ ] ;
110
109
return watchID ;
111
110
}
112
111
@@ -127,13 +126,9 @@ export function clearWatch(watchID: number) {
127
126
// array element refinements not yet enabled in Flow
128
127
const sub1 = sub [ 1 ] ;
129
128
sub1 && sub1 . remove ( ) ;
130
- subscriptions [ watchID ] = undefined ;
131
- let noWatchers = true ;
132
- for ( let ii = 0 ; ii < subscriptions . length ; ii ++ ) {
133
- if ( subscriptions [ ii ] ) {
134
- noWatchers = false ; // still valid subscriptions
135
- }
136
- }
129
+
130
+ delete subscriptions [ watchID ] ;
131
+ let noWatchers = Object . keys ( subscriptions ) . length === 0 ;
137
132
if ( noWatchers ) {
138
133
stopObserving ( ) ;
139
134
}
@@ -148,16 +143,11 @@ export function stopObserving() {
148
143
if ( updatesEnabled ) {
149
144
RNCGeolocation . stopObserving ( ) ;
150
145
updatesEnabled = false ;
151
- for ( let ii = 0 ; ii < subscriptions . length ; ii ++ ) {
152
- const sub = subscriptions [ ii ] ;
153
- if ( sub ) {
154
- warning ( false , 'Called stopObserving with existing subscriptions.' ) ;
155
- sub [ 0 ] . remove ( ) ;
156
- // array element refinements not yet enabled in Flow
157
- const sub1 = sub [ 1 ] ;
158
- sub1 && sub1 . remove ( ) ;
159
- }
160
- }
161
- subscriptions = [ ] ;
146
+ Object . values ( subscriptions ) . forEach ( ( [ sub , sub1 ] ) => {
147
+ warning ( false , 'Called stopObserving with existing subscriptions.' ) ;
148
+ sub . remove ( ) ;
149
+ sub1 && sub1 . remove ( ) ;
150
+ } ) ;
151
+ subscriptions = { } ;
162
152
}
163
153
}
0 commit comments