• Stars
    star
    1,333
  • Rank 34,487 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sensible promise handling and middleware for redux

Redux Pack

Sensible promise handling and middleware for redux

redux-pack is a library that introduces promise-based middleware that allows async actions based on the lifecycle of a promise to be declarative.

Async actions in redux are often done using redux-thunk or other middlewares. The problem with this approach is that it makes it too easy to use dispatch sequentially, and dispatch multiple "actions" as the result of the same interaction/event, where they probably should have just been a single action dispatch.

This can be problematic because we are treating several dispatches as all part of a single transaction, but in reality, each dispatch causes a separate rerender of the entire component tree, where we not only pay a huge performance penalty, but also risk the redux store being in an inconsistent state.

redux-pack helps prevent us from making these mistakes, as it doesn't give us the power of a dispatch function, but allows us to do all of the things we were doing before.

To give you some more context into the changes, here are some examples/information about the old way and new way of doing things below:

Ready to use it? Jump straight to the How-To and API doc

Data Fetching with redux-thunk (old way)

Before this change, you would create individual action constants for each lifecycle of the promise, and use redux-thunk to dispatch before the promise, and when it resolves/rejects.

// types.js
export const LOAD_FOO_STARTED = 'LOAD_FOO_STARTED';
export const LOAD_FOO_SUCCESS = 'LOAD_FOO_SUCCESS';
export const LOAD_FOO_FAILED = 'LOAD_FOO_FAILED';
// actions.js
export function loadFoo(id) {
  return dispatch => {
    dispatch({ type: LOAD_FOO_STARTED, payload: id });
    return Api.getFoo(id).then(foo => {
      dispatch({ type: LOAD_FOO_SUCCESS, payload: foo });
    }).catch(error => {
      dispatch({ type: LOAD_FOO_FAILED, error: true, payload: error });
    });
  };
}

In the reducer, you would handle each action individually in your reducer:

// reducer.js
export function fooReducer(state = initialState, action) {
  const { type, payload } = action;
  switch (type) {
    case LOAD_FOO_STARTED:
      return {
        ...state,
        isLoading: true,
        fooError: null
      };
    case LOAD_FOO_SUCCESS:
      return {
        ...state,
        isLoading: false,
        foo: payload
      };
    case LOAD_FOO_FAILED:
      return {
        ...state,
        isLoading: false,
        fooError: payload
      };
    default:
      return state;
  }
}

Note: The example uses { ...state } syntax that is called Object rest spread properties. If you'd prefer the API of Immutable.js, you could write code like the following:

switch (type) {
  case LOAD_FOO_STARTED:
    return state
      .set('isLoading', true)
      .set('fooError', null);
  case LOAD_FOO_SUCCESS:
    // ...
}

Data fetching with redux-pack (new way)

With redux-pack, we only need to define a single action constant for the entire promise lifecycle, and then return the promise directly with a promise namespace specified:

// types.js
export const LOAD_FOO = 'LOAD_FOO';
// actions.js
export function loadFoo(id) {
  return {
    type: LOAD_FOO,
    promise: Api.getFoo(id),
  };
}

In the reducer, you handle the action with redux-pack's handle function, where you can specify several smaller "reducer" functions for each lifecycle. finish is called for both resolving/rejecting, start is called at the beginning, success is called on resolve, failure is called on reject, and always is called for all of them.

// reducer.js
import { handle } from 'redux-pack';

export function fooReducer(state = initialState, action) {
  const { type, payload } = action;
  switch (type) {
    case LOAD_FOO:
      return handle(state, action, {
        start: prevState => ({
          ...prevState,
          isLoading: true,
          fooError: null
        }),
        finish: prevState => ({ ...prevState, isLoading: false }),
        failure: prevState => ({ ...prevState, fooError: payload }),
        success: prevState => ({ ...prevState, foo: payload }),
      });
    default:
      return state;
  }
}

Logging (before/after)

Often times we want to log whether an action succeeded or not etc. We are able to handle this now using the onSuccess or onFailure meta options:

Before:

// actions.js
export function loadFoo(id) {
  return dispatch => {
    dispatch(loadFooStart());
    Api.getFoo(id).then(response => {
      dispatch(loadFooSucceeded(response);
      logSuccess(response);
    }).catch(error => dispatch(loadFooFailed(error)));
  };
}

After:

// actions.js
export function loadFoo(id) {
  return {
    type: LOAD_FOO,
    promise: Api.getFoo(id),
    meta: {
      onSuccess: (response) => logSuccess(response)
    },
  };
}

How to

Install

The first step is to add redux-pack in your project

npm install -S redux-pack

# or

yarn add redux-pack

Setup the middleware

The redux-pack middleware is the heart of redux-pack. As the following example shows, it installs like most middlewares:

import { createStore, applyMiddleware } from 'redux'
import { middleware as reduxPackMiddleware } from 'redux-pack'
import thunk from 'redux-thunk'
import createLogger from 'redux-logger'
import rootReducer from './reducer'

const logger = createLogger()
const store = createStore(
  rootReducer,
  applyMiddleware(thunk, reduxPackMiddleware, logger)
)

Note that it should probably be one of the first middleware to run, here it would run just after thunk and before logger.

Using the handle() helper

Let's start with the function signature: handle(state, action, handlers) → newState

As you can see, it takes 3 arguments:

  1. state: the current state in your reducer
  2. action: the action that should be handled
  3. handlers: a object mapping the promise lifecycle steps to reducer functions
  • the steps names are: start, finish, failure, success and always
  • every handler function should be of the form: state => state

Here is a minimalist example:

import { handle } from 'redux-pack';
import { getFoo } from '../api/foo';

const LOAD_FOO = 'LOAD_FOO';
const initialState = {
  isLoading: false,
  error: null,
  foo: null,
};

export function fooReducer(state = initialState, action) {
  const { type, payload } = action;
  switch (type) {
    case LOAD_FOO:
      return handle(state, action, {
        start: prevState => ({ ...prevState, isLoading: true, error: null, foo: null }),
        finish: prevState => ({ ...prevState, isLoading: false }),
        failure: prevState => ({ ...prevState, error: payload }),
        success: prevState => ({ ...prevState, foo: payload }),
        always: prevState => prevState, // unnecessary, for the sake of example
      });
    default:
      return state;
  }
}

export function loadFoo() {
  return {
    type: LOAD_FOO,
    promise: getFoo(),
  }
}

Note: The example uses { ...state } syntax that is called Object rest spread properties.

Adding side-effects with event hooks

You might want to add side effects (like sending analytics events or navigate to different views) based on promise results.

redux-pack lets you do that through event hooks functions. These are functions attached to the meta attribute of the original action. They are called with two parameters:

  1. the matching step payload (varies based on the step, details below)
  2. the getState function

Here are the available hooks and their associated payload:

  • onStart, called with the initial action payload value
  • onFinish, called with true if the promise resolved, false otherwise
  • onSuccess, called with the promise resolution value
  • onFailure, called with the promise error

Here is an example usage to send analytics event when the user doesFoo:

import { sendAnalytics } from '../analytics';
import { doFoo } from '../api/foo';

export function userDoesFoo() {
  return {
    type: DO_FOO,
    promise: doFoo(),
    meta: {
      onSuccess: (result, getState) => {
        const userId = getState().currentUser.id;
        const fooId = result.id;
        sendAnalytics('USER_DID_FOO', {
          userId,
          fooId,
        });
      }
    }
  }
}

Testing

At the moment, testing reducers and action creators with redux-pack does require understanding a little bit about its implementation. The handle method uses a special KEY.LIFECYCLE property on the meta object on the action that denotes the lifecycle of the promise being handled.

Right now it is suggested to make a simple helper method to make testing easier. Simple test code might look something like this:

import { LIFECYCLE, KEY } from 'redux-pack';
import FooReducer from '../path/to/FooReducer';

// this utility method will make an action that redux pack understands
function makePackAction(lifecycle, { type, payload, meta={} }) {
  return {
    type,
    payload,
    meta: {
      ...meta,
      [KEY.LIFECYCLE]: lifecycle,
    },
  }
}

// your test code would then look something like...
const initialState = { ... };
const expectedEndState = { ... };
const action = makePackAction(LIFECYCLE.START, { type: 'FOO', payload: { ... } });
const endState = FooReducer(initialState, action);
assertDeepEqual(endState, expectedEndState);

More Repositories

1

react-primitives

Primitive React Interfaces Across Targets
JavaScript
3,085
star
2

react-native-parallax-view

Parallax view for vertical scrollview/listviews with a header image and header content
JavaScript
1,284
star
3

redux-injectable-store

Redux store with injectable reducers for use with bundle splitting, large apps, and SPAs.
JavaScript
230
star
4

react-native-segmented-view

Segmented View for React Native (with animation)
JavaScript
187
star
5

redux-entity

WIP. An abstraction layer around handling normalized entity storage and data fetching with redux
JavaScript
187
star
6

compose-dogfooding

Code written during the Compose Dogfooding streams
Kotlin
183
star
7

react-native-pan-controller

A react native component to help with common use cases for scrolling/panning/etc
JavaScript
181
star
8

recoil

Swift and Kotlin ports of React (Prototype)
Swift
180
star
9

dolla

A light-weight jQuery clone just cuz
JavaScript
139
star
10

enzyme-example-mocha

Example project with React + Enzyme + Mocha
JavaScript
132
star
11

react-native-safe-module

A safe way to consume React Native NativeModules
JavaScript
118
star
12

enzyme-example-react-native

Example project with React Native + Enzyme
Objective-C
87
star
13

native-navigation-boilerplate

Objective-C
82
star
14

enzyme-example-karma-webpack

Example project with React + Enzyme + Karma + Webpack
JavaScript
82
star
15

react_native_animation_examples

JavaScript
80
star
16

react-native-in-depth

Courseware and material for a class on React Native
Objective-C
74
star
17

react-native-future

Some thoughts on future directions and APIs for React Native
67
star
18

enzyme-example-jest

Example project with React + Enzyme + Jest
JavaScript
45
star
19

BQL

BQL: A Better Query Language, SQL Superset
C#
43
star
20

react-image-magnifier

A react component that accepts a high-res source image and produces a magnifier window on mouse hover over the part of the image the cursor is over
JavaScript
39
star
21

LukeMapper

Lucene.Net Document to Object ORM Mapper inspired by Dapper
C#
36
star
22

react-validators

Enhanced React Shape PropType Validators
JavaScript
36
star
23

knockout-react

A wrapper / bridge for using React.js with Knockout and Knockout with React.js
JavaScript
25
star
24

gestio

Declarative DOM-Based Gesture Responder System
JavaScript
21
star
25

style-equal

An efficient equality algorithm for React Native inline styles
JavaScript
20
star
26

astrobin-compose

Kotlin
20
star
27

knockout-components

A components library for Knockout.js (3.0 and above)
JavaScript
18
star
28

knockout-paged

Knockout Extension/Plugin for common paged-data scenarios
CSS
16
star
29

knuckles.js

A web application framework built on top of Knockout.js
JavaScript
13
star
30

react-native-animated-navigator

React Native's Navigator implemented with the Animated API
JavaScript
9
star
31

thinking-in-react

Courseware and material for a class on React
JavaScript
9
star
32

understanding-javascript

Courseware and material for a class on JavaScript
JavaScript
9
star
33

knockout-oftype

Some Extensions and Helpers for handling the Constructor pattern in Knockout
JavaScript
8
star
34

react-primitives-art

Cross-platform interface for react ART library
JavaScript
8
star
35

lucene-fluent-query-builder

A convenient Fluent-API around building Lucene.Net queries
C#
7
star
36

shallow-element-equals

Efficient shallow equality algorithm that also allows checks for react element equality of children props
JavaScript
7
star
37

babel-preset-react-native

Babel preset for react native... with a few tweaks
JavaScript
6
star
38

enzyme-example-karma

Example project with React + Enzyme + Karma
JavaScript
5
star
39

react-blogger

minimal isomorphic blog engine built on react
JavaScript
3
star
40

mixinjs

A small library to create factories with useful mixins-based inheritence. Inspired by React's createClass method.
JavaScript
3
star
41

murmur2js

An optimized JavaScript implementation of the MurmurHash algorithm.
JavaScript
3
star
42

MiniBlog

Low-Profile blogging framework built in ASP.Net WebPages Framework
JavaScript
3
star
43

keynote-highlight

Tiny web app to help with syntax highlighting code in keynote
HTML
2
star
44

sivi

Automation Software for Siempre Viva Remote Observatory
C++
2
star
45

lelandrichardson.github.io

Source code for intelligiblebabble.com
JavaScript
2
star
46

viki

Viki - The Visual Wiki Platform
JavaScript
2
star
47

funky.js

A functional programming library for JavaScript
JavaScript
1
star
48

gift-wrap

An asset bundler and minifier (CSS, JS, LESS) for ASP.Net MVC projects
C#
1
star
49

native-navigator

A fully "native" and cross platform Navigator for React Native
Objective-C
1
star
50

leap-gesto

JavaScript Gesture API for Leap Motion
JavaScript
1
star
51

Talks

JavaScript
1
star
52

react-native-windowed-listview

Windowed ListView for React Native (experimental)
1
star
53

module-dep-graph

Automatically track a dependency graph of all commonjs modules in an application
JavaScript
1
star
54

relay-global-immutable-store

playing around with a concept of a global store, similar to Relay, with some offline & eventually-consistent features
JavaScript
1
star