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

react-redux-procedures

v1.4.0

Published

A wrapper for react-redux's connect to allow execution of multiple actions in a single procedure

Downloads

14

Readme

react-redux-procedures

A wrapper for react-redux's connect to allow execution of multiple actions in a single procedure

How it works

The procedures are mapped into the three arguments of the basic connect method of react-redux, so that you get props of dispatchable "procedures". Procedures only take arguments and are already stateful and able to dispatch actions. This is really useful if you have actions that are always executed after or next to each other, or if you use actions that depend a lot on your apps State.

API

createProcedure

// procedure file
import { createProcedure } from "react-redux-procedures";

type State = { someStateVal: any }; // to be filled in, if working with flow
type Params = {};

export const procedure = createProcedure(
    dispatch => ({ someStateVal }: State) => ({  }: Params): Promise<any> => {
        // this is where the magic happens.
        // you can dispatch actions, they may be dependent on each other.
        dispatch(someAction());
        dispatch(someOtherAction);
        return dispatch(somePromiseAction()).then(() => 'Yay!'):
    },
    (state: AppState) => ({
        someStateVal: state.val
    })
);

IMPORTANT: Don't forget that the state is already bound when you call the procedure. this means it won't change after dispatching actions internally and may have already changed when you reach a promise result. Procedures should only be dependent on the state at the begin of the procedure.

connectProcedures(
    mapProceduresToProps: { [key]: Procedure },
    mapStateToProps?,
    mapDispatchToProps?,
    mergeProps?,
    options?
)

The optional Arguments are just your standard connect arguments that will get applied by connectProcedures so you don't have to wrap a component in connectProcedures and a standard connect as well.

// component file
import { connectProcedures, type ProcedureDispatcher } from 'react-redux-procedures';
import { someImportedProcedure } from 'somewhere';

type Props = {
    someProc: ProcedureDispatcher<typeof someImportedProcedure>,
};

const Component = ({ someProc }: Props) =>
    <span onClick={() => someProc()}> I dispatch a procedure! </span>;

const mapProceduresToProps = {
    someProc: someImportedProcedure,
};

export default connectProcedures(mapProceduresToProps)(Component);

prepareProcedure(Procedure, Dispatch, State)

// file where you need to call a procedure manually
import { prepareProcedure } from "react-redux-procedures";
import { someImportedProcedure } from "somewhere";

const prepared = prepareProcedure(someImportedProcedure, reduxStore.dispatch, reduxStore.getState());

prepared(/* params */);

Exploration of what Procedures are via their types

Procedure and StatelessProcedure

type Procedure<State, Params, Result, AppState> = {
    (dispatch: Dispatch<any>): State => Params => Result,
    mapStateToProcState: AppState => S,
};

So, a procedure is something that

  1. is dependent of 4 (type) arguments: State, Params, Result and AppState
  2. is mainly a curried function that takes a dispatch function, some procedure-specific state, and params
  3. has a property mapStateToProcState which takes the AppState and returns the procedure-specific state.
  4. has a result of type Promise (this is not in the procedure type above for simplicity, but is expected in the createProcedure function).

There is also a stateless variant, seen below.

type StatelessProcedure<Params, Result> = {
    (dispatch: Dispatch<any>): () => Params => Result,
};

ProcedureDispatcher

As the name suggests, this is just a helper type that takes the Params => Result bit of a given procedure. The type is simplified to not confuse you with flow weirdness, just know that Params and Result are inferred from the given Procedure.

type ProcedureDispatcher<Procedure|StatelessProcedure> = Params => Result;