This is a simple, extremely lightweight, zero-dependency event/pubsub system for node or the browser.
It's pronounced like "stroganoff", but "emit" instead of "stroge".
It gives your objects these really basic functions:
emit
- publish an eventon
- subscribe to an eventoff
- unsubscribe an event
Also available:
pause
- queue incomingemit()
while you are waiting for something (sometimes needed for testing)resume
- run all the paused queue and processemit()
normally, again
install
nodejs/webpack/browserify
npm install --save emitonoff
then, in your code:
var emitonoff = ;
plain browser global
You can use a CDN:
or alternately use one of the files in dist/
, locally.
usage details
var kitty = { this; } { this; } ; ; { console;} { console} kitty;kitty; kitty;kitty;kitty;kitty;kitty;
You can also use it without an object, like this:
var kitty = ; { console;} { console} kitty;kitty; kitty;kitty;kitty;
react
You can also use it with react (as a simpler alternative to flux, redux, thunk, etc.)
Here is an example:
// this could also be exported from a seperate file for sharing with other codeconst state = const App = <div className='App'>APP statecounter</div> ReactDOM // async re-renders
You can also make lots of little bound components, with different triggers and differnt stores!
// this could also be exported from a seperate file for sharing with other codeconst state = counter: random: const App = <div className='App'> APP <EmitOnOff state=statecounter trigger='update'> counter: statecountercounter </EmitOnOff> <EmitOnOff state=staterandom trigger='new'> random: staterandomvalue </EmitOnOff> </div> ReactDOM // async re-renders
Note: using the standard render
event trigger everywhere, and a single store, in general, will probably simplify things but do what you like!
browser support
I haven't done too much browser-testing, but it should work on any browser that can Array.slice
(all of them in the last 15 years.) If you need it to work on a particular old browser, file a bug and I will add support (or tell you t find a Array.slice
polyfill.)
It works in IE5.5.