Gathering detailed insights and metrics for redux-pender
Gathering detailed insights and metrics for redux-pender
Gathering detailed insights and metrics for redux-pender
Gathering detailed insights and metrics for redux-pender
redux middleware that helps to manages async actions based on promise
npm install redux-pender
Typescript
Module System
72.3
Supply Chain
98.8
Quality
76.1
Maintenance
100
Vulnerability
100
License
JavaScript (50.2%)
TypeScript (49.8%)
Total Downloads
183,327
Last Day
6
Last Week
359
Last Month
1,735
Last Year
25,855
MIT License
101 Stars
86 Commits
7 Forks
7 Watchers
36 Branches
1 Contributors
Updated on Jun 13, 2024
Minified
Minified + Gzipped
Latest Version
2.0.12
Package Id
redux-pender@2.0.12
Unpacked Size
22.91 kB
Size
6.90 kB
File Count
13
Cumulative downloads
Total Downloads
Last Day
-14.3%
6
Compared to previous day
Last Week
-33.1%
359
Compared to previous week
Last Month
-25.4%
1,735
Compared to previous month
Last Year
11.6%
25,855
Compared to previous year
Redux pender is a middleware that helps you to manage asynchronous actions based on promise. It comes with useful tools that help you to handle this even more easier.
This library is inspired from redux-promise-middleware. The difference between redux-promise-middleware and this library is that this comes with some handy utils. Additionally, it also handles the cancellation of the promise-based action. To check out detailed comparisons between other libraries, please check Comparisons document
1npm i --save redux-pender
1import { applyMiddleware, createStore, combineReducers } from 'redux'; 2import penderMiddleware, { penderReducer } from 'redux-pender'; 3 4const reducers = { 5 /* 6 ...your other reducers... 7 */ 8 pender: penderReducer 9}; 10 11const store = createStore( 12 reducers, 13 applyMiddleware(penderMiddleware()) 14);
penderReducer
is the reducer that tracks the status of your asynchronous actions.
store.getState().pender.pending[ACTION_NAME]
will turn true. It will set to false when it succeeds or fails.store.getState().pender.success[ACTION_NAME]
will turn true.store.getState().pender.failure[ACTION_NAME]
will turn true.If you are currently using redux-promise
or redux-promise-middleware
in your project, there will be a collision. To avoid the collision without uninstalling existing library, pass { major: false }
when you initialize the middleware:
1penderMiddleware({ major: false })
pender middleware will process the action when a Promise
is given as the payload
of the action:
1{ 2 type: 'ACTION_TYPE', 3 payload: Promise.resolve() 4}
If you have set major
to false
when you initialize the middleware to avoid the collision with redux-promise
or redux-promise-middleware
, the middleware will only accept following action:
1{ 2 type: 'ACTION_TYPE', 3 payload: { 4 pend: Promise.resolve() 5 } 6}
By default, middleware will accept both of the kinds of actions above.
Since it supports FSA actions, you can use createAction
of redux-actions.
The second parameter of createAction
should be a function that returns a Promise.
1import axios from 'axios'; 2import { createAction } from 'redux-actions'; 3 4const loadPostApi = (postId) => axios.get(`https://jsonplaceholder.typicode.com/posts/${postId}`); 5const LOAD_POST = 'LOAD_POST'; 6const loadPost = createAction(LOAD_POST, loadPostApi); 7store.dispatch(loadPost(1));
If you are using this middleware as {major: false}
, you have to use createPenderAction
1import { createPenderAction } from 'redux-pender'; 2const loadPost = createPenderAction(LOAD_POST, loadPostApi);
It pretty much works quite the same, but it puts the Promise at action.payload.pend
.
When you are making your reducer, it works the best when you are using handleActions
of redux-actions.
Handling action is done by using pender
.
For people who don't know what
handleActions
does, it handles action by creating an object, rather than aswitch
.
1 2import { handleActions } from 'redux-actions'; 3import { pender } from 'redux-pender'; 4 5const initialState = { 6 post: {} 7} 8export default handleActions({ 9 ...pender({ 10 type: LOAD_POST, 11 onSuccess: (state, action) => { 12 return { 13 post: action.payload.data 14 }; 15 } 16 }), 17 // ... other action handlers... 18}, initialState);
Do you want to do something when the action starts or fails? It is simple.
1...pender({ 2 type: LOAD_POST, 3 onPending: (state, action) => { 4 return state; // do something 5 }, 6 onSuccess: (state, action) => { 7 return { 8 post: action.payload.data 9 } 10 }, 11 onFailure: (state, action) => { 12 return state; // do something 13 } 14}, initialState)
When you omit one of those function, (state, action) => state
will be the default value.
Additionally, it is not recommended to manage the status of request in your own reducer, because the penderReducer will do this for you. You just need to care about the result of your task in your reducer.
1 2import { handleActions } from 'redux-actions'; 3import { pender, applyPenders } from 'redux-pender'; 4 5const initialState = { 6 post: {} 7} 8 9const reducer = handleActions({ 10 // ... some other action handlers... 11}, initialState); 12 13export default applyPenders(reducer, [ 14 { 15 type: LOAD_POST, 16 onPending: (state, action) => { 17 return state; // do something 18 }, 19 onSuccess: (state, action) => { 20 return { 21 post: action.payload.data 22 } 23 }, 24 onFailure: (state, action) => { 25 return state; // do something 26 } 27 } 28])
Cancelling the promise based action is very simple in redux-pender. You just have to call .cancel()
from the returned value of your promise based action creator.
1const p = loadPost(1); 2p.cancel();
When cancel
is executed, redux-pender middleware will dispatch ACTION_TYPE_CANCEL
. You can handle that action manually or configure onCancel
in the action pender.
1...pender({ 2 type: LOAD_POST, 3 onCancel: (state, action) => { 4 return state; // do something 5 } 6}, initialState)
1import React, { Component } from 'react'; 2import * as actions from './actions'; 3import { bindActionCreators } from 'redux'; 4import { connect } from 'react-redux'; 5 6class Example extends Component { 7 8 componentDidMount() { 9 this.fetchData(); 10 } 11 12 async fetchData() { 13 const { Actions } = this.props; 14 try { 15 await Actions.loadPost(1); 16 console.log('data is fetched!'); 17 } catch(e) { 18 console.log(e); 19 } 20 } 21 22 render() { 23 const { loading, post } = this.props; 24 25 return ( 26 <div> 27 { loading && 'Loading...' } 28 <div> 29 <h1>{post.title}</h1> 30 <p>{post.body}</p> 31 </div> 32 </div> 33 ); 34 } 35} 36 37export default connect( 38 state => ({ 39 post: state.blog.post, 40 loading: state.pender.pending['LOAD_POST'] 41 }), 42 dispatch => ({ 43 Actions: bindActionCreators(actions, dispatch) 44 }) 45)(Example)
An example project of using this library is provided in examples directory. If you want to see some more complex example, check out do-chat. It is a ChatApp project that uses firebase as backend.
Contributions, questions and pull requests are all welcomed.
Copyright (c) 2017. Velopert Licensed with The MIT License (MIT)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
145 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-09
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More