Skip to content

Commit 491a04f

Browse files
authored
Merge pull request #13 from kouhin/release/v2.2.0
Release/v2.2.0
2 parents 0e8cc29 + 8cbab13 commit 491a04f

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@ Memoize actionCreator and returns a memoized actionCreator. When dispatch action
143143

144144
#### Returns
145145

146-
- (Promise): will be resolved with the result of original actionCreator.
146+
- (Function): memoized actionCreator. Original action creator can be accessed by `memoize(actionCreator).unmemoized`, e.g.
147+
148+
``` javascript
149+
const actionCreator = () => {};
150+
const memoized = memoize(actionCreator);
151+
console.info(memoized.unmemoized === actionCreator);
152+
```
147153

148154
### createMemoizeMiddleware(globalOpts)
149155

@@ -170,7 +176,7 @@ In 2016, I wrote a library called [redux-dataloader](https://github.com/kouhin/r
170176

171177
## Why not memoize utils such as _.memoize?
172178

173-
Of course memoize utils such as lodash/memoize can solve duplicated requests on browser. But 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.
179+
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.
174180

175181
## LICENSE
176182

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-memoize",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"description": "Memoize action creator for redux, and let you dispatch common/thunk/promise/async action whenever you want to, without worrying about duplication",
55
"main": "lib/index.js",
66
"directories": {

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function memoize(opts, fn) {
100100
if (typeof func !== 'function') {
101101
throw new Error('Not a function');
102102
}
103-
return (...args) => {
103+
const memoized = (...args) => {
104104
const action = {
105105
type: ACTION_TYPE,
106106
payload: {
@@ -113,4 +113,6 @@ export function memoize(opts, fn) {
113113
}
114114
return action;
115115
};
116+
memoized.unmemoized = func;
117+
return memoized;
116118
}

test/middleware.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ describe('memoize', () => {
2929
memoize(() => {});
3030
}).not.toThrow();
3131
});
32+
it('original function should be exposed', () => {
33+
const creator = () => {};
34+
expect(memoize(creator).unmemoized).toBe(creator);
35+
});
3236
});
3337

3438
describe('options', () => {

0 commit comments

Comments
 (0)