@@ -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
@@ -117,6 +116,7 @@ export function watchPosition(
117
116
*/
118
117
export function clearWatch ( watchID : number ) {
119
118
const sub = subscriptions [ watchID ] ;
119
+ console . log ( 'clear watch' , watchID , sub ) ;
120
120
if ( ! sub ) {
121
121
// Silently exit when the watchID is invalid or already cleared
122
122
// This is consistent with timers
@@ -127,13 +127,9 @@ export function clearWatch(watchID: number) {
127
127
// array element refinements not yet enabled in Flow
128
128
const sub1 = sub [ 1 ] ;
129
129
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
- }
130
+
131
+ delete subscriptions [ watchID ] ;
132
+ let noWatchers = Object . keys ( subscriptions ) . length === 0 ;
137
133
if ( noWatchers ) {
138
134
stopObserving ( ) ;
139
135
}
@@ -146,18 +142,14 @@ export function clearWatch(watchID: number) {
146
142
*/
147
143
export function stopObserving ( ) {
148
144
if ( updatesEnabled ) {
145
+ console . log ( 'stopping observing' )
149
146
RNCGeolocation . stopObserving ( ) ;
150
147
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 = [ ] ;
148
+ Object . values ( subscriptions ) . forEach ( ( [ sub , sub1 ] ) => {
149
+ warning ( false , 'Called stopObserving with existing subscriptions.' ) ;
150
+ sub . remove ( ) ;
151
+ sub1 && sub1 . remove ( ) ;
152
+ } ) ;
153
+ subscriptions = { } ;
162
154
}
163
155
}
0 commit comments