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

Clean up DOM event listeners when destroying Map's animation proxy #9048

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 41 additions & 21 deletions src/map/Map.js
Expand Up @@ -159,7 +159,6 @@ export const Map = Evented.extend({
// happens after starting zoom animation (propagating to the map pane), we know that it ended globally
if (this._zoomAnimated) {
this._createAnimProxy();
DomEvent.on(this._proxy, 'transitionend', this._catchTransitionEnd, this);
}

this._addLayers(this.options.layers);
Expand Down Expand Up @@ -779,6 +778,8 @@ export const Map = Evented.extend({
this.fire('unload');
}

this._destroyAnimProxy();

let i;
for (i in this._layers) {
if (Object.hasOwn(this._layers, i)) {
Expand Down Expand Up @@ -1622,36 +1623,55 @@ export const Map = Evented.extend({
},

_createAnimProxy() {
this._proxy = DomUtil.create('div', 'leaflet-proxy leaflet-zoom-animated');
this._panes.mapPane.appendChild(this._proxy);

const proxy = this._proxy = DomUtil.create('div', 'leaflet-proxy leaflet-zoom-animated');
this._panes.mapPane.appendChild(proxy);
this.on('zoomanim', this._animateProxyZoom, this);
this.on('load moveend', this._animMoveEnd, this);

this.on('zoomanim', function (e) {
const transform = this._proxy.style.transform;
DomEvent.on(this._proxy, 'transitionend', this._catchTransitionEnd, this);
},

DomUtil.setTransform(this._proxy, this.project(e.center, e.zoom), this.getZoomScale(e.zoom, 1));
_animateProxyZoom(e) {
const transform = this._proxy.style.transform;

// workaround for case when transform is the same and so transitionend event is not fired
if (transform === this._proxy.style.transform && this._animatingZoom) {
this._onZoomTransitionEnd();
}
}, this);
DomUtil.setTransform(
this._proxy,
this.project(e.center, e.zoom),
this.getZoomScale(e.zoom, 1),
);

this.on('load moveend', this._animMoveEnd, this);
// workaround for case when transform is the same and so transitionend event is not fired
if (transform === this._proxy.style.transform && this._animatingZoom) {
this._onZoomTransitionEnd();
}
},

this._on('unload', this._destroyAnimProxy, this);
_animMoveEnd() {
const c = this.getCenter();
const z = this.getZoom();



DomUtil.setTransform(
this._proxy,
this.project(c, z),
this.getZoomScale(z, 1),
);
},

_destroyAnimProxy() {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
this._proxy.remove();
this.off('load moveend', this._animMoveEnd, this);
delete this._proxy;
},
// Just make sure this method is safe to call from anywhere, without knowledge
// of whether the animation proxy was created in the first place.
if (this._proxy) {
DomEvent.off(this._proxy, 'transitionend', this._catchTransitionEnd, this);

_animMoveEnd() {
const c = this.getCenter(),
z = this.getZoom();
DomUtil.setTransform(this._proxy, this.project(c, z), this.getZoomScale(z, 1));
this._proxy.remove();
this.off('zoomanim', this._animateProxyZoom, this);
this.off('load moveend', this._animMoveEnd, this);

delete this._proxy;
}
},

_catchTransitionEnd(e) {
Expand Down