ETX
A js super Event Trigger manager.
Install
npm i etx
Import
ES6:
CommonJS:
const Etx =
AMD:
Normal Browsers:
Usage
const etx = etx//...etx
API
on(event, callback, priority?)
- event: string, event name
- callback: function, should be bound function if needed
- priority: number, the bigger the earlier, default 10
etx etx
Callback function parameters:
- e: a object which have some information about current event
- target: event name which passed by
emit
- event: event name which passed by
on
- callback: event callback
- priority: event priority
- broadcast: is broadcasting?
- preventDefault: function, when invoked, the left callbacks of current event will not run
- stopPropagation: fuction, when invoked, callbacks of children's and parents' will not run, not contains roots' callbacks
- stopImmediatePropagation: function, preventDefault + stopPropagation, and roots' callbacks will not run
- stack: code stack, you can use it for debug
- target: event name which passed by
- ...args: which passed by
emit
event name rules
Use .
to concat deep path.
etx etx // fn not invokedetx // fn invoked
Use *
to stand for root binding. All emits will trigger the callback of *
, unless you call stopImmediatePropagation
inside one of callbacks.
etx
off(event, callback?)
If you do not pass callback, all callbacks of this event will be removed.
Notice: you should must off etx' callbacks when you do not need them!!!
once(event, callback, priority?)
The same as on
, callback will only run once, after it is executed, it will be offed.
contain(event, callback?)
Whether a event and callback is registered into the Etx instance.
if etx // ...
emit(broadcast?, event, ...args)
Trigger callback functions of this event by passing arguments. Will propagete to parents and roots.
args
will be received by on
callback function.
- broadcast: whether to broadcast to children? default false
- event: event name
etxetxetxetx etx // 2 1 0etx // 2 3 1 0 // broadcast to children before propagate to parents
dispatch(broadcast?, event, ...args)
Like emit
, but use async callback functions and return a promise:
etxetxetx await etx
For this code block, f2 will run after f1 resolved, f3 is the same will run after f2 resolved. If f1 rejected, f2 and f3 will not run any more.
Notice: callback function can be or not be async function.
distribute(broadcast?, event, ...args)
Like dispatch
but parallel in each level.
etxetxetx await etx
Each level (current, propagation, root) callbacks will run in parallel. Only after all callbacks resolved, the callback in then will run. If one of callbacks rejected, it not affect others, but the whole process will be rejected finally.
Notice: callback function can be or not be async function.
silent(func|bool|array)
Disable trigger callbacks in fn
.
etx
Or you can pass a boolean to switch silent mode.
etxetxetx
Or an array which contains events which want to disable.
etxetxetxetx
The events in passed array should extract match the emit event names.
scope(func|bool)
Only trigger self's callbacks in fn
, never propagate.
etxscope { etx // parents and roots will not be triggered}
destroy()
Destory the instance.