mapbox / mapbox/mapbox-gl-js

Layer-specific handlers not removed when layer removed

Open
#7,782 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

api :memo: needs discussion :speech_balloon:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

**mapbox-gl-js version**: 0.51

**browser**:

### Steps to Trigger Behavior

```
map.addLayer({id: 'foo', ... })
map.on('click', 'foo', window.alert);
map.removeLayer('foo');
map.addLayer({id: 'foo', ... })
```

### Expected Behavior
The handler is removed when the layer is removed

### Actual Behavior
The handler is still present, so any new layer with the same ID has the same behaviour.layer now still shows alert.

I guess the documentation doesn't exactly promise that `map.on(event, layername, f)` ties the event to the layer, but that feels like the expectation to me. It was very surprising to me that old handlers stick around.

It's also problematic when you're repeatedly creating and destroying the same layer, because you end up with multiple copies of the handler.

Also, because there's no way to access existing handlers, nor any way to remove them other than by specifically passing the same handler, it becomes a bit annoying to actually have to manage the reference to these various handlers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the layer-specific map.on(event, layername, handler) registration and the map.removeLayer('foo') lifecycle described in the reproduction. Check how handlers are retained across removal and re-adding a layer with the same ID. Done means removed layers no longer trigger old handlers and repeated create/remove cycles do not accumulate duplicates.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.