redux-memoize
Memoize action creator for redux, and let you dispatch common/thunk/promise/async action whenever you want to, without worrying about duplication.
npm install --save redux-memoize
Installation
npm install --save redux-memoize
Then create the redux-memoize middleware.
;; // a common action creatorconst increment = { return type: 'INCREMENT' payload: 1 ;}; // This is a memoized action creator.const memoizeIncrement = ; // Reducer { } const store = ; store;console; // OUTPUT: 1store;console; // OUTPUT: 2 const promise1 = store; // return a cached Promiseconsole; // OUTPUT: 3 const promise2 = store; // return previous cached Promiseconsole; // OUTPUT: 3, increment() didn't runconsole; OUTPUT: true // NOTICE: only works on browser.// In order to prevent memory leak, cached action creator will not be evicted on server side by default.// So the following code will output 3 on server side.// To enable eviction on server, use createMemoizeMiddleware({ disableTTL: false });
It works perfectly with redux-thunk
;;;; const fetchUserSuccess = { return type: 'FETCH_USER/SUCCESS' payload: user ;}); let creatorCalled = 0;let thunkCalled = 0; const fetchUserRequest = ; const store = ; // Component1const promise1 = store ; // Component2const promise2 = store ; Promiseallpromise1 promise2 ;
API
memoize(opts, actionCreator)
Memoize actionCreator and returns a memoized actionCreator. When dispatch action that created by memorized actionCreator, it will returns a Promise.
Arguments
opts
Object | numberttl
Number|Function: The time to live for cached action creator. Whenttl
is a function,getState
will be passed as argument, and it must returns a number.enabled
Boolean|Function: Whether use memorized action creator or not. Whenfalse
, cache will be ignored and the result of original action creator will be dispatched without caching. Whenenabled
is a function,getState
will be passed argument, and it must returns a boolean.isEqual
: arguments of action creator will be used as the map cache key. It uses lodash.isEqual to find the existed cached action creator. You can customize this function.- If
opts
is a number, the numbrer specifies the ttl.
Returns
- (Function): memoized actionCreator. Original action creator can be accessed by
memoize(actionCreator).unmemoized
, e.g.
const actionCreator = {};const memoized = ;console;
createMemoizeMiddleware(globalOpts)
Create a redux middleware.
Arguments
globalOpts
Object- Object: Default opts for memorize().
- Default:
{ ttl:0, enabled: true, isEqual: lodash.isEqual }
]. ttl is REQUIRED, You SHOULD set a ttl > 0 in millisecond - There is another options
disableTTL
. The default value istrue
on server andfalse
on browser. By default, cached action creator will not be evicted by setTimeout with TTL on server in order to prevent memory leak. You can enable it for test purpose. - You can pass a customized cache by
cache
instead of default cachenew WeakMap()
.
Returns
- (Function): Redux middleware.
You can find more examples in test files.
Motivation
redux-thunk and redux-saga are two popular libraries to handle asynchronous flow. redux-saga
monitors dispatched actions and make side effects. It's very powerful and you can use it to control in almost every detail in asynchronous flow. However it is a little complex. redux-thunk
is simple and artfully designed. It's created only by 11 lines of code. I like it very much but it can't solve the problem of duplicated requests.
In 2016, I wrote a library called redux-dataloader. It monitors dispatched action and avoids duplicated requests. We use this library in out project and it works well. But I think it's still a little complex and want to make it simpler just like redux-thunk. Because for a single task we have to create data loader and three actions and switch between actions and data loaders and get boring. Then I create this middleware just for reducing duplicated thunk calls. It works pretty good with redux-thunk and common actions, may even works with other middlewares such as redux-promise and so on.
Why not memoize utils such as _.memoize?
Of course memoize utils such as lodash/memoize can solve duplicated requests on browser. However, _.memoize
only puts the result of action creator into cache, and the result of dispatch()
cannot be cached. When the result of a action creator is a function (thunk), the function will still be executed by thunk middleware. It means _.memoize
can't cache thunk, and the async action will still be duplicated. Besides, it may cause memory problem on server side. On the server side, we will create a new store for each request. Since this library holds cache in middleware that is created with createStore, cache will be cleaned up after request by GC. It won't cause memory leak problem. What's more, it supports dynamic ttl
and enabled
by store.getState()
, so you can change these opions from remote api when needed.
LICENSE
MIT