@keyvhq/memoize 2.1.14 → 2.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/index.js +10 -11
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@keyvhq/memoize",
|
|
3
3
|
"description": "Memoize any function using Keyv as storage backend.",
|
|
4
4
|
"homepage": "https://keyvhq.js.org",
|
|
5
|
-
"version": "2.1.
|
|
5
|
+
"version": "2.1.16",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"author": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"value"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@keyvhq/core": "2.1.
|
|
32
|
+
"@keyvhq/core": "2.1.15",
|
|
33
33
|
"mimic-fn": "~3.0.0",
|
|
34
34
|
"null-prototype-object": "~1.2.5"
|
|
35
35
|
},
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "c24a2c751c11c30794f3f743dc4bc6bd4433c41f"
|
|
53
53
|
}
|
package/src/index.js
CHANGED
|
@@ -4,10 +4,6 @@ const NullProtoObj = require('null-prototype-object')
|
|
|
4
4
|
const Keyv = require('@keyvhq/core')
|
|
5
5
|
const mimicFn = require('mimic-fn')
|
|
6
6
|
|
|
7
|
-
const isFunction = input => typeof input === 'function'
|
|
8
|
-
const isNumber = input => typeof input === 'number'
|
|
9
|
-
const isString = input => typeof input === 'string'
|
|
10
|
-
const isUndefined = input => input === undefined
|
|
11
7
|
const identity = value => value
|
|
12
8
|
|
|
13
9
|
function memoize (
|
|
@@ -22,10 +18,10 @@ function memoize (
|
|
|
22
18
|
} = {}
|
|
23
19
|
) {
|
|
24
20
|
const keyv = keyvOptions instanceof Keyv ? keyvOptions : new Keyv(keyvOptions)
|
|
25
|
-
const ttl =
|
|
26
|
-
const staleTtl =
|
|
21
|
+
const ttl = typeof rawTtl === 'function' ? rawTtl : () => rawTtl
|
|
22
|
+
const staleTtl = typeof rawStaleTtl === 'function'
|
|
27
23
|
? rawStaleTtl
|
|
28
|
-
:
|
|
24
|
+
: typeof rawStaleTtl === 'number'
|
|
29
25
|
? () => rawStaleTtl
|
|
30
26
|
: rawStaleTtl
|
|
31
27
|
|
|
@@ -40,7 +36,7 @@ function memoize (
|
|
|
40
36
|
*/
|
|
41
37
|
async function getRaw (key) {
|
|
42
38
|
const raw = await keyv.store.get(keyv._getKeyPrefix(key))
|
|
43
|
-
return
|
|
39
|
+
return typeof raw === 'string' ? keyv.deserialize(raw) : raw
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
/**
|
|
@@ -61,14 +57,14 @@ function memoize (
|
|
|
61
57
|
const rawKey = getKey(...args)
|
|
62
58
|
const [key, forceExpiration] = Array.isArray(rawKey) ? rawKey : [rawKey]
|
|
63
59
|
|
|
64
|
-
if (
|
|
60
|
+
if (pending[key] !== undefined) return pending[key]
|
|
65
61
|
|
|
66
62
|
pending[key] = getRaw(key).then(async data => {
|
|
67
|
-
const hasValue = data ?
|
|
63
|
+
const hasValue = data ? data.value !== undefined : false
|
|
68
64
|
const hasExpires = hasValue && typeof data.expires === 'number'
|
|
69
65
|
const ttlValue = hasExpires ? data.expires - Date.now() : undefined
|
|
70
66
|
const staleTtlValue =
|
|
71
|
-
hasExpires &&
|
|
67
|
+
hasExpires && staleTtl !== undefined ? staleTtl(data.value) : false
|
|
72
68
|
const isExpired =
|
|
73
69
|
forceExpiration === true
|
|
74
70
|
? forceExpiration
|
|
@@ -101,6 +97,9 @@ function memoize (
|
|
|
101
97
|
pending[key] = undefined
|
|
102
98
|
throw error
|
|
103
99
|
}
|
|
100
|
+
}).catch(error => {
|
|
101
|
+
pending[key] = undefined
|
|
102
|
+
throw error
|
|
104
103
|
})
|
|
105
104
|
|
|
106
105
|
return pending[key]
|