hook-dispatcher
v0.0.6
Published
Custom event listener dispatchers and hooks
Downloads
2
Maintainers
Readme
hook-dispatcher
Custom event listener dispatchers and hooks
1. Dispatcher
Single hook
| name | Description |
| --- | --- |
| duplicate | boolean
Duplicate functions are allowed, default = true |
| limit | int
Callback limit, default = 0 |
| length | int
Get all callback count |
| add | function(callback)
Add event function |
| once | function(callback)
Add single event function |
| remove | function(callback)
Remove event function |
| removeAll | function(cleanProperties = false)
Remove all event functions |
| dispatch | function(detail = {}, proxy = {})
Dispatch event |
| onInitial | set function
The function is called when first event function will be added |
| onRemovable | set function
The function is called when last event function will be removed |
| onCatch | set function
The function is called if event function throw error |
import {Dispatcher} from "hook-dispatcher";
let listen = Dispatcher();
2. Emitter
Multiple hooks
| name | Description |
| --- | --- |
| limit | int
Callback limit, default = 0 |
| length | int
Get all callback count |
| count | function(name)
Get callback count for {name} expression |
| add | function(name, callback)
Add event |
| once | function(name, callback)
Add single event |
| remove | function(name, callback)
Remove event |
| removeAll | function(cleanProperties = false)
Remove all events |
| dispatch | function(name, detail = {}, proxy = {})
Dispatch event |
| getInstance | static function(name)
Get Emitter instance |
import {Emitter} from "hook-dispatcher";
let emit = Emitter(); // or Emitter.getInstance("name")
3. DataEmitter
Hooks for storage
| name | Description |
| --- | --- |
| set | function(name, value, trigger = true)
Set new value |
| get | function(name)
Get value |
| getIs | function(name)
Check value exists |
| destroy | function(name)
Remove value |
| filter | function(name, callback)
Filter value before write |
| on | function(callback, name = false)
Add callback event |
| once | function(callback, name = false)
Add single callback event |
| off | function(callback, name = false)
Remove callback event |
| getData | function(asArray = false)
Get storage object |
| reload | function(object = {}, props = {})
Reload storage object |
| getInstance | static function(name)
Get DataEmitter instance |
import {DataEmitter} from "hook-dispatcher";
let data = DataEmitter(); // or DataEmitter.getInstance("name")
// 1. add listener callback for all
data.on(e => {
console.log(e);
});
// 2. add listener callback for "single"
data.on(e => {
console.log(e);
}, "single");
// set value
// trigger action 1
data.set("name", "value");
// trigger action 1 & 2
data.set("single", "value 2");
// ignore all trigger
data.set("single", "value 2", false);