Gathering detailed insights and metrics for redux-wait-for-action-rn
Gathering detailed insights and metrics for redux-wait-for-action-rn
Gathering detailed insights and metrics for redux-wait-for-action-rn
Gathering detailed insights and metrics for redux-wait-for-action-rn
Redux middleware to make store.dispatch() return a promise and wait for another action.
npm install redux-wait-for-action-rn
Typescript
Module System
Node Version
NPM Version
JavaScript (97.35%)
Makefile (2.65%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
32 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 22, 2018
Latest Version
0.0.2
Package Id
redux-wait-for-action-rn@0.0.2
Size
5.52 kB
NPM Version
5.6.0
Node Version
8.9.4
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Redux middleware to make store.dispatch()
return a promise which will be fulfilled when another specified action is dispatched, which is useful for universal(isomorphic) React Web Apps with redux and server-side rendering.
Forked from redux-wait-for-action and modified to work with React Native.
npm install --save redux-wait-for-action-rn
Minimal starter kit for universal apps with redux and redux-saga
To fire todos/get
action and subscribe for todos/get/success
action:
1import { WAIT_FOR_ACTION, ERROR_ACTION } from 'redux-wait-for-action'; 2store.dispatch({ 3 type: 'todos/get', 4 [ WAIT_FOR_ACTION ]: 'todos/get/success', // Specify which action we are waiting for 5 [ ERROR_ACTION ]: 'todos/get/failed', // Optional 6}).then( payload => console.log('Todos got!') ) 7.catch( error => console.error('Failed!' + error.message) );
Alternatively, use conditional functions as WAIT_FOR_ACTION
, which is useful when firing multiple actions with same action.type
in parallel:
1store.dispatch({ 2 type: 'profile/get', 3 [ WAIT_FOR_ACTION ]: action => action.type === 'profile/get/success' && action.id === 1, 4 // Only subscribe for profile/get/success action whose profile id equals 1 5 [ ERROR_ACTION ]: action => action.type === 'profile/get/failed' && action.id === 1, 6}).then( payload => console.log('ID #1 Profile got!') ) 7.catch( error => console.error('Failed!' + error.message) );
fetchData()
where we return a store.dispatch()
call followed by automatic execution of side effects. We should call this store.dispatch()
with an action that also contains information about which action we are waiting for.fetchData()
s to populate page data on both client and server side.fetchData().then(() => { /* rendering logic here! */ })
, where side effects are completed and an action with finishing flag is dispatched.store.dispatch()
already returns a promise and you probably don't need this middleware. However, side effects like redux-saga running separately from primitive Redux flow don't explicitly notify us when a specific async fetch is finished, in which case redux-wait-for-action does the trick and makes those async tasks subscribable.runSaga().done
support which returns a promise to tell when a specific saga task is completed, it's quite tricky where saga tasks aren't started by a dispatch()
call and it does't work when using sagas containing infinite loops.configureStore()
function where a Redux store is created on both client and server side:
1import createReduxWaitForMiddleware from 'redux-wait-for-action'; 2 3function configureStore(initialState) { 4 const sagaMiddleware = createSagaMiddleware(); 5 let enhancer = compose( 6 applyMiddleware(sagaMiddleware), 7 applyMiddleware(createReduxWaitForMiddleware()), 8 ); 9 const store = createStore(rootReducer, initialState, enhancer); 10 11 // ... 12}
Assume we have saga effects like this:
1function* getTodosSaga() { 2 const payload = yield call(APIService.getTodos); 3 yield put({ 4 type: 'todos/get/success', 5 payload 6 }); 7} 8function* rootSaga() { 9 yield takeLatest('todos/get', getTodosSaga); 10}
Define a fetchData()
for each of our containers:
1import { WAIT_FOR_ACTION } from 'redux-wait-for-action'; 2 3class TodosContainer extends Component { 4 static fetchData(dispatch) { 5 return dispatch({ 6 type: 'todos/get', 7 [ WAIT_FOR_ACTION ]: 'todos/get/success', 8 }); 9 } 10 componentDidMount() { 11 // Populate page data on client side 12 TodosContainer.fetchData(this.props.dispatch); 13 } 14 // ... 15}
Here in our action we specify WAIT_FOR_ACTION
as 'profile/get/success'
, which tells our promise to wait for another action 'profile/get/success'
. WAIT_FOR_ACTION
is a ES6 Symbol
instance rather than a string, so feel free using it and it won't contaminate your action.
Next for server side rendering, we reuse those fetchData()
s to get the data we need:
1//handler for Express.js 2app.use('*', handleRequest); 3function handleRequest(req, res, next) { 4 //... 5 match({history, routes, location: req.url}, (error, redirectLocation, renderProps) => { 6 //...handlers for redirection, error and null renderProps... 7 8 const getReduxPromise = () => { 9 const component = renderProps.components[renderProps.components.length - 1].WrappedComponent; 10 const promise = component.fetchData ? 11 component.fetchData(store.dispatch) : 12 Promise.resolve(); 13 return promise; 14 }; 15 16 getReduxPromise().then(() => { 17 const initStateString = JSON.stringify(store.getState()); 18 const html = ReactDOMServer.renderToString( 19 <Provider store={store}> 20 { <RouterContext {...renderProps}/> } 21 </Provider> 22 ); 23 res.status(200).send(renderFullPage(html, initStateString)); 24 }); 25 }); 26}
Use try-catch
clause in saga effects. The todos/get/failed
action object should contain a top-level key error
or err
whose value is an error descriptor(An Error()
instance or a string).
1function* getTodosSaga() { 2 yield take('todos/get'); 3 try { 4 const payload = yield call(APIService.getTodos); 5 yield put({ 6 type: 'todos/get/success', 7 payload 8 }); 9 } catch (error) { 10 yield put({ 11 type: 'todos/get/failed', 12 error 13 }); 14 } 15}
Make sure both WAIT_FOR_ACTION
and ERROR_ACTION
symbols are specified in your todos/get
action:
1import { WAIT_FOR_ACTION, ERROR_ACTION } from 'redux-wait-for-action'; 2 3class TodosContainer extends Component { 4 static fetchData(dispatch) { 5 return dispatch({ 6 type: 'todos/get', 7 [ WAIT_FOR_ACTION ]: 'todos/get/success', 8 [ ERROR_ACTION ]: 'todos/get/failed', 9 }); 10 } 11 // ... 12}
Server side rendering logic:
1getReduxPromise().then(() => { 2 // ... 3 res.status(200).send(renderFullPage(html, initStateString)); 4}).catch((error) => { //action.error is passed to here 5 res.status(500).send(error.message); 6});
By default the payload
or data
field on the WAIT_FOR_ACTION
action is provided to the promise when it is resolved, or rejected with the error
or err
field.
There are two additional symbols, CALLBACK_ARGUMENT
and CALLBACK_ERROR_ARGUMENT
, which can be used to override this behavior. If functions are stored on the action using these symbols, they will be invoked and passed the entire action. The result returned from either function is used to resolve or reject the promise based on which symbol was used.
1import { WAIT_FOR_ACTION, ERROR_ACTION, CALLBACK_ARGUMENT, CALLBACK_ERROR_ARGUMENT} from 'redux-wait-for-action'; 2store.dispatch({ 3 type: 'todos/get', 4 [ WAIT_FOR_ACTION ]: 'todos/get/success', 5 [ ERROR_ACTION ]: 'todos/get/failed', 6 [ CALLBACK_ARGUMENT ]: action => action.customData, 7 [ CALLBACK_ERROR_ARGUMENT ]: action => action.customError, 8}).then( customData => console.log('Custom Data: ', customData) ) 9.catch( customError => console.error('Custom Error: ', customError) );
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-07-07
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