Guest User

Untitled

a guest
Oct 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. export const listenTo = <K extends keyof DocumentEventMap>(
  2. type: K | K[],
  3. listener: (this: Document, ev: DocumentEventMap[K]) => any,
  4. options?: boolean | AddEventListenerOptions
  5. ) => {
  6. if (Array.isArray(type)) {
  7. type.forEach(typ => {
  8. document.addEventListener(typ, listener, options);
  9. });
  10.  
  11. return () => {
  12. type.forEach(typ => {
  13. document.removeEventListener(typ, listener, options);
  14. });
  15. };
  16. } else {
  17. document.addEventListener(type, listener, options);
  18. return () => document.removeEventListener(type, listener, options);
  19. }
  20. };
Add Comment
Please, Sign In to add comment