Name: node-middleware
Owner: Reddit
Description: Middleware for Redux
Created: 2016-04-25 21:38:08.0
Updated: 2018-03-03 20:41:48.0
Pushed: 2016-09-22 02:06:09.0
Size: 31
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Helpful Redux middleware
Install via npm.
npm install -S @r/middleware
@r/middleware exports two middlewares: Thunker
and PromiseWell
;
Thunker expands on a traditional Redux thunk utility. Namely, it requires that the thunking action returns a promise. As a result, it becomes easier to know when the thunked actions have completed: when the promise resolves, all thunked actions have fired.
nitializing a Thunker
rt Thunker from '@r/middleware/Thunker';
t thunk = Thunker.create();
n Redux
yMiddleware(..., thunk, ...);
To simplify using Thunker, it is highly recommended es7 style async functions are used.
xample thunked action
t getData = () => async (dispatch, getState, utils) => {
immediately dispatch synchronous actions as normal
spatch(/* sync action */);
wait for data if need be
nst { foo, bar } = await asyncFunctionCall();
continue to dispatch as normal
spatch(/* another action */);
Thunker performs an additional task. Many times, it becomes necessary to wait for some piece of state to update, or to wait for a particular action to be dispatched before another action is dispatched. Thunker solves this by surfacing two utility methods: waitForState
and waitForAction
. Both return a promise that can be await
-ed on.
ForState(stateFn: Function, cb: Function [, stateFailedFn: Function]): Promise
stateFn(state: Object): Boolean
A function that creates the condition that must be met for the callback to fire. Receives a copy of state as its argument. Expected to return a truthy/falsey value.
cb(state: Object): void
A function that is called when the conditional is met. Receives a copy of state as its argument.
stateFailedFn(state: Object): void
(OPTIONAL)
A function that is called the first time the conditional is not met. Only fires once. Receives a copy of state as its argument.
ForAction(actionFn: Function, cb: Function): Promise
actionFn: Function(action: Object)
A function that receives a Redux action, represents the condition that must be met for the callback to fire. After waitForAction
is invoked, if action
is dispatched, the callback will fire.
cb(state: Object): void
A function that is called when the action
is dispatched. Receives a copy of state as its argument.
The PromiseWell merely collects promises that dispatched by other middleware. It should be one of the last middleware invoked.
nitializing a PromiseWell
rt PromiseWell from '@r/middleware/PromiseWell';
t well = PromiseWell.create();
n Redux
yMiddleware(..., well.middleware, ...);
n the server side
t well.onComplete();
In addition to capturing promises, the PromiseWell lets you query to check if all the captured promises have been completed through the use of onComplete
.