0.3.9 • Published 9 years ago
obseriot v0.3.9
Simple observer pattern
Install
npm install obseriotUsage
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',
action ( collection, id, action ) {
// Some processing and formatting
return [ collection, id, action ]
}
}
}Listen and Notify
// obseriot.listen( event object , callback function )
obseriot.listen( urlChange, ( ...arg ) => {
console.log( arg ) // => 'shop', 1, 'detail'
} )
// obseriot.notify( event object , parameters )
obseriot.notify( urlChange, 'shop', 1, 'detail' )One time listener
// obseriot.once( event object , callback function )
obseriot.once( urlChange, ( ...arg ) => {
console.log( arg )
} )Remove listeners
Remove all registered listeners.
// obseriot.remove( event object )
obseriot.remove( urlChange )Remove one registered listener.
// obseriot.remove( event object, callback function )
const callback = ( ...arg ) => {
console.log( arg )
}
obseriot.listen( urlChange, callback ) // Listen to the named function.
obseriot.remove( urlChange, callback ) // Remove!How to use like Flux
Define Action
export const increment = {
handler: {
name: 'action_increment',
action ( num = 1 ) {
return [ num ]
}
}
}Define Store
import {increment} from './action/increment'
import obseriot from 'obseriot'
export const count = {
state: 0,
handler: {
name: 'store_count',
action () {
return [ count.state ]
}
}
}
obseriot.listen( increment, num => {
count.state = count.state + num
obseriot.notify( count )
} )Your Component
import {increment} from './action/increment'
import {count} from './store/count'
import obseriot from 'obseriot'
// Action in somewhere components
obseriot.notify( increment, 1 )
// Listen to Store update
obseriot.listen( count, newCount => {
console.log( newCount ) // => 1
} )0.3.9
9 years ago
0.3.8
9 years ago
0.3.7
9 years ago
0.3.6
9 years ago
0.3.5
9 years ago
0.3.4
9 years ago
0.3.3
9 years ago
0.3.2
9 years ago
0.3.1
9 years ago
0.3.0
9 years ago
0.2.0
9 years ago
0.1.16
10 years ago
0.1.15
10 years ago
0.1.14
10 years ago
0.1.13
10 years ago
0.1.12
10 years ago
0.1.111
10 years ago
0.1.11
10 years ago
0.1.1
10 years ago
0.1.0
10 years ago