Skip to content

Instantly share code, notes, and snippets.

@robinpokorny
Last active January 17, 2017 13:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinpokorny/dd97bd013dc5198a5bd0556c591f661c to your computer and use it in GitHub Desktop.
Save robinpokorny/dd97bd013dc5198a5bd0556c591f661c to your computer and use it in GitHub Desktop.
📢 Dead simple tweetable JavaScript Emitter pattern module using Map (ES2015)
import pubsub from './pubsub'
// Import the dead simple tweetable JavaScript PubSub pattern module using Set (ES2015)
// See https://gist.github.com/robinpokorny/d743ed9e0bc5214f79076a16c8e44a8f
export default () => {
const events = new Map()
const on = (name, fn) => {
if (!events.has(name)) events.set(name, pubsub())
return events.get(name).sub(fn)
}
const emit = (name, data) => events.has(name) && events.get(name).pub(data)
return Object.freeze({ on, emit })
}
import p from './pubsub'
// Minified (by hand), 139 bytes
export default e=>(e=new Map, Object.freeze({on:(n,f)=>(e.has(name)||e.set(n,p()),e.get(n).sub(f)),emit:(n,d)=>e.has(n)&&e.get(n).pub(d)}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment