Skip to content

Restore DomEvent.off(el) functionality when calling with one arg - #7125

Merged
mourner merged 5 commits into
Leaflet:masterfrom
johnd0e:6013_domevent_off
Nov 5, 2021
Merged

mourner merged 5 commits into
Leaflet:masterfrom
johnd0e:6013_domevent_off

Conversation

@johnd0e

@johnd0e johnd0e commented May 5, 2020 •

Copy link
Copy Markdown
Collaborator

Currently we have code (unsuccessfully) trying to handle this case, see #6013 (comment)

This PR (originally based on #6032) fixes this case.

  1. Two more variations of off added:
// @alternative
// @function off(el: HTMLElement, types: String): this
// Removes all previously added listeners of given types.
// @alternative
// @function off(el: HTMLElement): this
// Removes all previously added listeners from given HTMLElement
  1. More strict arguments check to catch common bugs

  2. All cases are covered by tests.


This change is Reviewable

@johnd0e johnd0e linked an issue May 5, 2020 that may be closed by this pull request
@johnd0e
johnd0e force-pushed the 6013_domevent_off branch from 1efbac8 to 9bbe742 Compare May 5, 2020 13:44
@johnd0e
johnd0e marked this pull request as ready for review May 5, 2020 13:51
@johnd0e
johnd0e marked this pull request as draft May 5, 2020 16:10
@johnd0e
johnd0e force-pushed the 6013_domevent_off branch 4 times, most recently from 64d9a01 to 6d81342 Compare May 6, 2020 12:44
@johnd0e
johnd0e marked this pull request as ready for review May 6, 2020 12:52
@johnd0e johnd0e mentioned this pull request Jan 28, 2021
2 tasks done
@johnd0e
johnd0e force-pushed the 6013_domevent_off branch 3 times, most recently from 662a567 to 2d2f3e2 Compare March 18, 2021 12:29
@johnd0e
johnd0e requested a review from Falke-Design November 1, 2021 10:39

@Falke-Design Falke-Design left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be very helpful!

Comment thread src/dom/DomEvent.js
export function on(obj, types, fn, context) {

if (typeof types === 'object') {
if (types && typeof types === 'object') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a check if(!types) because now it will go into Util.splitWords(types); and throws an error (if types == undefined)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and throws an error (if types == undefined)

Because that is exactly what is desired - to catch user bug.

@mourner

mourner commented Nov 1, 2021

Copy link
Copy Markdown
Member

Same comment as in another PR — should we avoid using arguments to be on the safe side for performance?

Comment thread src/dom/DomEvent.js Outdated
Comment on lines +59 to +66
function batchRemove(obj, filterFn) {
for (var id in obj[eventsKey]) {
var type = id.split(/\d/)[0];
if (!filterFn || filterFn(type)) {
removeOne(obj, type, null, null, id);
}
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to move this function below off() because else the documentation can be irritating

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

caleblogan and others added 5 commits November 5, 2021 14:13
To prevent bugs when call such code with undefined `types` arg:
`L.DomEvent.off(el, types, ...)`

Note: `typeof null` is `object`, that's why need additional check for this case
// @Alternative
// @function off(el: HTMLElement, types: String): this
// Removes all previously added listeners of given types.
It was not only unneeded but even harmful, as it could mask real bugs.
@johnd0e

johnd0e commented Nov 5, 2021

Copy link
Copy Markdown
Collaborator Author

Same comment as in another PR — should we avoid using arguments to be on the safe side for performance?

@mourner
Ref: #7518 (review)

@johnd0e
johnd0e requested a review from mourner November 5, 2021 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DomEvent.off() not working correctly when called with one parameter

4 participants