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

faststate

v0.4.4-alpha

Published

FastState is a small and fast reactive programming library for JavaScript apps

Downloads

4

Readme

FastState

FastState is a small and fast reactive programming library for JavaScript apps. It maintains an up-to-date state, so when one bit of state changes, it automatically calculates and updates downstream state.

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

Features:

  • miniscule (~200 lines of code)
  • no dependencies
  • single atomic state object
  • modular: all functionality added in modules to aid separation of concerns and portability
  • computed functions

FastState is deliberately minimal, but very powerful, allowing you to use it as the core of a reactive JavaScript app. Computed functions are the beating heart of the library. With them, you can:

  • create computed properties that are added to the state
  • call actions as a result of state changes (subscriptions)
  • do anything else you want as a result of state changes, whether that be re-rendering or firing-off async actions.

Installation

$ npm install faststate --save

Minimal example

import createApp from 'faststate';

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

const app = createApp(config);

app.actions.counter.up(1);
console.log(app.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 app = createApp(config);

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

// outputs:
// 1
// 2

Computed Functions

Add computed functions by specifying dependencies up front. Computed functions are guaranteed to be up-to-date, so you can have other computed properties as dependencies too. FastState will keep make sure that the computed functions are executed in the correct order.

const config = {
  counter: {
    computed: {
      total: {
        deps: ['count', 'initial'], // state dependencies local to this module
        rootDeps: ['user.username'],
        // available params are: actions, state, rootActions, rootState
        getter: ({ rootState, state }) => `${rootState.user.username}: ${state.count + state.initial}`,
    }
    state: {
      count: 0,
      initial: 5,
    }
  }
};

const app = createApp(config);
app.actions.counter.up(1)

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

You don't have to return a value from a computed function, you can call another action, or do anything else async. So computed functions are also subscriptions:

const config = {
  counter: {
    computed: {
      total: {
        deps: ['count'], // state dependencies local to this module
        // available params are: actions, state, rootActions, rootState
        getter: ({ actions, state }) => {
          console.log(state.count);
          actions.myOtherAction(state.count);
        },
    }
    state: {
      count: 0,
      initial: 5,
    }
  }
};

const app = createApp(config);
app.actions.counter.up(1); // console logs: 1