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-index-property

v1.0.4

Published

The easy way to create and maintain maps(key indexed properties) in redux state

Downloads

12

Readme

redux-index-property

The easy way to create and maintain maps(key indexed properties) in redux state

Install and Run

npm install

npm start

In this application you have the redux store that suddenly got more complicated, so you come up with the decent API. API usage examples are in index.ts. I did my best to make the code readable.

Then it's a good time to meet some code:

Reducer:

switch (action.type) {
case userActionTypes.USER_DASHBOARD_ACTION: {
            const userDashboardsIdAction = action as IPayloadIdAction<IPayloadAction<number | string>>;

            const getTargetDashboard =
            (
                statePart: IUser,
            ) => statePart.dashboards[userDashboardsIdAction.id];

            const setTargetDashboard =
            (
                statePart: IUser,
                newValue,
            ) => {
                statePart.dashboards[userDashboardsIdAction.id] = newValue;
            };

            const acceptor = new PropertyAcceptor(getTargetDashboard, setTargetDashboard);

            const idIndexedProperty = new KeyIndexedProperty(acceptor, dashboardReducer, state);
            idIndexedProperty.dispatch(userDashboardsIdAction.payload);
            const newState = idIndexedProperty.getState();

            return {
                ...newState,
            };

        }
    }

Here we have our idIndexedProperty calculating the next state out of current state and dispatched action. The two things to mention here are:

  1. Our reducer (dashboardReducer in our case) is written like it manages the single state instance
  2. We separate our access logic (search for the target property) from our calculating logic with PropertyAcceptor .

Initial intent.

So, why do we need key indexed properties in our state?

  1. It's fast (you have constant access time).
  2. Sometimes you need some of you properties to relocate and have multiple entries in your state. Like if you had settings for everybody and now for each user. You do not want to rewrite all your model logic, you want to reuse your existing reducer.

Architecture decisions

Model-level logic (you do not export part of your model (reducer) outside of the model).

By saying model I mean the place you store data in your application and API around:

Store - the object, return result of the createStore function, it provides the State of our application to the rest of our application and takes care of state of our application can not be changes any way except through the Actions API. And most importantly it makes synchronous changes and it makes your state much more predictable and reliable. Reducers - pure functions that calculate the new State out of the previous State and the dispatched Action) ActionCreators - it is the Model Change API we’d love the rest of our application know nothing about how things are happened inside)

There are also: Actions – the objects that has type: string property The State – it is the state of our application. Our application is just the function that returns jsx with TheState argument passed.
We never work with them directly though. We most likely have connect and we definitely use ActionCreators. First to consume the Model API and the second to provide the Model Change API.

So we could’ve thought of grabbing the reducer (since it is a pure function and it returns the new State) into a thunk action or a saga, calculate the new state there and then write into the state.

But, you can not just grab the hidden API out of your mode, then import it somewhere and then say you have separation of concerns. So we keep our State changing logic inside the model and provide API as nice as we can.