npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

state-me

v1.1.1

Published

Lightweight state management library.

Downloads

11

Readme

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 [email protected]

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.