@5app/memoize
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -0,1 +1,8 @@ | ||
# [1.1.0](https://github.com/5app/memoize/compare/v1.0.2...v1.1.0) (2020-02-21) | ||
### Features | ||
* **useCached:** update useCached to accept a function or boolean ([bfbdd8f](https://github.com/5app/memoize/commit/bfbdd8f77c8e49839f65da005733e5b2aba08714)) | ||
## [1.0.2](https://github.com/5app/memoize/compare/v1.0.1...v1.0.2) (2020-02-21) | ||
@@ -2,0 +9,0 @@ |
16
index.js
@@ -16,3 +16,3 @@ /** | ||
* @param {object} opts - Options | ||
* @param {boolean} [opts.useCached=true] - Whether to serve from previous resolved cached responses | ||
* @param {boolean|Function} [opts.useCached=true] - Whether to serve from previous resolved cached responses | ||
* @param {number} [opts.staleInMs=10000] - Number of milliseconds before the item is regarded as stale | ||
@@ -38,2 +38,12 @@ * @param {Function} [opts.getKey=JSON.stringify] - Function used to define the key for use | ||
// Default check for shouldUseCache | ||
// If we have a resolved value, but we want to keep it up to date set to true | ||
let shouldUseCache = item => item.status === 'resolved' && useCached; | ||
// If the settings say it's a function use that instead | ||
if (typeof useCached === 'function') { | ||
// Update the condition to use the bespoke option | ||
shouldUseCache = useCached; | ||
} | ||
/** | ||
@@ -58,4 +68,4 @@ * Decorator | ||
// If we have a resolved value, but we want to keep it up to date | ||
if (item.status === 'resolved' && useCached) { | ||
// Serve the cached value? | ||
if (item.value && shouldUseCache(item)) { | ||
// We're going to return the last resolved value | ||
@@ -62,0 +72,0 @@ // But first let's identify the next request |
{ | ||
"name": "@5app/memoize", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Memoize decorator", | ||
@@ -60,2 +60,5 @@ "main": "index.js", | ||
] | ||
], | ||
"scope-case": [ | ||
0 | ||
] | ||
@@ -62,0 +65,0 @@ } |
8461
105