redux-phunk
A redux middleware to encourage smarter reducers and more phunktional action creators.
Motivation
Reducers are the best place to put conditional logic and data transformations, but many redux middleware patterns encourage putting that stuff into action creators. By moving logic out of action creators and into reducers, the logic is more reusable and testable. Redux-phunk makes your action creators just smart enough to handle async actions like API calls, but keeps them dumb enough so that conditional logic and data transformations remain in reducers.
What does it do?t
Redux-phunk allows you to dispatch an array of actions that will be executed in sequence. For asynchronous code such as ajax, a special type of object (called a phunk
) is included in the array, which all subsequent actions in the sequence will wait for.
When you dispatch an array of actions, the return value will be a Promise that resolves when all of the actions in the array have been dispatched.
; { return type: "GETTING_WEATHER" name: "currentWeather" async { return await ; } type: "GOT_WEATHER" weather: ;} store;
Installation
npm install --save redux-phunk
Then use applyMiddleware to use redux-phunk.
;;; const store = ; store;
Phunks?? That's not even a word!
"Phunks" are objects which complement actions. They are a way of waiting for asynchronous code (such as ajax, setTimeout, etc) before dispatching actions.
To use phunks, dispatch an array of objects where at least one of the objects has a name
and phunk
property. The name
is just a string, and phunk
is a function that returns an asynchronous value, such as a Promise or a (single-valued) Observable.
Examples:
// es2016 name: "getUser" async { return await ; }
// es6 + Observables name: "getTypeaheadOptions" { return Observable; }
// es5 name: "saveComment" { return { } }}
Putting async values into vanilla redux actions
Running asynchronous code as a "phunk" is great, but in order to actually use the asynchronous value, redux-phunk provides the asyncValue
function. This is used inside of vanilla redux actions so that the asynchronous value is passed to the reducer, where it will be transformed and handled properly. In the following example, the /api/books/:id api will first be called. Once the ajax request is completed, GOT_BOOK will be dispatched with newBook
set to whatever the api returned for the book:
; { return name: 'book' async { return await ; } type: 'GOT_BOOK' newBook: } store;
Using async values inside of another phunk
If you need to reuse asynchronous values across multiple phunks, you can call the valueOf
function provided as the first argument to each phunk.
; { return name: 'user' async { return await } name: 'userPreferences' async { const userPreferencesId = preferencesId; return await } type: 'NEW_USER_PREFERENCES' preferences: ;}
Knowing when a sequence is done
When dispatching an array of actions (including phunks), the return value of dispatch()
will be a promise that resolves when all the actions have been dispatched. The promise will be resolved with all values returned from phunks in an object with key/value pairs. For example:
store { return name: 'value1' async { return "this is a value"; } type: 'VALUE_1' val: name: 'value2' async { return "this is another value"; } type: 'VALUE_2' val: ;}
Catching errors
You can handle errors for each phunk, for the entire sequence of actions, or both. If handling errors for the entire sequence of actions, use a catchAll
object at the very end of your array. catchAll
functions may return nothing, a synchronous value, a Promise, or an Observable.
; { return name: 'deletePost' async { return await } type: 'DELETED_POST' id { // This is where all errors will be funneled no matter what logErrors } ;}