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 4b411b4
Show file tree
Hide file tree
Showing 2 changed files with 31 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
42 changes: 17 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 @@ -117,6 +116,7 @@ export function watchPosition(
*/
export function clearWatch(watchID: number) {
const sub = subscriptions[watchID];
console.log('clear watch', watchID, sub);
if (!sub) {
// Silently exit when the watchID is invalid or already cleared
// This is consistent with timers
Expand All @@ -127,13 +127,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 @@ -146,18 +142,14 @@ export function clearWatch(watchID: number) {
*/
export function stopObserving() {
if (updatesEnabled) {
console.log('stopping observing')

Check failure on line 145 in js/implementation.native.ts

View workflow job for this annotation

GitHub Actions / Linter

Insert `;`
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 4b411b4

Please sign in to comment.