Lightweight Event Dispatcher (umd support), allowing you dispatch anywhere in the code, very handy in callback hell situations, deep promises, or any other complicated computations. Integrated with callback memory (so you dont have to subscribe first to get your data.)
Install:
/$ npm i x-dispatcher
Why use it ?
- Avoid ugly callback > callback > callback hell!
- Avoid messy Promises
- Prefer clean, readable code hierarchy
- Easy to implement
- No dependencies
- UMD/esm support so you can use same declaration in most environments: (AMD, Node, or browser)
Example
// import { xdispatcher } from 'x-dispatcher/esm' when using esm or ts const xdispatcher = // /node or /umd can also be used const uid = `dispatch_job_1` // optional const DEBUG = true // optional const ds = // thanks to callback memory `next` can be called before subscribe! dsnext type: 'profile' data: address: 'xox' email: 'johndoe@email.xo' name: 'John Doe' company: 'Anonymous'
More Examples
Checkout ./examples.js
Version support
es5/es6 support for universal module, can use for: AMD, Node, or browser
const xdispatcher = // defaults to nodeconst xdispatcher = const xdispatcher = // umd/es6 <script src="..path/x-dispatcher/umd"> const xd = window // initialize new</script>
Methods
METHODS | RETURN | DESCRIPTION |
---|---|---|
subscribe( (data,uid,index)=> ) | self |
start listening for events, before or after next() . uid => default of provided id for this dispatcher. index => counts callback events. |
onComplete( (uid)=> ) | self |
when subscribe event is deleted, callback is initiated |
next( data )/emit( data ) | self |
send data to subscribe callback. Can be declared before subscribe was initialized, there are no timers, so do not worry about memory leaks! |
del()/delete()/unsubscribe() | self |
remove dispatcher from stack |
isActive() | boolean |
tells you if dispatcher is still active |
Stack
es5/es6, Javascript, lint, callback, chaining support, umd, commonjs, node.js
Contact
Have questions, or would like to submit feedback, contact eaglex