chekit
4/11/2016 - 5:22 PM

My pubsub module for future use

My pubsub module for future use

//Example: http://jsbin.com/ciguhe/edit?js,console

'use strict';

module.exports = (function () {
  let events = {};

  function publish (evt, val) {
    events[evt].forEach((fn) => fn(val));
  }
  
  function subscribe (evt, ...fn) {
    events[evt] = events[evt] || [];
    
    Array.prototype.forEach.call(fn, (item) => events[evt].push(item));
  }
  
  function unsubscribe (evt, fn) {
    events[evt] = events[evt].filter((item) => item !== fn);
  }
  
  return {
    publish,
    subscribe,
    unsubscribe
  }
})();