Skip to content

Instantly share code, notes, and snippets.

@codemilli
Created July 24, 2016 09:22
Show Gist options
  • Save codemilli/ce0eac669773c3a9d293154ada929f13 to your computer and use it in GitHub Desktop.
Save codemilli/ce0eac669773c3a9d293154ada929f13 to your computer and use it in GitHub Desktop.
function listen(element, eventName) {
return new Observable(observer => {
// Create an event handler which sends data to the sink
let handler = event => observer.next(event);
// Attach the event handler
element.addEventListener(eventName, handler, true);
// Return a function which will cancel the event stream
return () => {
// Detach the event handler from the element
element.removeEventListener(eventName, handler, true);
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment