How is using an AbortController any more ergonomic/better than just using removeEventListener? I use removeEventListener often enough in effect cleanup functions (VDOM frameworks). I didn't realize it was out of style
For a single callback, it's not just different, but when you've got multiple callbacks that all need to be removed at once, it's usually cleaner to use signals because then the cleanup just becomes one call instead of several. Also when you need to remove callbacks in several places rather than a single one, that also becomes easier because you can't forget a callback in one place but not another - you just always call the abort function.
Are you suggesting multiple listeners sharing an AbortController? That's not something I've ever done before (or knew could be done), but I can definitely see the utility of it.
You’re carrying a single value instead of 4 (element, event, callback, capture mode), you can hook abort signals into more locations if you want to sync up the removal with other things, you can receive cancellation notifications, …
I used to use it a lot but also it was always super annoying to use since you can't pass an inline function anymore once you need it. Always made me grumpy
Otherwise removeEventListener doesn't make sense because add doesn't return a handle.
This is AIUI one of the earliest instances of a standard JS function where identity mattered. Outside event listener removal and before Map and friends (don't get me started on custom elements constructors), Javascript was, aspirationally at least, structurally typed.
Hasn't JS always been fairly identity-focused? There are many forms of equality defined, but they're all reference-based, none test for structural equality.
I'd say JS is more duck-typed than structurally typed. If you have an object representing a certain behaviour then you can pass it to anything that accepts objects of that shape. But if you have an object representing data in a particular shape, then it becomes a lot harder to work with at a structural level.
Javascript was, aspirationally at least, structurally typed.
How do you figure? Afaik JavaScript as a langage had no relevant structural typing until symbols were introduced, and that arrived at about the same time as Map and friends. And to my knowledge it still does not have any sort of “structural identity” e.g. deep equality is not a thing that’s built in ECMAScript, unlike identity checks of various flavours.
Would be fun to see data about how many people actually use
removeEventListener.I’d assume not many these days, given
onceandsignalcover most of the cases where you’d want to remove a listener.How is using an AbortController any more ergonomic/better than just using removeEventListener? I use removeEventListener often enough in effect cleanup functions (VDOM frameworks). I didn't realize it was out of style
For a single callback, it's not just different, but when you've got multiple callbacks that all need to be removed at once, it's usually cleaner to use signals because then the cleanup just becomes one call instead of several. Also when you need to remove callbacks in several places rather than a single one, that also becomes easier because you can't forget a callback in one place but not another - you just always call the abort function.
Are you suggesting multiple listeners sharing an AbortController? That's not something I've ever done before (or knew could be done), but I can definitely see the utility of it.
You’re carrying a single value instead of 4 (element, event, callback, capture mode), you can hook abort signals into more locations if you want to sync up the removal with other things, you can receive cancellation notifications, …
I bet it's in almost zero codebases and almost every dependency tree.
I used to use it a lot but also it was always super annoying to use since you can't pass an inline function anymore once you need it. Always made me grumpy
Otherwise removeEventListener doesn't make sense because add doesn't return a handle.
This is AIUI one of the earliest instances of a standard JS function where identity mattered. Outside event listener removal and before Map and friends (don't get me started on custom elements constructors), Javascript was, aspirationally at least, structurally typed.
Hasn't JS always been fairly identity-focused? There are many forms of equality defined, but they're all reference-based, none test for structural equality.
I'd say JS is more duck-typed than structurally typed. If you have an object representing a certain behaviour then you can pass it to anything that accepts objects of that shape. But if you have an object representing data in a particular shape, then it becomes a lot harder to work with at a structural level.
How do you figure? Afaik JavaScript as a langage had no relevant structural typing until symbols were introduced, and that arrived at about the same time as Map and friends. And to my knowledge it still does not have any sort of “structural identity” e.g. deep equality is not a thing that’s built in ECMAScript, unlike identity checks of various flavours.
I don’t see this as a quirk like the post goes about. And read it like “register this reference for a function in this event”.
If two of these are done, I expect one of them to be a nop:
“register this ref in this event” “register this ref in this event” (already done, nothing to see here)
Oops, corrupted state.
Data doesn't have to be unique in order to need repetition consistency.