1.0.2 • Published 6 years ago

redux-alfred v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

alfred

Redux Middleware to dispatch several actions at once, by Victor Buzzegoli

Lightweight, Powerfull, 4M 1.6 compliant (check out : Modern Modular Middleware Model)

Installation

To install Alfred in your project, navigate to your project folder in your terminal and run :

npm i --save redux-alfred

Setup

To start using Alfred, you will first need to apply the middleware to your store, just like any redux middleware :

    ...
    import alfred from "redux-alfred";
    ...
    export default createStore(rootReducer,applyMiddleware([alfred]));

Usage

Using ES6+ syntax

    import * as actions from "../constants/action-types";

    export const someAction = () => {
        type: actions.DISPATCH_GROUP,
        payload: 0,
        alfred: {
            actions: [
                {type: actions.ANOTHER_ACTION, payload: 0},
                {type: actions.YET_ANOTHER_ACTION, payload: 0}
            ]
        }
    }

actions must be an array of action to be dispatched

Note that by default the parent action DISPATCH_GROUP will not be forwarded to the next middleware / reducer. If this action needs to be dispatched as well, it can be added as an action in alfred.actions.

Behaviour

  • Use onDispatch and onError to override the default behaviour

Note that these functions are called reactions, accordingly to the Modern Modular Middleware Model. Therefore they contain a next argument that can be use to release an action to the reducer (or next middleware). They can be used like :

In /reactions :

export const customReaction = (action, next, dispatch) => {
    console.log("Will be dispatched :", action);
    next(action);
};

In /actions :

    import * as actions from "../constants/action-types";
    import { customReaction } from "../reactions/customReaction";

    export const someAction = () => {
        type: actions.DISPATCH_GROUP,
        payload: 0,
        alfred: {
            actions: [
                {type: actions.ANOTHER_ACTION, payload: 0},
                {type: actions.YET_ANOTHER_ACTION, payload: 0}
            ]
        }
    }
  • Use log: true to print the middleware logs in the console

Here is a overview of every options possible:

    alfred: {
        actions: [
            {type: actions.ANOTHER_ACTION, payload: 0},
            {type: actions.YET_ANOTHER_ACTION, payload: 0},
            ...
        ],
        log: true,
        onDispatch: (action, next, dispatch) => {
            //...
        },
        onError: (prevAction, next, dispatch) => {
            //...
        }
    }

Version

1.0.2

License

The MIT License

Copyright 2018 - Victor Buzzegoli

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contact

@victorbuzzegoli on Twitter