@checkdigit/memoize 1.0.0 → 1.1.0-PR.4-f8a0
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/README.md +16 -5
- package/dist-cjs/memoize.cjs +13 -8
- package/dist-mjs/memoize.mjs +13 -8
- package/dist-types/memoize.d.ts +3 -4
- package/package.json +1 -75
- package/src/memoize.ts +21 -12
package/README.md
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2023 [Check Digit, LLC](https://checkdigit.com)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
The promise
|
|
7
|
-
|
|
5
|
+
`@checkdigit/memoize` memoizes async functions, with cache eviction on reject.
|
|
6
|
+
The resulting promise is cached, guaranteeing the underlying function is called at most once for a given set of
|
|
7
|
+
arguments, unless the promise ultimately rejects.
|
|
8
|
+
|
|
9
|
+
If the promise is rejected, it will be removed from the memoize cache.
|
|
8
10
|
This allows the function to be retried.
|
|
9
11
|
|
|
10
12
|
### Install
|
|
@@ -18,9 +20,18 @@ $ npm install @checkdigit/memoize
|
|
|
18
20
|
```
|
|
19
21
|
import memoize from '@checkdigit/memoize';
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
async function doIt(parameter: number): Promise<number> {
|
|
24
|
+
console.log('doing it', parameter); // doing it 123, doing it 456 (note parameter=123 only executed once)
|
|
25
|
+
return parameter * 2;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const memoizedDoIt = memoize(doIt);
|
|
29
|
+
|
|
30
|
+
const value1 = await memoizedDoIt(123);
|
|
31
|
+
const value2 = await memoizedDoIt(456);
|
|
32
|
+
const value3 = await memoizedDoIt(123); // uses cached result
|
|
22
33
|
|
|
23
|
-
|
|
34
|
+
console.log('the results were', value1, value2, value3); // the results were 246 912 246
|
|
24
35
|
```
|
|
25
36
|
|
|
26
37
|
## License
|
package/dist-cjs/memoize.cjs
CHANGED
|
@@ -23,19 +23,24 @@ __export(memoize_exports, {
|
|
|
23
23
|
default: () => memoize_default
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(memoize_exports);
|
|
26
|
-
var memoize_default = (
|
|
26
|
+
var memoize_default = (memoizableFunction) => {
|
|
27
27
|
const cache = /* @__PURE__ */ new Map();
|
|
28
|
-
return (...
|
|
29
|
-
const
|
|
30
|
-
|
|
28
|
+
return (...argumentList) => {
|
|
29
|
+
const keys = /* @__PURE__ */ new Set();
|
|
30
|
+
JSON.stringify(argumentList, (key, value2) => {
|
|
31
|
+
keys.add(key);
|
|
32
|
+
return value2;
|
|
33
|
+
});
|
|
34
|
+
const cacheKey = JSON.stringify(argumentList, [...keys].sort());
|
|
35
|
+
let value = cache.get(cacheKey);
|
|
31
36
|
if (value === void 0) {
|
|
32
|
-
value =
|
|
33
|
-
cache.set(
|
|
37
|
+
value = memoizableFunction(...argumentList);
|
|
38
|
+
cache.set(cacheKey, value);
|
|
34
39
|
value.catch(() => {
|
|
35
|
-
cache.delete(
|
|
40
|
+
cache.delete(cacheKey);
|
|
36
41
|
});
|
|
37
42
|
}
|
|
38
43
|
return value;
|
|
39
44
|
};
|
|
40
45
|
};
|
|
41
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
46
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL21lbW9pemUudHMiXSwKICAibWFwcGluZ3MiOiAiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUFBO0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFvQkEsSUFBTyxrQkFBUSxDQUNiLHVCQUMwQztBQUMxQyxRQUFNLFFBQVEsb0JBQUksSUFBNkI7QUFDL0MsU0FBTyxJQUFJLGlCQUFpQjtBQUUxQixVQUFNLE9BQU8sb0JBQUksSUFBcUI7QUFDdEMsU0FBSyxVQUFVLGNBQWMsQ0FBQyxLQUFLQSxXQUFtQjtBQUNwRCxXQUFLLElBQUksR0FBRztBQUNaLGFBQU9BO0FBQUEsSUFDVCxDQUFDO0FBR0QsVUFBTSxXQUFXLEtBQUssVUFBVSxjQUFjLENBQUMsR0FBRyxJQUFJLEVBQUUsS0FBSyxDQUFDO0FBRTlELFFBQUksUUFBUSxNQUFNLElBQUksUUFBUTtBQUM5QixRQUFJLFVBQVUsUUFBVztBQUN2QixjQUFRLG1CQUFtQixHQUFHLFlBQVk7QUFDMUMsWUFBTSxJQUFJLFVBQVUsS0FBSztBQUN6QixZQUFNLE1BQU0sTUFBTTtBQUVoQixjQUFNLE9BQU8sUUFBUTtBQUFBLE1BQ3ZCLENBQUM7QUFBQSxJQUNIO0FBQ0EsV0FBTztBQUFBLEVBQ1Q7QUFDRjsiLAogICJuYW1lcyI6IFsidmFsdWUiXQp9Cg==
|
package/dist-mjs/memoize.mjs
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
// src/memoize.ts
|
|
2
|
-
var memoize_default = (
|
|
2
|
+
var memoize_default = (memoizableFunction) => {
|
|
3
3
|
const cache = /* @__PURE__ */ new Map();
|
|
4
|
-
return (...
|
|
5
|
-
const
|
|
6
|
-
|
|
4
|
+
return (...argumentList) => {
|
|
5
|
+
const keys = /* @__PURE__ */ new Set();
|
|
6
|
+
JSON.stringify(argumentList, (key, value2) => {
|
|
7
|
+
keys.add(key);
|
|
8
|
+
return value2;
|
|
9
|
+
});
|
|
10
|
+
const cacheKey = JSON.stringify(argumentList, [...keys].sort());
|
|
11
|
+
let value = cache.get(cacheKey);
|
|
7
12
|
if (value === void 0) {
|
|
8
|
-
value =
|
|
9
|
-
cache.set(
|
|
13
|
+
value = memoizableFunction(...argumentList);
|
|
14
|
+
cache.set(cacheKey, value);
|
|
10
15
|
value.catch(() => {
|
|
11
|
-
cache.delete(
|
|
16
|
+
cache.delete(cacheKey);
|
|
12
17
|
});
|
|
13
18
|
}
|
|
14
19
|
return value;
|
|
@@ -17,4 +22,4 @@ var memoize_default = (function_) => {
|
|
|
17
22
|
export {
|
|
18
23
|
memoize_default as default
|
|
19
24
|
};
|
|
20
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
25
|
+
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vc3JjL21lbW9pemUudHMiXSwKICAibWFwcGluZ3MiOiAiO0FBb0JBLElBQU8sa0JBQVEsQ0FDYix1QkFDMEM7QUFDMUMsUUFBTSxRQUFRLG9CQUFJLElBQTZCO0FBQy9DLFNBQU8sSUFBSSxpQkFBaUI7QUFFMUIsVUFBTSxPQUFPLG9CQUFJLElBQXFCO0FBQ3RDLFNBQUssVUFBVSxjQUFjLENBQUMsS0FBS0EsV0FBbUI7QUFDcEQsV0FBSyxJQUFJLEdBQUc7QUFDWixhQUFPQTtBQUFBLElBQ1QsQ0FBQztBQUdELFVBQU0sV0FBVyxLQUFLLFVBQVUsY0FBYyxDQUFDLEdBQUcsSUFBSSxFQUFFLEtBQUssQ0FBQztBQUU5RCxRQUFJLFFBQVEsTUFBTSxJQUFJLFFBQVE7QUFDOUIsUUFBSSxVQUFVLFFBQVc7QUFDdkIsY0FBUSxtQkFBbUIsR0FBRyxZQUFZO0FBQzFDLFlBQU0sSUFBSSxVQUFVLEtBQUs7QUFDekIsWUFBTSxNQUFNLE1BQU07QUFFaEIsY0FBTSxPQUFPLFFBQVE7QUFBQSxNQUN2QixDQUFDO0FBQUEsSUFDSDtBQUNBLFdBQU87QUFBQSxFQUNUO0FBQ0Y7IiwKICAibmFtZXMiOiBbInZhbHVlIl0KfQo=
|
package/dist-types/memoize.d.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import type { URL } from 'node:url';
|
|
3
1
|
export type Argument = string | number | boolean | null | Date | URL | Argument[] | {
|
|
4
2
|
[key: string]: Argument;
|
|
5
3
|
};
|
|
4
|
+
export type MemoizableFunction<Arguments extends Argument[], Return> = (...argumentList: Arguments) => Promise<Return>;
|
|
6
5
|
/**
|
|
7
6
|
* Memoize an async function. The promise is cached, not the value. This guarantees the underlying function is
|
|
8
7
|
* called at most once for a given set of arguments, unless the promise is rejected.
|
|
9
8
|
* If the promise is rejected, it will be removed from the memoize cache. This allows the function to be retried.
|
|
10
9
|
*
|
|
11
|
-
* @param
|
|
10
|
+
* @param memoizableFunction async function to memoize
|
|
12
11
|
*/
|
|
13
|
-
declare const _default: <
|
|
12
|
+
declare const _default: <Arguments extends Argument[], Return>(memoizableFunction: MemoizableFunction<Arguments, Return>) => MemoizableFunction<Arguments, Return>;
|
|
14
13
|
export default _default;
|
package/package.json
CHANGED
|
@@ -1,75 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@checkdigit/memoize",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "Memoize is a small async function for memoizing promises with cache eviction on reject",
|
|
5
|
-
"exports": {
|
|
6
|
-
".": {
|
|
7
|
-
"types": "./dist-types/index.d.ts",
|
|
8
|
-
"require": "./dist-cjs/index.cjs",
|
|
9
|
-
"import": "./dist-mjs/index.mjs",
|
|
10
|
-
"default": "./dist-mjs/index.mjs"
|
|
11
|
-
}
|
|
12
|
-
},
|
|
13
|
-
"files": [
|
|
14
|
-
"src",
|
|
15
|
-
"dist-types",
|
|
16
|
-
"dist-cjs",
|
|
17
|
-
"dist-mjs",
|
|
18
|
-
"!src/**/*.test.ts",
|
|
19
|
-
"!src/**/*.spec.ts",
|
|
20
|
-
"!dist-types/**/*.test.d.ts",
|
|
21
|
-
"!dist-types/**/*.spec.d.ts",
|
|
22
|
-
"!dist-cjs/**/*.test.cjs",
|
|
23
|
-
"!dist-cjs/**/*.spec.cjs",
|
|
24
|
-
"!dist-mjs/**/*.test.mjs",
|
|
25
|
-
"!dist-mjs/**/*.spec.mjs",
|
|
26
|
-
"SECURITY.md"
|
|
27
|
-
],
|
|
28
|
-
"prettier": "@checkdigit/prettier-config",
|
|
29
|
-
"engines": {
|
|
30
|
-
"node": ">=18"
|
|
31
|
-
},
|
|
32
|
-
"repository": {
|
|
33
|
-
"type": "git",
|
|
34
|
-
"url": "git+https://github.com/checkdigit/memoize.git"
|
|
35
|
-
},
|
|
36
|
-
"author": "Check Digit, LLC",
|
|
37
|
-
"license": "MIT",
|
|
38
|
-
"bugs": {
|
|
39
|
-
"url": "https://github.com/checkdigit/memoize/issues"
|
|
40
|
-
},
|
|
41
|
-
"homepage": "https://github.com/checkdigit/memoize#readme",
|
|
42
|
-
"devDependencies": {
|
|
43
|
-
"@checkdigit/eslint-config": "^7.8.0",
|
|
44
|
-
"@checkdigit/jest-config": "^2.3.0",
|
|
45
|
-
"@checkdigit/prettier-config": "^3.4.0",
|
|
46
|
-
"@checkdigit/typescript-config": "^3.5.0",
|
|
47
|
-
"@types/uuid": "^9.0.2",
|
|
48
|
-
"rimraf": "^5.0.1",
|
|
49
|
-
"uuid": "^9.0.0"
|
|
50
|
-
},
|
|
51
|
-
"eslintConfig": {
|
|
52
|
-
"extends": [
|
|
53
|
-
"@checkdigit/eslint-config"
|
|
54
|
-
]
|
|
55
|
-
},
|
|
56
|
-
"jest": {
|
|
57
|
-
"preset": "@checkdigit/jest-config"
|
|
58
|
-
},
|
|
59
|
-
"scripts": {
|
|
60
|
-
"prepublishOnly": "npm run build:dist-types && npm run build:dist-cjs && npm run build:dist-esm",
|
|
61
|
-
"build:dist-types": "rimraf dist-types && npx builder --type=types --outDir=dist-types",
|
|
62
|
-
"build:dist-cjs": "rimraf dist-cjs && npx builder --type=commonjs --sourceMap --outDir=dist-cjs",
|
|
63
|
-
"build:dist-esm": "rimraf dist-esm && npx builder --type=module --sourceMap --outDir=dist-mjs",
|
|
64
|
-
"prettier": "prettier --ignore-path .gitignore --list-different .",
|
|
65
|
-
"prettier:fix": "prettier --ignore-path .gitignore --write .",
|
|
66
|
-
"lint:fix": "eslint -f unix --ext .ts,.mts src --fix",
|
|
67
|
-
"lint": "eslint -f unix --ext .ts,.mts src",
|
|
68
|
-
"test": "npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style",
|
|
69
|
-
"ci:compile": "tsc --noEmit",
|
|
70
|
-
"ci:test": "jest --coverage=false",
|
|
71
|
-
"ci:coverage": "jest --coverage=true",
|
|
72
|
-
"ci:lint": "npm run lint",
|
|
73
|
-
"ci:style": "npm run prettier"
|
|
74
|
-
}
|
|
75
|
-
}
|
|
1
|
+
{"name":"@checkdigit/memoize","version":"1.1.0-PR.4-f8a0","description":"Memoizing promises with cache eviction on reject","exports":{".":{"types":"./dist-types/index.d.ts","require":"./dist-cjs/index.cjs","import":"./dist-mjs/index.mjs","default":"./dist-mjs/index.mjs"}},"files":["src","dist-types","dist-cjs","dist-mjs","!src/**/*.test.ts","!src/**/*.spec.ts","!dist-types/**/*.test.d.ts","!dist-types/**/*.spec.d.ts","!dist-cjs/**/*.test.cjs","!dist-cjs/**/*.spec.cjs","!dist-mjs/**/*.test.mjs","!dist-mjs/**/*.spec.mjs","SECURITY.md"],"prettier":"@checkdigit/prettier-config","engines":{"node":">=18"},"repository":{"type":"git","url":"git+https://github.com/checkdigit/memoize.git"},"author":"Check Digit, LLC","license":"MIT","bugs":{"url":"https://github.com/checkdigit/memoize/issues"},"homepage":"https://github.com/checkdigit/memoize#readme","devDependencies":{"@checkdigit/eslint-config":"^8.1.0","@checkdigit/jest-config":"^3.0.0","@checkdigit/prettier-config":"^4.0.0","@checkdigit/typescript-config":"^4.0.0","@types/uuid":"^9.0.2","rimraf":"^5.0.1","uuid":"^9.0.0"},"eslintConfig":{"extends":["@checkdigit/eslint-config"]},"jest":{"preset":"@checkdigit/jest-config"},"scripts":{"prepublishOnly":"npm run build:dist-types && npm run build:dist-cjs && npm run build:dist-esm","build:dist-types":"rimraf dist-types && npx builder --type=types --outDir=dist-types","build:dist-cjs":"rimraf dist-cjs && npx builder --type=commonjs --sourceMap --outDir=dist-cjs","build:dist-esm":"rimraf dist-esm && npx builder --type=module --sourceMap --outDir=dist-mjs","prettier":"prettier --ignore-path .gitignore --list-different .","prettier:fix":"prettier --ignore-path .gitignore --write .","lint:fix":"eslint -f unix --ext .ts,.mts src --fix","lint":"eslint -f unix --ext .ts,.mts src","test":"npm run ci:compile && npm run ci:test && npm run ci:lint && npm run ci:style","ci:compile":"tsc --noEmit","ci:test":"jest --coverage=false","ci:coverage":"jest --coverage=true","ci:lint":"npm run lint","ci:style":"npm run prettier"}}
|
package/src/memoize.ts
CHANGED
|
@@ -6,31 +6,40 @@
|
|
|
6
6
|
* This code is licensed under the MIT license (see LICENSE.txt for details).
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { URL } from 'node:url';
|
|
10
|
-
|
|
11
9
|
// arguments must be stringify-able
|
|
12
10
|
export type Argument = string | number | boolean | null | Date | URL | Argument[] | { [key: string]: Argument };
|
|
13
11
|
|
|
12
|
+
export type MemoizableFunction<Arguments extends Argument[], Return> = (...argumentList: Arguments) => Promise<Return>;
|
|
13
|
+
|
|
14
14
|
/**
|
|
15
15
|
* Memoize an async function. The promise is cached, not the value. This guarantees the underlying function is
|
|
16
16
|
* called at most once for a given set of arguments, unless the promise is rejected.
|
|
17
17
|
* If the promise is rejected, it will be removed from the memoize cache. This allows the function to be retried.
|
|
18
18
|
*
|
|
19
|
-
* @param
|
|
19
|
+
* @param memoizableFunction async function to memoize
|
|
20
20
|
*/
|
|
21
|
-
export default <
|
|
22
|
-
|
|
23
|
-
):
|
|
21
|
+
export default <Arguments extends Argument[], Return>(
|
|
22
|
+
memoizableFunction: MemoizableFunction<Arguments, Return>,
|
|
23
|
+
): MemoizableFunction<Arguments, Return> => {
|
|
24
24
|
const cache = new Map<string, Promise<Return>>();
|
|
25
|
-
return (...
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
return (...argumentList) => {
|
|
26
|
+
// create a set of all object keys used in the argument list
|
|
27
|
+
const keys = new Set<string | number>();
|
|
28
|
+
JSON.stringify(argumentList, (key, value: unknown) => {
|
|
29
|
+
keys.add(key);
|
|
30
|
+
return value;
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// create a cache key so that e.g. [{ a: 1, b: 2 }] and [{ b: 2, a: 1 }] map to the same cache entry
|
|
34
|
+
const cacheKey = JSON.stringify(argumentList, [...keys].sort());
|
|
35
|
+
|
|
36
|
+
let value = cache.get(cacheKey);
|
|
28
37
|
if (value === undefined) {
|
|
29
|
-
value =
|
|
30
|
-
cache.set(
|
|
38
|
+
value = memoizableFunction(...argumentList);
|
|
39
|
+
cache.set(cacheKey, value);
|
|
31
40
|
value.catch(() => {
|
|
32
41
|
// on a reject, evict key from cache
|
|
33
|
-
cache.delete(
|
|
42
|
+
cache.delete(cacheKey);
|
|
34
43
|
});
|
|
35
44
|
}
|
|
36
45
|
return value;
|