Skip to content

Commit

Permalink
Don't call onSuccess and onError callbacks after control was removed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewharvey committed Feb 12, 2020
1 parent f9b5ef7 commit 7de4857
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ui/control/geolocate_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ class GeolocateControl extends Evented {
}

_onSuccess(position: Position) {
if (!this._map) {
// control has since been removed
return;
}

if (this._isOutOfMapMaxBounds(position)) {
this._setErrorState();

Expand Down Expand Up @@ -293,6 +298,11 @@ class GeolocateControl extends Evented {
}

_onError(error: PositionError) {
if (!this._map) {
// control has since been removed
return;
}

if (this.options.trackUserLocation) {
if (error.code === 1) {
// PERMISSION_DENIED
Expand Down
11 changes: 11 additions & 0 deletions test/unit/ui/control/geolocate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ test('GeolocateControl geolocate fitBoundsOptions', (t) => {
geolocation.send({latitude: 10, longitude: 20, accuracy: 1});
});

test('GeolocateControl with removed before Geolocation callback', (t) => {
const map = createMap(t);
t.plan(0);

const geolocate = new GeolocateControl();
map.addControl(geolocate);
geolocate.trigger();
map.removeControl(geolocate);
t.end();
});

test('GeolocateControl non-zero bearing', (t) => {
t.plan(3);

Expand Down

0 comments on commit 7de4857

Please sign in to comment.