Skip to content

Instantly share code, notes, and snippets.

@chekit
Last active June 19, 2020 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chekit/91bf9b7f0d599abf1c49bebbeafe15b5 to your computer and use it in GitHub Desktop.
Save chekit/91bf9b7f0d599abf1c49bebbeafe15b5 to your computer and use it in GitHub Desktop.
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
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment