Skip to content

Commit

Permalink
Add pMemoize.clear() method
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
sindresorhus committed May 25, 2018
1 parent fb98cea commit 610a959
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
yarn.lock
12 changes: 8 additions & 4 deletions index.js
Expand Up @@ -2,17 +2,21 @@
const mem = require('mem');
const mimicFn = require('mimic-fn');

const memoizedFns = new WeakMap();

module.exports = (fn, opts) => {
const memoized = mem(fn, opts);

const ret = function (...args) {
return memoized.apply(this, args).catch(err => {
mem.clear(memoized);
throw err;
});
return memoized.apply(this, args);
};

mimicFn(ret, fn);
memoizedFns.set(ret, memoized);

return ret;
};

module.exports.clear = fn => {
mem.clear(memoizedFns.get(fn));
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"bluebird"
],
"dependencies": {
"mem": "^1.1.0",
"mem": "^3.0.0",
"mimic-fn": "^1.0.0"
},
"devDependencies": {
Expand Down
42 changes: 30 additions & 12 deletions readme.md
Expand Up @@ -8,7 +8,7 @@ Useful for speeding up consecutive function calls by caching the result of calls
## Install

```
$ npm install --save p-memoize
$ npm install p-memoize
```


Expand All @@ -19,23 +19,41 @@ const pMemoize = require('p-memoize');
const got = require('got');
const memGot = pMemoize(got, {maxAge: 1000});

memGot('sindresorhus.com').then(() => {
// this call is cached
memGot('sindresorhus.com').then(() => {
setTimeout(() => {
// this call is not cached as the cache has expired
memGot('sindresorhus.com').then(() => {});
}, 2000);
});
});
(async () => {
memGot('sindresorhus.com');

// This call is cached
memGot('sindresorhus.com');

setTimeout(() => {
// This call is not cached as the cache has expired
memGot('sindresorhus.com');
}, 2000);
})();
```


## API

See the [`mem` docs](https://github.com/sindresorhus/mem#api).
### pMemoize(input, [options])

Returns a memoized version of the `input` function.

#### input

Type: `Function`

Promise-returning or async function to be memoized.

#### options

Type: `Object`

See the [`mem` options](https://github.com/sindresorhus/mem#options).

### pMemoize.clear(fn)

The only difference is that this module does not cache rejected promises.
Clear all cached data of a memoized function.


## Related
Expand Down
11 changes: 11 additions & 0 deletions test.js
Expand Up @@ -36,3 +36,14 @@ test('does not memoize rejected promise', async t => {
test('preserves the original function name', t => {
t.is(m(async function foo() {}).name, 'foo'); // eslint-disable-line func-names, prefer-arrow-callback
});

test('pMemoize.clear()', t => {
let i = 0;
const f = () => i++;
const memoized = m(f);
t.is(memoized(), 0);
t.is(memoized(), 0);
m.clear(memoized);
t.is(memoized(), 1);
t.is(memoized(), 1);
});

0 comments on commit 610a959

Please sign in to comment.