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

staunch-store

v1.2.0

Published

> loyal, firm, and dependable - solid or substantial in construction

Downloads

4

Readme

staunch

loyal, firm, and dependable - solid or substantial in construction

Staunch is a highly opinionated Redux-style state management system for large-scale apps. Powered by ImmutableJS & RxJS. It's designed to handle both the synchronous state-updates we've all come to love, along with providing best-in-game async support via the mighty RxJS.

We're talking serious power/safety here - combine this with a tiny view library Preact, and you have the tools to build something amazing.

Example bundle 41.3kb (min+gzipped)

  • ImmutableJS
  • Rx-Lite
  • Staunch-store
  • Preact

So Staunch, Rx-Lite, ImmutableJS + Preact all together weigh in at less than 42kb combined - this means it's not really suitable for micro-apps, but if you're already using Immutable or Rx in a project, you can add Staunch on top without issue :)

Examples

Install

Note, RxJS & ImmutableJS are requirements, you'll need to install them separately.

yarn add rx immutable staunch-store

npm i rx immutable staunch-store --save

Change feed for entire store, or any part of the state object.

No joke, just specify which 'path' on the tree you're interested in and you'll only be notified when the data has actually changed.

const { createStore } = require('staunch-store');
const store = createStore({user: {name: 'Shane'}});

// listen to the entire state tree for changes
store.changes()
    .subscribe(state => 
        console.log('updated state', state)
    );
    
// or listen to just the 'user' section of the state
store.changes('user')
    .subscribe(user => 
        console.log('updated state', user)
    );

// dispatch an action
store.dispatch({type: 'USER_AUTH'});

Complete safety and removal of defensive coding patterns

The true power of immutable data can only be realised when you go 'all-in'. Staunch will convert your initial state, or any that's added on the fly into ImmutableJS Maps or Lists automatically. This allows you the freedom to just code your state updates without having to worry about mutating anything. Say good riddance to Object.assign once and for all!

import { createStore } from 'staunch-store';
import { fromJS } from 'immutable';

// initial user state
const intitial = {user: {name: 'shane'}};

// create store + add a reducer that will be
// bound to the 'user' path
const store = createStore(intitial, {
    user: function userReducer (user, action) {
          switch(action.type) {
              case 'USER_AUTH':
                  // look ma, no Object.assign in sight!
                  return user.set('auth', action.payload); 
              default:
                  return user;
          }
    }
});

Async built in

Nothing solves async like Rx does. Taking inspiration from redux-observable and building upon it, Staunch has side effects nailed. Effects are just functions that take a stream of actions and return more actions.

import { createStore } from 'staunch-store';

const initial = {user: {auth: false}};


/**
 * User reducer
 */
function userReducer (user, action) {
    switch(action.type) {
        case 'USER_FETCH':
            return user.set('loading', true);
        case 'USER_FETCH_SUCCESS':
            return user.set('data', action.payload);
        case 'USER_FETCH_ERROR':
            return user
                .set('data', {})
                .set('message', action.payload)
        default:
            return user;
    }
}

/**
 * action$ here is a stream of all actions that occur
 * When USER_FETCH is fired, it will trigger an ajax request 
 * mapping the result (or error) to other actions that
 * affect the state.
 */
function userEffect(action$) {
    return action$.ofType('USER_FETCH')
        .flatMapLatest(({action, state}) => {
            return fetch(action.payload.url).then(x => x.json())
        })
        .map(result => {
            return {
                type: 'USER_FETCH_SUCCESS',
                payload: result
            }
        })
        .catch(err => {
            return Rx.Observable.of({
                type: 'USER_FETCH_ERROR',
                payload: err.message
            });
        });
}

// store with 
// 1. initial state 
// 2. reducer bound to 'user' path
// 3. the userEffect fn
const store = createStore(initial, {
    user: userReducer
}, userEffect);


/**
 * Now fire the USER_AUTH action
 */
store.dispatch({type: 'USER_AUTH'});