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

picostore

v0.4.3-alpha

Published

A tiny and fast state management container

Downloads

14

Readme

PicoStore

PicoStore is a small and fast state management container for JavaScript apps.

Note: this library is currently in alpha and therefore the API is subject to breaking changes.

Features:

  • tiny (<1KB minified)
  • no dependencies
  • single atomic state object
  • modular: all functionality added in modules to aid separation of concerns and portability
  • computed properties
  • subscribe to changes in specific properties in the state object

Installation

$ npm install picostore --save

Minimal example

import createStore from 'picostore';

const config = {
  counter: {
    actions: {
      up: value => state => ({ count: state.count + value }),
      down: value => state => ({ count: state.count - value })
    },
    state: {
      count: 0
    }
  }
};

const store = createStore(config);

store.actions.counter.up(1);
console.log(store.state.count); // 1

Async

Actions don't have to return a segment of the state, they can be used to fire asynchronous actions, or do anything else you want. If you later want to update the state synchronously, you must call another action:

const config = {
  counter: {
    actions: {
      get: () => ({ actions }) => new Promise((resolve) => {
        actions.set(1);
        resolve();
      }),

      set: value => state => ({ count: value });
    },
    state: {
      count: 0;
    }
  }
}

const store = createStore(config);

store.actions.getCount();
console.log(1);

// outputs:
// 1
// 2

Computed Properties

Add computed properties by specifying dependencies up front. Computed functions are memoized so they are exceedingly fast.

const config = {
  counter: {
    actions: {
      up: value => state => ({ count: state.count + value }),
      down: value => state => ({ count: state.count - value }),
    },
    computed: {
      total: ['count', 'initial', (state) => state.count + state.initial],
    }
    state: {
      count: 0,
      initial: 5,
    }
  }
};

const store = createStore(config);
store.actions.counter.up(1)

console.log(store.state.counter.total); // 6

Subscriptions

Subscribe to nested properties by specifying their paths in dot notation. You can also subscribe to computed properties.

let count, total;

const config = {
  counter: {
    actions: {
      up: value => state => ({ count: state.count + value }),
    },
    computed: {
      total: ['count', 'initial', (state) => state.count + state.initial],
    }
    state: {
      count: 0,
      initial: 5,
    },
    subscriptions: {
      count: (newVal) => { count = newVal; }
      total: (newVal) => { total = newVal; } // subscribe to computed properties too
    }
  }
};

const store = createStore(config);

store.actions.counter.up(1);

console.log(count, total); // 1, 6