Created
February 12, 2019 04:42
-
-
Save NetanelBasal/16b0a6386648f3c77ed8f7b927d94bea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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