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

toystore

v1.5.4

Published

Lightweight central store of state with the ability to watch for and react to specific property changes

Downloads

21

Readme

Toystore.js

Lightweight central store of state with the ability to watch for and react to specific property changes

Think "toy" as in small. This thing is ready to rock at just a hair over 2kb minified and gzipped.

Installation

npm install toystore --save

Usage

Create a new store instance with the initial store values:

const toystore = require('toystore');

let store = toystore.create({
  foo: 'bar',
  user: {
    email: '[email protected]',
    id: 1,
  }
});

module.exports = store;

Get store values anywhere in your app, even nested keys:

const store = require('./mystore');

function renderUserEmail() {
  return store.get('user.email');
}

Watch for changes on specific keys and react to them:

const store = require('./mystore');

store.watch(['user.email'], updateUserEmail);

Control the order watchers fire using priority weightings. The default priority is 0. Negative numbers can be used to push watchers to the end.

const store = require('./mystore');

store.watch(['user'], secondTask);
store.watch(['user'], firstTask, { priority: 10 });
store.watch(['user'], thirdTask, { priority: -1 });

Watchers can also be executed asynchronously after a short timeout instead of invoked immediately:

const store = require('./mystore');

// Executed async with setTimeout instead of immediately called
store.watch(['user'], updateUserInfo, { async: true });

// Executed async after provided milliseconds
store.watch(['user'], updateUserInfoInOtherPlace, { async: 500 });

Update store values from anywhere in your app:

const store = require('./mystore');

function fetchUser() {
  return fetch('/myapi/users/1')
    .then(json => {
      store.set('user', {
        email: json.data.email,
        id: json.id,
      });
    });
}

Usage With React

If you use React and want to bind your React components to automatically re-render on store key changes, use the toystore-react package.

API

After you create a store instance using toystore.create(), the resulting store object has the following methods:

get(path)

Get a single store value from the provided path. This can use periods for nesting, i.e. user.email.

store.get('user.email'); // [email protected]

getAll(paths = null)

Returns an object with key/value pairs of all the keys requested. The provided paths argument must be an array.

If you do not provide any arguments, the entire store object will be returned.

store.getAll(['is_cool', 'is_quality']); // { is_cool: true, is_quality: true }

set(path, value)

Set a single store value to the provided path. This can use periods for nesting, i.e. user.email.

store.set('user.email', '[email protected]');

setSilent(path, value)

Same as set, but will not notify watchers that the key has been updated.

store.setSilent('user.email', '[email protected]');

setAll(object)

Takes an object with key/value pairs of all the keys and values to be set. Will only notify watchers of updates once - after all keys have been set.

store.setAll({ is_cool: true, is_quality: true });

reset(object)

Reset the whole store to the provided object. Commonly used for testing and/or resetting the store to the default state.

NOTE: This will trigger all watchers because all keys will change, so if you also want to remove all the watchers before using reset(), call unwatchAll().

store.reset({ is_cool: true, is_quality: true, user: false });

watch(paths, callback, options = {})

Watch provided paths for changes, and execute callback when those values change.

This method is very useful to seamlessly react to data changes in the store without having to create events or other mechanisms to update views or content mannually after updating store values.

The callback function will receive an object with key/value pairs of the new store values after the triggered change.

store.watch(['user'], updateUserInfo);
store.watch(['mode'], changeMode);
store.watch(['router.url'], (newValues) => navigateToPage(newValues.router.url));

// With priorities (higher gets executed first)
store.watch(['cart'], updateShoppingCart, { priority: 1 });
store.watch(['cart'], updateShoppingCartCount, { priority: 10 });

watchAll(callback)

Similar to watch, but provided callback will execute whenever any key in the whole store is changed. Will only be fired once when setAll is used with multiple keys.

store.watchAll(renderApp); // Will execute when *any* key changes

watchOnce(paths, callback, options = {})

Similar to watch, but provided callback will only execute a single time, and then will unwatch itself automatically. Useful when chaining watchers, or when the watcher is created inside another function that is not always applied.

store.watchOnce(['visit'], showUserPopupAd); // Will only execute ONCE

unwatch(callback)

Unregisters only the provided callback that has been added with watcher.

store.unwatch(updateUserInfo); // Remove updateUserInfo only

unwatchAll()

Removes all registered watchers.

store.unwatchAll(); // Removes ALL watchers