Last active
June 19, 2020 13:11
-
-
Save chekit/91bf9b7f0d599abf1c49bebbeafe15b5 to your computer and use it in GitHub Desktop.
My pubsub module for future use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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