Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. const Events = (() => {
  2. const topics = {};
  3. const hOP = topics.hasOwnProperty;
  4.  
  5. return {
  6. subscribe(topic, listener) {
  7. // Create the topic's object if not yet created
  8. if(!hOP.call(topics, topic)) topics[topic] = [];
  9.  
  10. // Add the listener to queue
  11. let index = topics[topic].push(listener) -1;
  12.  
  13. // Provide handle back for removal of topic
  14. return {
  15. remove() {
  16. delete topics[topic][index];
  17. }
  18. };
  19. },
  20. publish(topic, info) {
  21. // If the topic doesn't exist,
  22. // or there's no listeners in queue,
  23. // just leave
  24. if(!hOP.call(topics, topic)){ return; }
  25.  
  26. // Cycle through topics queue, fire!
  27. topics[topic].forEach(item => {
  28. item(info != undefined ? info : {});
  29. });
  30. }
  31. };
  32. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement