Skip to content

Instantly share code, notes, and snippets.

@orloffv
Created October 31, 2014 09:27
Show Gist options
  • Save orloffv/4aa04d7d5f9af8a4be67 to your computer and use it in GitHub Desktop.
Save orloffv/4aa04d7d5f9af8a4be67 to your computer and use it in GitHub Desktop.
"use strict";
var {unload} = require("unload+");
/**
* Helper that adds event listeners and remembers to remove on unload
*/
exports.listen = function listen(window, node, event, func, capture) {
// Default to use capture
if (capture == null)
capture = true;
node.addEventListener(event, func, capture);
function undoListen() {
node.removeEventListener(event, func, capture);
}
// Undo the listener on unload and provide a way to undo everything
let undoUnload = unload(undoListen, window);
return function() {
undoListen();
undoUnload();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment