Skip to content

Commit 546b906

Browse files
committed
(refs #463) cleanup event handlers when remove simplestyle
1 parent efb3469 commit 546b906

1 file changed

Lines changed: 44 additions & 13 deletions

File tree

src/lib/simplestyle.ts

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class SimpleStyle {
2121
private geojson;
2222
private map;
2323
private options;
24+
private _eventHandlers: { event: string; layer: string; handler: (...args) => void }[] = [];
2425

2526
constructor(geojson, options?) {
2627
this.setGeoJSON(geojson);
@@ -270,7 +271,7 @@ export class SimpleStyle {
270271
}
271272

272273
async setPopup(map, source) {
273-
map.on('click', source, async (e) => {
274+
const clickHandler = async (e) => {
274275
const center = turfCenter(e.features[0]).geometry.coordinates as [
275276
number,
276277
number,
@@ -284,17 +285,27 @@ export class SimpleStyle {
284285
.setHTML(sanitizedDescription)
285286
.addTo(map);
286287
}
287-
});
288+
};
288289

289-
map.on('mouseenter', source, (e) => {
290+
const mouseEnterHandler = (e) => {
290291
if (e.features[0].properties.description) {
291292
map.getCanvas().style.cursor = 'pointer';
292293
}
293-
});
294+
};
294295

295-
map.on('mouseleave', source, () => {
296+
const mouseLeaveHandler = () => {
296297
map.getCanvas().style.cursor = '';
297-
});
298+
};
299+
300+
map.on('click', source, clickHandler);
301+
map.on('mouseenter', source, mouseEnterHandler);
302+
map.on('mouseleave', source, mouseLeaveHandler);
303+
304+
this._eventHandlers.push(
305+
{ event: 'click', layer: source, handler: clickHandler },
306+
{ event: 'mouseenter', layer: source, handler: mouseEnterHandler },
307+
{ event: 'mouseleave', layer: source, handler: mouseLeaveHandler },
308+
);
298309
}
299310

300311
/**
@@ -325,9 +336,11 @@ export class SimpleStyle {
325336
},
326337
});
327338

328-
this.map.on('click', `${this.options.id}-clusters`, async (e) => {
339+
const clusterLayer = `${this.options.id}-clusters`;
340+
341+
const clusterClickHandler = async (e) => {
329342
const features = this.map.queryRenderedFeatures(e.point, {
330-
layers: [`${this.options.id}-clusters`],
343+
layers: [clusterLayer],
331344
});
332345
const clusterId = features[0].properties.cluster_id;
333346
const zoom = await this.map
@@ -338,19 +351,36 @@ export class SimpleStyle {
338351
center: features[0].geometry.coordinates,
339352
zoom: zoom,
340353
});
341-
});
354+
};
342355

343-
this.map.on('mouseenter', `${this.options.id}-clusters`, () => {
356+
const clusterEnterHandler = () => {
344357
this.map.getCanvas().style.cursor = 'pointer';
345-
});
358+
};
346359

347-
this.map.on('mouseleave', `${this.options.id}-clusters`, () => {
360+
const clusterLeaveHandler = () => {
348361
this.map.getCanvas().style.cursor = '';
349-
});
362+
};
363+
364+
this.map.on('click', clusterLayer, clusterClickHandler);
365+
this.map.on('mouseenter', clusterLayer, clusterEnterHandler);
366+
this.map.on('mouseleave', clusterLayer, clusterLeaveHandler);
367+
368+
this._eventHandlers.push(
369+
{ event: 'click', layer: clusterLayer, handler: clusterClickHandler },
370+
{ event: 'mouseenter', layer: clusterLayer, handler: clusterEnterHandler },
371+
{ event: 'mouseleave', layer: clusterLayer, handler: clusterLeaveHandler },
372+
);
350373
}
351374

352375
remove() {
376+
if (!this.map) return this;
353377
const id = this.options.id;
378+
379+
for (const { event, layer, handler } of this._eventHandlers) {
380+
this.map.off(event, layer, handler);
381+
}
382+
this._eventHandlers = [];
383+
354384
const layerIds = [
355385
`${id}-polygon-symbol`,
356386
`${id}-linestring-symbol`,
@@ -371,6 +401,7 @@ export class SimpleStyle {
371401
this.map.removeSource(sourceId);
372402
}
373403
}
404+
return this;
374405
}
375406

376407
setGeoJSON(geojson) {

0 commit comments

Comments
 (0)