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

#4268 fix async race when adding/removing geolocate rapidly #4454

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Fix lag on fast map zoom ([#4366](https://github.com/maplibre/maplibre-gl-js/pull/4366))
- Fix unguarded read access to possibly undefined object ([#4431](https://github.com/maplibre/maplibre-gl-js/pull/4431))
- Fix remove hash string when map is removed ([#4427](https://github.com/maplibre/maplibre-gl-js/pull/4427))
- Fix GeolocateControl may be added twice when calling addControl/removeControl/addControl rapidly ([#4454](https://github.com/maplibre/maplibre-gl-js/pull/4454))
- Fix `style.loadURL` abort error being logged when removing style ([#4425](https://github.com/maplibre/maplibre-gl-js/pull/4425))
- _...Add new stuff here..._

Expand Down
16 changes: 13 additions & 3 deletions src/ui/control/geolocate_control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ export class GeolocateControl extends Evented implements IControl {
onAdd(map: Map) {
this._map = map;
this._container = DOM.create('div', 'maplibregl-ctrl maplibregl-ctrl-group');
checkGeolocationSupport().then((supported) => this._setupUI(supported));
this._setupUI();
checkGeolocationSupport().then((supported) => this._finishSetupUI(supported));
return this._container;
}

Expand Down Expand Up @@ -523,8 +524,7 @@ export class GeolocateControl extends Evented implements IControl {
this._timeoutId = undefined;
};

_setupUI = (supported: boolean) => {
// this method is called asynchronously during onAdd
_setupUI = () => {
// the control could have been removed before reaching here
if (!this._map) {
return;
Expand All @@ -534,6 +534,15 @@ export class GeolocateControl extends Evented implements IControl {
this._geolocateButton = DOM.create('button', 'maplibregl-ctrl-geolocate', this._container);
DOM.create('span', 'maplibregl-ctrl-icon', this._geolocateButton).setAttribute('aria-hidden', 'true');
this._geolocateButton.type = 'button';
this._geolocateButton.disabled = true;
};

_finishSetupUI = (supported: boolean) => {
// this method is called asynchronously during onAdd
if (!this._map) {
// control has since been removed
return;
}

if (supported === false) {
warnOnce('Geolocation support is not available so the GeolocateControl will be disabled.');
Expand All @@ -543,6 +552,7 @@ export class GeolocateControl extends Evented implements IControl {
this._geolocateButton.setAttribute('aria-label', title);
} else {
const title = this._map._getUIString('GeolocateControl.FindMyLocation');
this._geolocateButton.disabled = false;
this._geolocateButton.title = title;
this._geolocateButton.setAttribute('aria-label', title);
}
Expand Down
15 changes: 15 additions & 0 deletions test/integration/browser/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,19 @@ describe('Browser tests', () => {
expect(center.lng).toBeCloseTo(11.39770);
expect(center.lat).toBeCloseTo(47.29960);
}, 20000);

HarelM marked this conversation as resolved.
Show resolved Hide resolved
test('Geolocate control should appear only once', async () => {
nkundu marked this conversation as resolved.
Show resolved Hide resolved
await page.evaluate(async () => {
const geolocateControl = new maplibregl.GeolocateControl({});

map.addControl(geolocateControl);
map.removeControl(geolocateControl);
map.addControl(geolocateControl);

await map.once('idle');
});

const geolocateUIelem = await page.$$('.maplibregl-ctrl-geolocate');
expect(geolocateUIelem).toHaveLength(1);
}, 20000);
});