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

valstore

v2.0.0

Published

Featherweight key-based value store with zero dependencies

Downloads

17

Readme

valstore

NPM Build
Status Minzipped Size

Featherweight key-based central store of state with zero dependencies.

Spiritual successor to toystore. valstore is lighter weight, has no dependencies, and has a smaller, leaner API.

Val = Value

Store = Central store of application state

Easy to Learn and Use

Other state management systems can be difficult or cumbersome to use, or require a lot of repetitive boilerplate code that gets in the way.

valstore is based around get and set calls with nested keys for object structures, like store.get('foo.bar.baz').

Install

Install valstore with NPM:

npm install valstore --save

Example

Import if using ES6+ or Transpiling:

import valstore from 'valstore';

Require if using ES5 or Node.js:

const valstore = require('valstore');

Create a new store instance with its initial state using valstore.create():

const store = valstore.create({
  foo: {
    bar: 'baz'
  }
});

API

create(initialState)

Create a new store with its initial state. This should be an object.

const store = valstore.create({
  foo: {
    bar: 'baz'
  }
});

get(key: string | function)

Get a value from the store by string key. Accepts simple keys like user and nested keys like foo.bar.baz.

const user = store.get('user');
const value = store.get('foo.bar.baz');

Selector Functions

If you prefer using selector functions for retrieving values from the store, you can use those with get also:

const getFooBarBaz = state => state.foo.bar.baz;
const value = store.get(getFooBarBaz);

set(key: string, value: any)

Set a value to the store with string key. Just like get, this accepts simple keys like user and nested keys like foo.bar.baz.

store.set('user', { id: 2, name: 'Testy McTesterpants', email: '[email protected]' });
store.set('foo.bar.baz', 'qux');

batch(name: string, trx: function)

Batches set multiple values into the store in a single transaction before broadcasting any updates. For example if you call store.set twice (or even hundreds of times!) in a batch, all the affected keys will be collected, and your matching subscribers will only fire once the batch is complete instead of once instead of once per set call.

Batches are a good performant way to make many set calls in sequence in situations where your subscribers can wait to fire at the end.

store.batch('USER_UPDATE', function () {
  store.set('user', { id: 2, name: 'Testy McTesterpants', email: '[email protected]' });
  store.set('foo.bar.baz', 'qux');
});

Note that any async set calls made will not be covered in the batch (for example a set call after a fetch call that is wrapped in a separate batch). Nested batches are not recommended as they are not accounted for by valstore.

subscribe(callback: function, [key: string | string[]])

Subscribe a function to changes in the store. Just like get, this accepts simple keys like user and nested keys like foo.bar.baz.

Subscribe callbacks receive the current state as the first argument.

Returns an id of the subscription that can be used with unsubscribe(id).

function updateCart(state) {
  updateCartItemTotals(state.shopping.cart);
}

Subscribe to ALL/ANY changes

If you want to ALL/ANY changes in the store, just pass a function as an argument with no keys:

store.subscribe(updateCart);

Subscribe to specific key

If you want to subscribe to a specific key, specify the key as the first argument:

store.subscribe(updateCart, 'shopping.cart');

Subscribe to multiple keys

If you want to subscribe to multiple keys, specify the key as an array in the first argument:

store.subscribe(updateCart, ['shopping.cart', 'user']);

unsubscribe(id|callback)

Unsubscribe by id or callback. Unsubscribes all registered callbacks when called with no arguments.

store.unsubscribe();

With key only (will unsubscribe ALL listeners on this key):

store.unsubscribe('shopping.cart');

With key and callback:

store.unsubscribe(updateCart, 'shopping.cart');

With subscriber id (returned from subscribe call):

var id = store.subscribe(updateCart, 'shopping.cart');

store.unsubscribe(id);

trigger(keys)

Used internally after calls to set or after a batch in batch. Triggers an update on the provided key or array of keys. This will fire any subscriber functions that are listening on those keys.

Note: You should never have to call this manually. This is only documented here for completeness.

store.trigger('foo.bar.baz');
store.trigger(['shopping.cart', 'user']);