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 global keyboard and window listeners are not removed after the map is destroyed #1434

Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions cypress/integration/mixins.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
describe('Should unbind event listeners that bound by the Mixins after the map is destroyed', () => {
it('KeyboardMixin', () => {
plainheart marked this conversation as resolved.
Show resolved Hide resolved
cy.window().then((window) => {
const { map, document } = window;
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved

map.remove();

const isWindowBlurEventUnbound = !Object.entries(
window._leaflet_events
).some(([name, handler]) => name.startsWith('blur') && handler);
expect(
isWindowBlurEventUnbound,
'window blur event listener is not unbound'
).to.eq(true);

const isKeyUpDownEventUnbound = !Object.entries(
document._leaflet_events
).some(([name, handler]) => name.startsWith('key') && handler);
expect(
isKeyUpDownEventUnbound,
'document keyboard event listener is not unbound'
).to.eq(true);
});
});
});
4 changes: 2 additions & 2 deletions src/js/L.PM.Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import GlobalDragMode from './Mixins/Modes/Mode.Drag';
import GlobalRemovalMode from './Mixins/Modes/Mode.Removal';
import GlobalRotateMode from './Mixins/Modes/Mode.Rotate';
import EventMixin from './Mixins/Events';
import KeyboardMixins from './Mixins/Keyboard';
import createKeyboardMixins from './Mixins/Keyboard';
import { getRenderer } from './helpers';

const Map = L.Class.extend({
Expand All @@ -20,7 +20,7 @@ const Map = L.Class.extend({
this.map = map;
this.Draw = new L.PM.Draw(map);
this.Toolbar = new L.PM.Toolbar(map);
this.Keyboard = KeyboardMixins;
this.Keyboard = createKeyboardMixins();

this.globalOptions = {
snappable: true,
Expand Down
13 changes: 10 additions & 3 deletions src/js/Mixins/Keyboard.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const KeyboardMixins = {
// use function to create a new mixin object for keeping isolation
// to make it work for multiple map instances
const createKeyboardMixins = () => ({
_lastEvents: { keydown: undefined, keyup: undefined, current: undefined },
_initKeyListener(map) {
this.map = map;
L.DomEvent.on(document, 'keydown keyup', this._onKeyListener, this);
L.DomEvent.on(window, 'blur', this._onBlur, this);
// clean up global listeners when current map instance is destroyed
map.on('unload', () => {
L.DomEvent.off(document, 'keydown keyup', this._onKeyListener, this);
L.DomEvent.off(window, 'blur', this._onBlur, this);
});
},
_onKeyListener(e) {
plainheart marked this conversation as resolved.
Show resolved Hide resolved
let focusOn = 'document';
Expand Down Expand Up @@ -44,6 +51,6 @@ const KeyboardMixins = {
getPressedKey() {
return this._lastEvents.current?.event.key;
},
};
});

export default KeyboardMixins;
export default createKeyboardMixins;
Loading