Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Created February 12, 2019 04:42
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 NetanelBasal/16b0a6386648f3c77ed8f7b927d94bea to your computer and use it in GitHub Desktop.
Save NetanelBasal/16b0a6386648f3c77ed8f7b927d94bea to your computer and use it in GitHub Desktop.
const eventDelegation = {
map: new Map(),
init: false,
supports(eventName: string) {
return eventName.includes("delegate");
},
addEventListener(
element,
eventName,
originalHandler
) {
this.map.set(element, originalHandler);
if (!this.init) {
document.addEventListener("click", e => {
this.map.forEach((handler, element) => {
if (element.contains(e.target)) {
// In real-life only the handler should run inside the zone
handler(e);
}
});
});
this.init = true;
}
return () => {
this.map.delete(element);
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment