Last active
April 15, 2021 10:20
-
-
Save farskid/900d71e5822099f728091fb05a230cc4 to your computer and use it in GitHub Desktop.
Conventional subscription API for DOM events. Subscriber returns an unsubscribe callback that once called, will detach the event handler.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ...args: [event, handler, capture] | |
function onEvent(element, ...args) { | |
element.addEventListener(...args); | |
return () => { | |
element.removeEventListener(...args); | |
} | |
} | |
const form = $('#form'); | |
const detachSubmit = onEvent(form, 'submit', submitForm}); | |
// need to detach? | |
detachSubmit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment