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-promise-reducer

v1.0.1

Published

Simple reducer for `redux-promise-middleware`

Downloads

7

Readme

redux-promise-reducer

Provides an action creator and reducer that easily integrates with redux-promise-middleware.

Installation

npm install redux-promise-reducer --save

Usage

1. Setup the redux-promise-middleware

Setup your store with the middleware to make sure it handles the actions you'll create for resolving promises.

2. Create the reducer that will handle all the actions created by the middleware.

You can either use the default reducing functions for the PENDING, FULFILLED and REJECTED actions (see below for details):

import { promiseReducer } from 'redux-promise-reducer';
...
const myreducer = promiseReducer();

or you can customize how your reducer calculated the next state for each update in the promise state:

const myreducer = promiseReducer({
    // here we choose not to change the default reducer for PENDING
    fulfilledReducer: () => 'ok', 
    rejectedReducer: () => 'awww man!'
});

3. Make sure your store uses your new reducer

Here we use redux.combineReducers to make sure all your promises reside under a specific property (data in this case) in your state:

import { combineReducers } from 'redux'
const rootReducer = combineReducers({
  data: promiseReducer,
  ... // other reducers heres
})
// create the store using `rootReducer`

4. Dispatch actions using the supplied action creator

The action creator createPromiseAction creates actions that will be handled by redux-promise-middleware. The middleware will immediately invoke a PENDING action; it will also make sure that the promise embedded in the action is either FULFILLED or REJECTED, and will invoke new actions when that happens. These new actions will then be reduced by the reducer we created earlier.

Every action created by this creator get prefixed with RPR. Also, the reducer only handles actions that have this prefix (along with some other requirements, like terminating with either PENDING|FULFILLED|REJECTED). This currently hard-coded.

Some examples:

  • create an action with type RPR_RESOLVE_PROPNAME (the action name is being inferred from the property name - note it adds RESOLVE_ to it). Data will be stored in the state under a property named propname
import { createPromiseAction } from 'redux-promise-reducer';
const promise = Promise.resolve('ok');
const action = createPromiseAction({parameter: 'propname', promise});
  • you can specify the action name instead. This creates an action with type RPR_JUST_DO_IT
const promise = Promise.resolve('ok');
const action = createPromiseAction({parameter: 'propname', actionName: 'JUST_DO_IT', promise});

Also note the action names are always in caps.

The default reducing functions

You create your reducer by invoking promiseReducer. This functions accepts an object, which holds functions that define how your state is modified in each state of the promise:

promiseReducer({
    // function that will determine the next state when a PENDING action is dispatched by the middleware
    pendingReducer: (state, action) => ...  
    fulfilledReducer: (state, action) => ...  
    rejectedReducer: (state, action) => ...  
})

You can however omit the whole object, or at least some of its parameters. In that case the default reducing functions will be used, which will produce the following states (note that propname is the name of the property that was specified when creating the action with createPromiseAction:

For the PENDING state:

propname: {
    pending: true,
    error: false,
    ready: false
}

For the FULFILLED state:

propname: {
    pending: false,
    error: false,
    ready: true,
    data: ... // result of the promise
}

For the REJECTED state:

propname: {
    pending: true,
    error: true,
    ready: false
}