Skip to content

Instantly share code, notes, and snippets.

@brigand
Last active September 1, 2020 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brigand/29d6eb8e6b04d7d90da763ddc8e9df00 to your computer and use it in GitHub Desktop.
Save brigand/29d6eb8e6b04d7d90da763ddc8e9df00 to your computer and use it in GitHub Desktop.
domOn, domOnce
function domOn(subject, eventName, handler, options = false) {
// This function is useless except in the case domOn(x, 'e', f); domOn(x, 'e', f); is run
// where cleanup can remove the distinct 'inner' function from each call, instead of both 'f' listeners.
function inner(event) {
return handler.call(this, event);
}
subject.addEventListener(eventName, inner, options);
const cleanup = () => subject.removeEventListener(eventName, inner, options);
return cleanup;
}
function domOnce(subject, eventName, handler, options = false) {
const cleanup = domOn(subject, eventName, function (event) {
cleanup();
return handler(event);
}, options);
return cleanup;
}
domOnce(session, 'end', () => {
// TODO: implement
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment