Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix clear watch logic #329

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {};
}
}
Loading