obzervable
![build status](https://travis-ci.org/voodoo-child/observable.svg?branch=master)
Create observable states.
Documentation
Documentation and usage examples: documentation
Example
Counter
import { createState, observe } from 'obzervable'
const State = createState( {
count: 0,
active: false
} )
const Counter = document.createElement( 'h1' )
const StartButton = document.createElement('button')
const ResetButton = document.createElement('button')
const Body = document.getElementsByTagName('body')[0]
ResetButton.innerText = 'reset'
Body.appendChild( Counter )
Body.appendChild( StartButton )
Body.appendChild( ResetButton )
observe( State, ( { count } ) => ( { count } ) )( ( { count } ) => {
return Counter.innerText = count
} );
( ( interval ) => {
observe( State, ( { active } ) => ( { active } ) )( ( { active } ) => {
if ( active ) {
interval = setInterval( () => State.count += 1, 1000)
} else {
clearInterval( interval )
}
Button.innerText = ( active ) ? 'stop' : 'start'
return Counter.style.color = ( active ) ? 'green' : 'red'
} )
} )( null )
StartButton.addEventListener( 'click', ( e ) => {
const { active } = State
State.active = !active
} )
ResetButton.addEventListener( 'click', ( e ) => {
State.apply( {
count: 0,
active: false
} )
} )