Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. //
  2. // const emitter = EventEmitter();
  3. // emitter.on('event', (...args) => console.log(...args));
  4. //
  5.  
  6. const addHandler = (handlers, handler) => (
  7. handlers != null
  8. ? [...handlers, handler]
  9. : [handler]
  10. );
  11.  
  12. const call = args => fn => fn(...args);
  13.  
  14. const callHandlers = (handlers, args) => (
  15. handlers != null
  16. ? handlers.forEach(call(args))
  17. : void 0
  18. );
  19.  
  20. const on = handlers => (event, handler) => handlers.set(
  21. event,
  22. addHandler(
  23. handlers.get(event), handler
  24. )
  25. );
  26.  
  27. const emit = handlers => (event, ...args) => callHandlers(
  28. handlers.get(event),
  29. args
  30. );
  31.  
  32. const EmitterMethods = handlers => ({
  33. on: on(handlers),
  34. emit: emit(handlers),
  35. });
  36.  
  37. export const EventEmitter = () => EmitterMethods(new Map());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement