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

redux-shrub

v2.0.4

Published

Branch - Leaf based Redux framework that creates the reducers, actions and selectors on the go.

Downloads

3

Readme

Redux Shrub Logo

Branch - Leaf based Redux framework that creates the reducers, actions and selectors on the go.

The idea is based on incorporating a OOP approach to redux store design and action declaring.

Table of Contents

Concepts

How does this work with Redux

It actually doesn't require Redux to work. It produces a reducer that can be used to manipulate the state that redux keeps track of. And if you call the reducer function with an initial state of undefined it give you the state! In addition to that, it creates the actions and selectors from the information passed in. So in the end, all you need to define are classes that interact with parts of the tree(leaves, branches) using their methods(each method becomes an action).

How is a shrub different than a tree?

Shrub is subset of trees as a data structure. The concept of a shrub is that the data is only on the leaves on of the tree. By keeping the data only on the leaves, it becomes really simple to trace bugs and reason about the structure of the data stored globally.

Reasoning behind coming up with redux-shrub

I personally felt challenged with the lack of structure that can could be given with a redux framework and went ahead and created one. This framework provides a structure in which the user doesn't have to worry about creating 3 different functions. Reducers, actions and selectors are created as you declare new leaves in your state tree. It has similar API to redux-tree but does more than that.

Main Components

There are 3 types of building blocks to a shrub.

leaf

This is where you keep your data. These will be at the end of a path on the tree and will have actions to modify them.

branch

This is how you organize your data, you create different branches to split your information/leaves.

polyBranch

This is a bit more complex but is necessary in a lot cases. Let's say you have multiple entries for a data object in your redux store.

{
  'article-1': {...some data}
  'article-2': {...same data here, maybe the values are different but same structure}
  'article-3': {...same data here, maybe the values are different but same structure}
  ...
}

In this case, you can't manually set your tree to have a certain number of branches. And if you want to have actions for the data inside of these keys, you don't want to have duplicate actions. To solve this, you can define a way to produce keys and pass that in to a polyBranch() with whatever other branches / leaves you want in it and it will handle the rest. A bit magical but trust me it works :)

Installation

npm i redux-shrub

Examples

import { compose, leaf } from 'redux-shrub';

class NameReducer {
  // this is what is called to
  // generate your initial state
  newState (){
    return 'initial name'
  }
  // this method will become an action
  update = state => payload => payload.newName
}

const todos = leaf('name', NameReducer);

const shrub = compose([ todos ]);

// Fake what redux does to generate the initial state
const reducer =  shrub.reducer(undefined, '@@init');
const actions = shrub.actions;
const selectors = shrub.selectors;

console.log(reducer, actions, selectors)
// { name: 'initial name' } { NAME_UPDATE: [Function] } { name: [Function] }

Another example

import { compose, leaf, branch } from 'redux-shrub';

class NameReducer {
  // this is what is called to
  // generate your initial state
  newState (){
    return 'initial name'
  }
  // this method will become an action
  update = state => payload => payload.newName
}

class DataReducer {

}

const todos = leaf('name', NameReducer);
const data = branch('data', DataReducer, [ todos ]);

const shrub = compose([ data ]);

// Fake what redux does to generate the initial state
const reducer =  shrub.reducer(undefined, '@@init');
const actions = shrub.actions;
const selectors = shrub.selectors;

console.log(reducer, actions, selectors)
// { data: { name: 'initial name' } } { NAME_UPDATE: [Function] } { name: [Function], data: [Function] }