Simple observer pattern
Install
npm install obseriot
Usage
Define event
Object with a handler.name
and handler.action
.
handler.name
=> Event namehandler.action
=> Return the parameters to be provided to the listener- Array is provided as a variadic argument
- Can be provide a variety of types. String, Object, Function, whatever
const urlChange = handler: name: 'url_change' { // Some processing and formatting return collection id action }
Listen and Notify
// obseriot.listen( event object , callback function )obseriot // obseriot.notify( event object , parameters )obseriot
One time listener
// obseriot.once( event object , callback function )obseriot
Remove listeners
Remove all registered listeners.
// obseriot.remove( event object )obseriot
Remove one registered listener.
// obseriot.remove( event object, callback function )const callback = { console}obseriot // Listen to the named function.obseriot // Remove!
How to use like Flux
Define Action
const increment = handler: name: 'action_increment' { return num }
Define Store
const count = state: 0 handler: name: 'store_count' { return countstate } obseriot
Your Component
// Action in somewhere componentsobseriot // Listen to Store updateobseriot