Skip to content

Commit

Permalink
chore: fix watch clearing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
michalchudziak committed Sep 1, 2024
1 parent 99f5636 commit 6013d52
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
14 changes: 14 additions & 0 deletions ios/RNCGeolocation.mm
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ - (void)enableBackgroundLocationUpdates
RCT_REMAP_METHOD(startObserving, startObserving:(RNCGeolocationOptions)options)
{
checkLocationConfig();

if (_observingLocation) {
[self stopObserving];
}

// Select best options
_observerOptions = options;
Expand Down Expand Up @@ -376,6 +380,12 @@ - (void)enableBackgroundLocationUpdates
successBlock(@[_lastLocationEvent]);
return;
}

BOOL didPause = NO;
if (_observingLocation) {
[self stopObserving];
didPause = YES;
}

// Create request
RNCGeolocationRequest *request = [RNCGeolocationRequest new];
Expand All @@ -400,6 +410,10 @@ - (void)enableBackgroundLocationUpdates
[self beginLocationUpdatesWithDesiredAccuracy:accuracy
distanceFilter:options.distanceFilter
useSignificantChanges:options.useSignificantChanges];

if (didPause) {
[self startObserving];
}
}

#pragma mark - CLLocationManagerDelegate
Expand Down
40 changes: 15 additions & 25 deletions js/implementation.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ import type {

const { RNCGeolocation, GeolocationEventEmitter } = GeolocationNativeInterface;

let subscriptions: (
| [EmitterSubscription, EmitterSubscription | null]
| undefined
)[] = [];
let subscriptions: {
[key: number]: [EmitterSubscription, EmitterSubscription | null];
} = {};
let updatesEnabled = false;

/**
Expand Down Expand Up @@ -100,13 +99,13 @@ export function watchPosition(
RNCGeolocation.startObserving(options);
updatesEnabled = true;
}
const watchID = subscriptions.length;
subscriptions.push([
const watchID = Object.keys(subscriptions).length + 1000;
subscriptions[watchID] = [
GeolocationEventEmitter.addListener('geolocationDidChange', success),
error
? GeolocationEventEmitter.addListener('geolocationError', error)
: null,
]);
];
return watchID;
}

Expand All @@ -127,13 +126,9 @@ export function clearWatch(watchID: number) {
// array element refinements not yet enabled in Flow
const sub1 = sub[1];
sub1 && sub1.remove();
subscriptions[watchID] = undefined;
let noWatchers = true;
for (let ii = 0; ii < subscriptions.length; ii++) {
if (subscriptions[ii]) {
noWatchers = false; // still valid subscriptions
}
}

delete subscriptions[watchID];
let noWatchers = Object.keys(subscriptions).length === 0;
if (noWatchers) {
stopObserving();
}
Expand All @@ -148,16 +143,11 @@ export function stopObserving() {
if (updatesEnabled) {
RNCGeolocation.stopObserving();
updatesEnabled = false;
for (let ii = 0; ii < subscriptions.length; ii++) {
const sub = subscriptions[ii];
if (sub) {
warning(false, 'Called stopObserving with existing subscriptions.');
sub[0].remove();
// array element refinements not yet enabled in Flow
const sub1 = sub[1];
sub1 && sub1.remove();
}
}
subscriptions = [];
Object.values(subscriptions).forEach(([sub, sub1]) => {
warning(false, 'Called stopObserving with existing subscriptions.');
sub.remove();
sub1 && sub1.remove();
});
subscriptions = {};
}
}

0 comments on commit 6013d52

Please sign in to comment.