redux-hurakken
v1.3.1
Published
Throttling as a Redux Middleware (4M compliant)
Downloads
3
Maintainers
Readme
Advanced throttling module as a Redux Middleware, by Victor Buzzegoli
Lightweight, Powerfull, 4M 1.6 compliant (check out : Modern Modular Middleware Model)
Often used along with Axiom
Installation
To install Hurakken in your project, navigate to your project folder in your terminal and run :
npm i --save redux-hurakken
Setup
To start using Hurakken, you will first need to apply the middleware to your store, just like any redux middleware :
...
import hurakken from "redux-hurakken";
...
export default createStore(rootReducer,applyMiddleware([hurakken]));
Note that a light version of
Hurakken
is already included in Axiom.
Usage
Using ES6+ syntax
import * as actions from "../constants/action-types";
export const clearArray = () => {
type: actions.CLEAR_ARRAY,
payload: [],
hurakken: {
throttle: 5000
}
}
throttle
is a value in milliseconds for which this action will not be dispatchable again. Note that due to Javascript single threaded environment, this value can be subject to slight variant, and is therefore not precisely defined.
- Throttling can be espacially useful to prevent from overloading a network by rejecting spam attempts. For that reason, Hurakken is included natively into our REST Middleware Axiom.
Behaviour
- Use
onAccepted
andonRejected
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("Rejected Action :", action);
next(action);
};
In /actions
:
import * as actions from "../constants/action-types";
import { customReaction } from "../reactions/customReaction";
export const clearArray = () => {
type: actions.CLEAR_ARRAY,
payload: [],
hurakken: {
throttle: 5000,
onRejected: customReaction
}
}
If you were to use a non 4M compliant middleware such as redux-thunk, which is deprecated by the 4M documentation, please note that, by default, using/dispatching the action returned by
onAccepted
oronRejected
will not trigger Hurakken again even though the arguments are still contained in the action's parameters. To force triggering Hurakken again, use :_skip: false
or remove_skip
in thehurakken
node.
- Use
log: true
to print the middleware logs in the console (addxlog: true
for extended logs)
Here is a overview of every options possible:
hurakken: {
throttle: 3000,
log: true,
xlog: true,
onSuccess: (action, next, dispatch) => {
//...
},
onRejected: (action, next, dispatch) => {
//...
}
}
Version
1.3.1
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