Socket
Socket
Sign inDemoInstall

obzervable

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    obzervable

![build status](https://travis-ci.org/voodoo-child/obzervable.svg?branch=master)


Version published
Maintainers
1
Install size
383 kB
Created

Readme

Source

obzervable

build status

obzervable is a library for creating observable mutable states.

  • Works with nested objects and array mutators.
  • Supports mapping of states that allows to listen only to changes of certain properties of the state.
  • Can be used to do DOM Manipulations only when they are required.

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 )

const mapCount = ( { count } ) => ( { count } )
const mapActive = ( { active } ) => ( { active } )

observe( State, mapCount )( ( { count } ) => {
	return Counter.innerText = count
} );

( ( interval ) => {

	observe( State, mapActive )( ( { active } ) => {

		if ( active ) {
			interval = setInterval( () => State.count += 1, 1000)
		} else {
			clearInterval( interval )
		}

		StartButton.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
	} )
} )

FAQs

Last updated on 29 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc