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

Fix: unable to remove listener with layer(s) registered with Map#once #4592

Merged
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 @@ -7,6 +7,7 @@
### 🐞 Bug fixes

- Heatmap Fix for 3D terrain ([#4571](https://github.com/maplibre/maplibre-gl-js/pull/4571))
- Fix Map#off to not remove listener with layer(s) registered with Map#once ([#4592](https://github.com/maplibre/maplibre-gl-js/pull/4592))
- _...Add new stuff here..._

## 4.6.0
Expand Down
16 changes: 15 additions & 1 deletion src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ export type CompleteMapOptions = Complete<MapOptions>;
type DelegatedListener = {
layers: string[];
listener: Listener;
delegates: {[type in keyof MapEventType]?: (e: any) => void};
delegates: {[type in keyof MapEventType]?: Delegate};
HarelM marked this conversation as resolved.
Show resolved Hide resolved
}

type Delegate = (e: any) => void;

const defaultMinZoom = -2;
const defaultMaxZoom = 22;

Expand Down Expand Up @@ -1496,6 +1498,18 @@ export class Map extends Camera {

const delegatedListener = this._createDelegatedListener(type, layerIds, listener);

for (const key in delegatedListener.delegates) {
const delegate: Delegate = delegatedListener.delegates[key];
delegatedListener.delegates[key] = (...args: Parameters<Delegate>) => {
this._removeDelegatedListener(type, layerIds, listener);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is that in this._removeDelegatedListener the Map#off is called. On the other hand here we have reference to delegate, so it shouldn't harm. As a solution for that, we can add parameter, to skip calling off in this._removeDelegatedListener.

delegate(...args);
};
}

this._delegatedListeners = this._delegatedListeners || {};
HarelM marked this conversation as resolved.
Show resolved Hide resolved
this._delegatedListeners[type] = this._delegatedListeners[type] || [];
this._delegatedListeners[type].push(delegatedListener);

for (const event in delegatedListener.delegates) {
this.once(event, delegatedListener.delegates[event]);
}
Expand Down
15 changes: 15 additions & 0 deletions src/ui/map_tests/map_events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,21 @@ describe('map events', () => {
expect(spy).toHaveBeenCalledTimes(1);
});

test('Map#off removes listener registered with Map#once', () => {
const map = createMap();

jest.spyOn(map, 'getLayer').mockReturnValue({} as StyleLayer);
jest.spyOn(map, 'queryRenderedFeatures').mockReturnValue([{} as MapGeoJSONFeature]);

const spy = jest.fn();

map.once('click', 'layer', spy);
map.off('click', 'layer', spy);
simulate.click(map.getCanvas());

expect(spy).not.toHaveBeenCalled();
});

(['mouseenter', 'mouseover'] as (keyof MapLayerEventType)[]).forEach((event) => {
test(`Map#on ${event} does not fire if the specified layer does not exist`, () => {
const map = createMap();
Expand Down