Skip to content

Instantly share code, notes, and snippets.

@farskid
Last active April 15, 2021 10:20
Show Gist options
  • Save farskid/900d71e5822099f728091fb05a230cc4 to your computer and use it in GitHub Desktop.
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.
// ...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