Skip to content

Instantly share code, notes, and snippets.

@brenapp
Last active March 27, 2017 21:51
Show Gist options
  • Save brenapp/b9c44cb54ce4084ff7f44a60c87b4766 to your computer and use it in GitHub Desktop.
Save brenapp/b9c44cb54ce4084ff7f44a60c87b4766 to your computer and use it in GitHub Desktop.
Super small (150b) pub-sub with some cool features

Usage:

var ee = pub();
ee.on('*', (p, e) => console.log(`${e} trigged: ${p}`)); // Called for all events (returns all * listeners)
ee.on("load", d = (t, e) => console.log(`Page loaded in ${t}ms`)); // Returns an array of all listeners for that event (minus * listeners)
ee.emit("load", 2323); // Returns the return values of the .on() functions, in this order: [...specific listeners, ...* listeners]
ee.off("load", d); // Returns the new array of listeners
pub=(_=>(e={})=>({on:(a,b)=>e[a]?e[a].push(b):e[a]=[b],emit:(a,b)=>(e[a]||[]).concat(e['*']).map(f=>f(b,a)),off:(a,b)=>e[a]=e[a].filter(f=>b!=f)}))()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment