From 4937ec969d383e81f01dc65fa5d309b3e95d89b3 Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 12 Feb 2020 16:26:18 +1100 Subject: [PATCH] fix bug where onSuccess and onError callbacks were still being called after control was removed --- src/ui/control/geolocate_control.js | 10 ++++++++++ test/unit/ui/control/geolocate.test.js | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/ui/control/geolocate_control.js b/src/ui/control/geolocate_control.js index 88e17390471..417da1d594d 100644 --- a/src/ui/control/geolocate_control.js +++ b/src/ui/control/geolocate_control.js @@ -194,6 +194,11 @@ class GeolocateControl extends Evented { } _onSuccess(position: Position) { + if (!this._map) { + // control has since been removed + return; + } + if (this._isOutOfMapMaxBounds(position)) { this._setErrorState(); @@ -294,6 +299,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 diff --git a/test/unit/ui/control/geolocate.test.js b/test/unit/ui/control/geolocate.test.js index fe91812a5c3..6888edd87c8 100644 --- a/test/unit/ui/control/geolocate.test.js +++ b/test/unit/ui/control/geolocate.test.js @@ -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);