Skip to content

Instantly share code, notes, and snippets.

@SergProduction
Last active January 22, 2017 11:47
Show Gist options
  • Save SergProduction/239505ee7d9e5fc828edb44298097cf5 to your computer and use it in GitHub Desktop.
Save SergProduction/239505ee7d9e5fc828edb44298097cf5 to your computer and use it in GitHub Desktop.
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