redux-offline-chain

0.2.3 • Public • Published

redux-offline-chain

A redux-middleware for redux-offline, inspired by redux-thunk.

redux-offline-chain allows you to chain offline actions. It looks for action.meta.then functions and invokes them in a redux-thunk like manner. The then callback gets the payload from the effect reconciler and optionally redux's dispatch and getState functions.

Caveat: Please note that the functions cannot be persisted and therefore only work within the same session.

Installation

$ npm install --save redux-offline-chain
import { applyMiddleware, compose, createStore, Store } from 'redux'
import { offline } from 'redux-offline'
import offlineConfig from 'redux-offline/lib/defaults'
import offlineChain from 'redux-offline-chain'

const store = offline(offlineConfig)(createStore)(
  rootReducer,
  compose(applyMiddleware(offlineChain))
)

Usage

type OfflineAction = {
  type: string,
  payload: any,
  meta: {
    offline: {
      effect: any,
      rollback: Action
      commit: {
        type: string,
        meta: {
+         then: Function,
        },
      },
    },
  }
}
export const actionCreator = payload => ({
  type: 'SOME_ACTION,'
  payload,
  meta: {
    offline: {
      effect: { path: '/some/endpoint' },
      rollback: { type: 'SOME_ACTION_ROLLBACK' },
      commit: {
        type: 'SOME_ACTION_COMMIT',
        meta: {
          then: payload => ({
            type: 'ANOTHER_ACTION',
            payload,
            meta: {
              offline: {
                effect: { path: '/another/endpoint', method: 'POST' },
                rollback: { type: 'ANOTHER_ACTION_ROLLBACK' },
                commit: {
                  type: 'ANOTHER_ACTION_COMMIT',
                  meta: {
                    then: payload => (dispatch, getState) => {
                      // ....
                    },
                  }
                },
              },
            },
          }),
        },
      },
    },
  },
})

Package Sidebar

Install

npm i redux-offline-chain

Weekly Downloads

42

Version

0.2.3

License

MIT

Last publish

Collaborators

  • birgernass