Skip to content

Commit 4b411b4

Browse files
chore: fix watch clearing issues
1 parent 99f5636 commit 4b411b4

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

ios/RNCGeolocation.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ - (void)enableBackgroundLocationUpdates
309309
RCT_REMAP_METHOD(startObserving, startObserving:(RNCGeolocationOptions)options)
310310
{
311311
checkLocationConfig();
312+
313+
if (_observingLocation) {
314+
[self stopObserving];
315+
}
312316

313317
// Select best options
314318
_observerOptions = options;
@@ -376,6 +380,12 @@ - (void)enableBackgroundLocationUpdates
376380
successBlock(@[_lastLocationEvent]);
377381
return;
378382
}
383+
384+
BOOL didPause = NO;
385+
if (_observingLocation) {
386+
[self stopObserving];
387+
didPause = YES;
388+
}
379389

380390
// Create request
381391
RNCGeolocationRequest *request = [RNCGeolocationRequest new];
@@ -400,6 +410,10 @@ - (void)enableBackgroundLocationUpdates
400410
[self beginLocationUpdatesWithDesiredAccuracy:accuracy
401411
distanceFilter:options.distanceFilter
402412
useSignificantChanges:options.useSignificantChanges];
413+
414+
if (didPause) {
415+
[self startObserving];
416+
}
403417
}
404418

405419
#pragma mark - CLLocationManagerDelegate

js/implementation.native.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import type {
2323

2424
const { RNCGeolocation, GeolocationEventEmitter } = GeolocationNativeInterface;
2525

26-
let subscriptions: (
27-
| [EmitterSubscription, EmitterSubscription | null]
28-
| undefined
29-
)[] = [];
26+
let subscriptions: {
27+
[key: number]: [EmitterSubscription, EmitterSubscription | null];
28+
} = {};
3029
let updatesEnabled = false;
3130

3231
/**
@@ -100,13 +99,13 @@ export function watchPosition(
10099
RNCGeolocation.startObserving(options);
101100
updatesEnabled = true;
102101
}
103-
const watchID = subscriptions.length;
104-
subscriptions.push([
102+
const watchID = Object.keys(subscriptions).length + 1000;
103+
subscriptions[watchID] = [
105104
GeolocationEventEmitter.addListener('geolocationDidChange', success),
106105
error
107106
? GeolocationEventEmitter.addListener('geolocationError', error)
108107
: null,
109-
]);
108+
];
110109
return watchID;
111110
}
112111

@@ -117,6 +116,7 @@ export function watchPosition(
117116
*/
118117
export function clearWatch(watchID: number) {
119118
const sub = subscriptions[watchID];
119+
console.log('clear watch', watchID, sub);
120120
if (!sub) {
121121
// Silently exit when the watchID is invalid or already cleared
122122
// This is consistent with timers
@@ -127,13 +127,9 @@ export function clearWatch(watchID: number) {
127127
// array element refinements not yet enabled in Flow
128128
const sub1 = sub[1];
129129
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;
137133
if (noWatchers) {
138134
stopObserving();
139135
}
@@ -146,18 +142,14 @@ export function clearWatch(watchID: number) {
146142
*/
147143
export function stopObserving() {
148144
if (updatesEnabled) {
145+
console.log('stopping observing')
149146
RNCGeolocation.stopObserving();
150147
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 = {};
162154
}
163155
}

0 commit comments

Comments
 (0)