Skip to content

Commit e7deb5c

Browse files
authored
Fix several listener leaks in popups (#9498)
1 parent d912efb commit e7deb5c

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/ui/popup.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import {extend, bindAll} from '../util/util';
44
import {Event, Evented} from '../util/evented';
5+
import {MapMouseEvent} from '../ui/events';
56
import DOM from '../util/dom';
67
import LngLat from '../geo/lng_lat';
78
import Point from '@mapbox/point-geometry';
@@ -95,7 +96,7 @@ export default class Popup extends Evented {
9596
constructor(options: PopupOptions) {
9697
super();
9798
this.options = extend(Object.create(defaultOptions), options);
98-
bindAll(['_update', '_onClose', 'remove'], this);
99+
bindAll(['_update', '_onClose', 'remove', '_onMouseMove', '_onMouseUp', '_onDrag'], this);
99100
}
100101

101102
/**
@@ -105,6 +106,8 @@ export default class Popup extends Evented {
105106
* @returns {Popup} `this`
106107
*/
107108
addTo(map: Map) {
109+
if (this._map) this.remove();
110+
108111
this._map = map;
109112
if (this.options.closeOnClick) {
110113
this._map.on('click', this._onClose);
@@ -118,8 +121,8 @@ export default class Popup extends Evented {
118121
this._update();
119122

120123
if (this._trackPointer) {
121-
this._map.on('mousemove', (e) => { this._update(e.point); });
122-
this._map.on('mouseup', (e) => { this._update(e.point); });
124+
this._map.on('mousemove', this._onMouseMove);
125+
this._map.on('mouseup', this._onMouseUp);
123126
if (this._container) {
124127
this._container.classList.add('mapboxgl-popup-track-pointer');
125128
}
@@ -172,7 +175,9 @@ export default class Popup extends Evented {
172175
this._map.off('move', this._onClose);
173176
this._map.off('click', this._onClose);
174177
this._map.off('remove', this.remove);
175-
this._map.off('mousemove');
178+
this._map.off('mousemove', this._onMouseMove);
179+
this._map.off('mouseup', this._onMouseUp);
180+
this._map.off('drag', this._onDrag);
176181
delete this._map;
177182
}
178183

@@ -219,7 +224,7 @@ export default class Popup extends Evented {
219224

220225
if (this._map) {
221226
this._map.on('move', this._update);
222-
this._map.off('mousemove');
227+
this._map.off('mousemove', this._onMouseMove);
223228
if (this._container) {
224229
this._container.classList.remove('mapboxgl-popup-track-pointer');
225230
}
@@ -240,8 +245,8 @@ export default class Popup extends Evented {
240245
this._update();
241246
if (this._map) {
242247
this._map.off('move', this._update);
243-
this._map.on('mousemove', (e) => { this._update(e.point); });
244-
this._map.on('drag', (e) => { this._update(e.point); });
248+
this._map.on('mousemove', this._onMouseMove);
249+
this._map.on('drag', this._onDrag);
245250
if (this._container) {
246251
this._container.classList.add('mapboxgl-popup-track-pointer');
247252
}
@@ -409,6 +414,18 @@ export default class Popup extends Evented {
409414

410415
}
411416

417+
_onMouseUp(event: MapMouseEvent) {
418+
this._update(event.point);
419+
}
420+
421+
_onMouseMove(event: MapMouseEvent) {
422+
this._update(event.point);
423+
}
424+
425+
_onDrag(event: MapMouseEvent) {
426+
this._update(event.point);
427+
}
428+
412429
_update(cursor: PointLike) {
413430
const hasPosition = this._lngLat || this._trackPointer;
414431

0 commit comments

Comments
 (0)