Last active
January 22, 2017 11:47
-
-
Save SergProduction/239505ee7d9e5fc828edb44298097cf5 to your computer and use it in GitHub Desktop.
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
const Emitter = () => { | |
let store = [] | |
let subscribe = ( name, fn ) => { | |
store.push({name, fn}) | |
return () => { | |
store = store.find( el => el.name != name); | |
} | |
} | |
let publish = ( name, data) => store.forEach( obj =>{ | |
if(obj.name == name) obj.fn( data ) | |
}) | |
return {subscribe, publish} | |
} | |
var a = Emitter(); | |
let delBla = a.subscribe('bla', (d) => { | |
console.log(d) | |
}) | |
setTimeout(()=>{ | |
a.publish('bla', '+100500') | |
},5 * 1000) | |
setTimeout(()=>{ | |
delBla() | |
},4 * 1000) | |
setTimeout(()=>{ | |
a.publish('bla', '-200300') | |
},3 * 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment