1.1.1 • Published 7 years ago

state-me v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

StateMe is a lightweight, easy to use, powerful state management library for JavaScript applications.

Main Features

  • Immutable state implemented by default.
  • No fancy theories or patters.
  • No tens of files required to set-up.
  • Just install and you are ready.

Installation

Using npm:

$ npm install state-me --save

Using yarn:

$ yarn add state-me

Usage

As ES6 module:

// Load the module.
import State from 'state-me'

// Get a copy of the current state.
// Note: The entire state will be cloned.
//       Should not be used in production, as cloning may be slow.
State.state() // {}

// Get a copy of all listeners.
// Note: The entire list of listeners will be cloned.
//       Should not be used in production, as cloning may be slow.
State.listeners() // {}

// Set the initial state.
// Note: Passed initial state is cloned and frozen to prevent mutations.
State.init({ foo: 'bar' })

// Get a value from the state.
// Note: All returned values are frozen to prevent mutations.
//       If you want to get a mutable copy use `store.clone()` method.
State.get('foo') // bar
State.get('bar') // undefined
State.get('bar', 'baz') // baz

// Get a mutable copy of a value from the state.
// Note: Cloning may be slow.
State.clone('foo') // bar
State.clone('bar') // undefined
State.clone('bar', 'baz') // baz

// Add a new value to the state.
// Note: All new values are cloned and frozen to prevent mutations.
State.set('foo', 'baz')
State.set('foo.bar', 'baz')

// Listen to state value changes.
// Note: The callback will be executed every time when the `foo` value changes.
// Note: `newValue` and `oldValue` are frozen to prevent mutations.
State.on('foo', (newValue, oldValue) => {
  console.log(newValue, oldValue)
})

// Listen for changes on all values of the state.
// Note: The callback will be executed every time a value of the state changes.
// Note: `newValue` and `oldValue` are frozen to prevent mutations.
State.on((key, newValue, oldValue) => {
  console.log(key, newValue, oldValue)
})

// Remove a listener.
let index = State.on('foo', (newValue, oldValue) => {})
State.off(index)

// Remove all listeners on a specific key.
State.off('foo')

// Manually trigger an event on a specific key.
// Note: `newValue` and `oldValue` are mutable.
// Note: Does not save `newValue` or `oldValue` to the state.
State.emit('foo', newValue, oldValue)

In NodeJS:

// Load the module.
const state = require('state-me')

// The API is the same as for the ES6 module.

Note on key names

To make StateMe work as fast as it can, it uses only top-level keys to set / get / update the state.

For example, if your the following state:

{
  "a": {
    "b": {
      "c": "foobar"
    }
  }
}

You can not directly access the foobar value using get method:

State.get('a.b.c') // undefined

Instead, you can use:

State.get('a').b.c // foobar

And if you set a value with dot notation:

State.set('a.b.c', 'foobar')

It will give the following result:

{
  "a.b.c": "foobar"
}

Which can be accessed directly using get method:

State.get('a.b.c') // foobar

Browser support

StateMe works in all modern browsers, and in older browsers with polyfills.

Testing

$ npm test

License

The MIT License (MIT)

Copyright (c) 2017 Ion Suman sumanion122@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago