Skip to content

Commit

Permalink
feat: memoize and memoizeAsync now take function args as additional p…
Browse files Browse the repository at this point in the history
…arams
  • Loading branch information
Lukas Siemon committed Aug 24, 2024
1 parent 93485aa commit 433bfc8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ When set to other than `true`, the `null` value is not cached with `memoize` and

## Additional Functions

### memoize(key: String, valueFn: function)
### memoize(key: String, valueFn: function, ...args)

Only when the key is not present in cache (or has expired), `valueFn` is called and placed into cache.

Expand All @@ -42,7 +42,7 @@ The cache is left empty if an error is thrown at any point in `valueFn` (even as

Useful when multiple async operation need to access the same async information.

### memoizeSync(key: String, valueFn: function)
### memoizeSync(key: String, valueFn: function, ...args)

Similar to "memoize", when key is not present (or has expired), `valueFn` is called and placed into the cache.

Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export default class LRUe extends LRU {
this.cacheNull = cacheNull;
}

async memoize(key, valueFn) {
async memoize(key, valueFn, ...args) {
assert(typeof valueFn === 'function');
if (!this.has(key)) {
this.set(key, valueFn());
this.set(key, valueFn(args));
}
try {
const r = await this.peek(key);
Expand All @@ -26,10 +26,10 @@ export default class LRUe extends LRU {
}
}

memoizeSync(key, valueFn) {
memoizeSync(key, valueFn, ...args) {
assert(typeof valueFn === 'function');
if (!this.has(key)) {
const r = valueFn();
const r = valueFn(args);
assert(!(r instanceof Promise), 'Use memoize() instead');
this.set(key, r);
}
Expand Down

0 comments on commit 433bfc8

Please sign in to comment.