@chriscdn/memoize 1.0.10 → 1.0.12
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/lib/index.cjs +99 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +24 -0
- package/lib/index.d.ts +24 -22
- package/lib/index.js +63 -0
- package/lib/index.js.map +1 -0
- package/package.json +26 -16
- package/__tests__/memoize.test.ts +0 -201
- package/lib/memoize.cjs +0 -2
- package/lib/memoize.cjs.map +0 -1
- package/lib/memoize.modern.js +0 -2
- package/lib/memoize.modern.js.map +0 -1
- package/lib/memoize.module.js +0 -2
- package/lib/memoize.module.js.map +0 -1
- package/src/index.ts +0 -97
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Memoize: () => Memoize,
|
|
34
|
+
MemoizeAsync: () => MemoizeAsync
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(index_exports);
|
|
37
|
+
var import_promise_semaphore = require("@chriscdn/promise-semaphore");
|
|
38
|
+
var import_quick_lru = __toESM(require("quick-lru"), 1);
|
|
39
|
+
var kDefaultMaxSize = 1e3;
|
|
40
|
+
var Memoize = (cb, options = {}) => {
|
|
41
|
+
const maxAge = options.maxAge;
|
|
42
|
+
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
43
|
+
const shouldCache = options.shouldCache ?? (() => true);
|
|
44
|
+
const resolver = options.resolver ?? ((...args) => JSON.stringify(args));
|
|
45
|
+
const cache = new import_quick_lru.default({
|
|
46
|
+
maxAge,
|
|
47
|
+
maxSize
|
|
48
|
+
});
|
|
49
|
+
const memoizedFunction = (...args) => {
|
|
50
|
+
const key = resolver(...args);
|
|
51
|
+
if (cache.has(key)) {
|
|
52
|
+
return cache.get(key);
|
|
53
|
+
} else {
|
|
54
|
+
const returnValue = cb(...args);
|
|
55
|
+
if (shouldCache(returnValue, key)) {
|
|
56
|
+
cache.set(key, returnValue);
|
|
57
|
+
}
|
|
58
|
+
return returnValue;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
memoizedFunction.cache = cache;
|
|
62
|
+
return memoizedFunction;
|
|
63
|
+
};
|
|
64
|
+
var MemoizeAsync = (cb, options = {}) => {
|
|
65
|
+
const maxAge = options.maxAge;
|
|
66
|
+
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
67
|
+
const shouldCache = options.shouldCache ?? (() => true);
|
|
68
|
+
const resolver = options.resolver ?? ((...args) => JSON.stringify(args));
|
|
69
|
+
const cache = new import_quick_lru.default({
|
|
70
|
+
maxAge,
|
|
71
|
+
maxSize
|
|
72
|
+
});
|
|
73
|
+
const semaphore = new import_promise_semaphore.Semaphore();
|
|
74
|
+
const memoizedFunction = async (...args) => {
|
|
75
|
+
const key = resolver(...args);
|
|
76
|
+
try {
|
|
77
|
+
await semaphore.acquire(key);
|
|
78
|
+
if (cache.has(key)) {
|
|
79
|
+
return cache.get(key);
|
|
80
|
+
} else {
|
|
81
|
+
const returnValue = await cb(...args);
|
|
82
|
+
if (shouldCache(returnValue, key)) {
|
|
83
|
+
cache.set(key, returnValue);
|
|
84
|
+
}
|
|
85
|
+
return returnValue;
|
|
86
|
+
}
|
|
87
|
+
} finally {
|
|
88
|
+
semaphore.release(key);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
memoizedFunction.cache = cache;
|
|
92
|
+
return memoizedFunction;
|
|
93
|
+
};
|
|
94
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
95
|
+
0 && (module.exports = {
|
|
96
|
+
Memoize,
|
|
97
|
+
MemoizeAsync
|
|
98
|
+
});
|
|
99
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Semaphore } from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const semaphore = new Semaphore();\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = await cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,+BAA0B;AAC1B,uBAAqB;AAErB,IAAM,kBAAkB;AAYxB,IAAM,UAAU,CACd,IACA,UAA0C,CAAC,MACxC;AACH,QAAM,SAA6B,Q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ACF,UAAE;AACA,gBAAU,QAAQ,GAAG;AAAA,IACvB;AAAA,EACF;AAEA,mBAAiB,QAAQ;AAEzB,SAAO;AACT;","names":["QuickLRU"]}
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import QuickLRU from 'quick-lru';
|
|
2
|
+
|
|
3
|
+
type Options<T extends any[], Return> = {
|
|
4
|
+
maxSize: number;
|
|
5
|
+
maxAge?: number;
|
|
6
|
+
shouldCache: (returnValue: Return, key: string) => boolean;
|
|
7
|
+
resolver: (...args: T) => string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Memoize a synchronous function.
|
|
11
|
+
*/
|
|
12
|
+
declare const Memoize: <Args extends unknown[], Return>(cb: (...args: Args) => Return, options?: Partial<Options<Args, Return>>) => {
|
|
13
|
+
(...args: Args): Return;
|
|
14
|
+
cache: QuickLRU<string, Return>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Memoize an asynchronous function.
|
|
18
|
+
*/
|
|
19
|
+
declare const MemoizeAsync: <Args extends unknown[], Return>(cb: (...args: Args) => Promise<Return>, options?: Partial<Options<Args, Return>>) => {
|
|
20
|
+
(...args: Args): Promise<Return>;
|
|
21
|
+
cache: QuickLRU<string, Return>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { Memoize, MemoizeAsync };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
import QuickLRU from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import QuickLRU from 'quick-lru';
|
|
2
|
+
|
|
3
|
+
type Options<T extends any[], Return> = {
|
|
4
|
+
maxSize: number;
|
|
5
|
+
maxAge?: number;
|
|
6
|
+
shouldCache: (returnValue: Return, key: string) => boolean;
|
|
7
|
+
resolver: (...args: T) => string;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Memoize a synchronous function.
|
|
11
|
+
*/
|
|
12
|
+
declare const Memoize: <Args extends unknown[], Return>(cb: (...args: Args) => Return, options?: Partial<Options<Args, Return>>) => {
|
|
13
|
+
(...args: Args): Return;
|
|
14
|
+
cache: QuickLRU<string, Return>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Memoize an asynchronous function.
|
|
18
|
+
*/
|
|
19
|
+
declare const MemoizeAsync: <Args extends unknown[], Return>(cb: (...args: Args) => Promise<Return>, options?: Partial<Options<Args, Return>>) => {
|
|
20
|
+
(...args: Args): Promise<Return>;
|
|
21
|
+
cache: QuickLRU<string, Return>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export { Memoize, MemoizeAsync };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { Semaphore } from "@chriscdn/promise-semaphore";
|
|
3
|
+
import QuickLRU from "quick-lru";
|
|
4
|
+
var kDefaultMaxSize = 1e3;
|
|
5
|
+
var Memoize = (cb, options = {}) => {
|
|
6
|
+
const maxAge = options.maxAge;
|
|
7
|
+
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
8
|
+
const shouldCache = options.shouldCache ?? (() => true);
|
|
9
|
+
const resolver = options.resolver ?? ((...args) => JSON.stringify(args));
|
|
10
|
+
const cache = new QuickLRU({
|
|
11
|
+
maxAge,
|
|
12
|
+
maxSize
|
|
13
|
+
});
|
|
14
|
+
const memoizedFunction = (...args) => {
|
|
15
|
+
const key = resolver(...args);
|
|
16
|
+
if (cache.has(key)) {
|
|
17
|
+
return cache.get(key);
|
|
18
|
+
} else {
|
|
19
|
+
const returnValue = cb(...args);
|
|
20
|
+
if (shouldCache(returnValue, key)) {
|
|
21
|
+
cache.set(key, returnValue);
|
|
22
|
+
}
|
|
23
|
+
return returnValue;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
memoizedFunction.cache = cache;
|
|
27
|
+
return memoizedFunction;
|
|
28
|
+
};
|
|
29
|
+
var MemoizeAsync = (cb, options = {}) => {
|
|
30
|
+
const maxAge = options.maxAge;
|
|
31
|
+
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
32
|
+
const shouldCache = options.shouldCache ?? (() => true);
|
|
33
|
+
const resolver = options.resolver ?? ((...args) => JSON.stringify(args));
|
|
34
|
+
const cache = new QuickLRU({
|
|
35
|
+
maxAge,
|
|
36
|
+
maxSize
|
|
37
|
+
});
|
|
38
|
+
const semaphore = new Semaphore();
|
|
39
|
+
const memoizedFunction = async (...args) => {
|
|
40
|
+
const key = resolver(...args);
|
|
41
|
+
try {
|
|
42
|
+
await semaphore.acquire(key);
|
|
43
|
+
if (cache.has(key)) {
|
|
44
|
+
return cache.get(key);
|
|
45
|
+
} else {
|
|
46
|
+
const returnValue = await cb(...args);
|
|
47
|
+
if (shouldCache(returnValue, key)) {
|
|
48
|
+
cache.set(key, returnValue);
|
|
49
|
+
}
|
|
50
|
+
return returnValue;
|
|
51
|
+
}
|
|
52
|
+
} finally {
|
|
53
|
+
semaphore.release(key);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
memoizedFunction.cache = cache;
|
|
57
|
+
return memoizedFunction;
|
|
58
|
+
};
|
|
59
|
+
export {
|
|
60
|
+
Memoize,
|
|
61
|
+
MemoizeAsync
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Semaphore } from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const semaphore = new Semaphore();\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = await cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,cA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AACjC,gBAAM,IAAI,KAAK,WAAW;AAAA,QAC5B;AACA,eAAO;AAAA,MACT;AAAA,IACF,UAAE;AACA,gBAAU,QAAQ,GAAG;AAAA,IACvB;AAAA,EACF;AAEA,mBAAiB,QAAQ;AAEzB,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,32 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/memoize",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "Memoize a synchronous or asynchronous function.",
|
|
5
5
|
"repository": "https://github.com/chriscdn/memoize",
|
|
6
6
|
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"type": "module",
|
|
9
|
-
"
|
|
9
|
+
"main": "./lib/index.cjs",
|
|
10
|
+
"module": "./lib/index.js",
|
|
11
|
+
"types": "./lib/index.d.ts",
|
|
10
12
|
"exports": {
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
".": {
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
16
|
+
"default": "./lib/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./lib/index.d.cts",
|
|
20
|
+
"default": "./lib/index.cjs"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
14
23
|
},
|
|
15
|
-
"main": "./lib/memoize.cjs",
|
|
16
|
-
"module": "./lib/memoize.module.js",
|
|
17
|
-
"__unpkg": "./lib/memoize.umd.js",
|
|
18
|
-
"types": "./lib/index.d.ts",
|
|
19
24
|
"scripts": {
|
|
20
|
-
"build": "
|
|
21
|
-
"
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"watch": "yarn build --watch",
|
|
22
27
|
"test": "vitest"
|
|
23
28
|
},
|
|
24
29
|
"dependencies": {
|
|
25
|
-
"@chriscdn/promise-semaphore": "^3.
|
|
26
|
-
"quick-lru": "^7.0
|
|
30
|
+
"@chriscdn/promise-semaphore": "^3.1.3",
|
|
31
|
+
"quick-lru": "^7.3.0"
|
|
27
32
|
},
|
|
28
33
|
"devDependencies": {
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
34
|
+
"@tsconfig/strictest": "^2.0.8",
|
|
35
|
+
"tsup": "^8.5.1",
|
|
36
|
+
"typescript": "^5.9.3",
|
|
37
|
+
"vitest": "^4.0.8"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"lib"
|
|
41
|
+
]
|
|
32
42
|
}
|
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { Memoize, MemoizeAsync } from "../src/index";
|
|
3
|
-
|
|
4
|
-
let addSyncCount = 0;
|
|
5
|
-
let addAsyncCount = 0;
|
|
6
|
-
|
|
7
|
-
const add = (x: number, y: number) => {
|
|
8
|
-
addSyncCount += 1;
|
|
9
|
-
return x + y;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const addAsync = async (x: number, y: number) => {
|
|
13
|
-
addAsyncCount += 1;
|
|
14
|
-
return x + y;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const _asyncThrowError = MemoizeAsync(async (x: number, y: number) => {
|
|
18
|
-
addAsyncCount += 1;
|
|
19
|
-
throw new Error("Boom!");
|
|
20
|
-
return x + y;
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
const asyncThrowError = MemoizeAsync(async (x: number, y: number) => {
|
|
24
|
-
try {
|
|
25
|
-
await _asyncThrowError(x, y);
|
|
26
|
-
} catch {
|
|
27
|
-
return -1;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe("Memoization", () => {
|
|
32
|
-
it("sync", async () => {
|
|
33
|
-
const addCached = Memoize(add);
|
|
34
|
-
|
|
35
|
-
expect(addCached(1, 2)).toBe(3);
|
|
36
|
-
expect(addCached(1, 2)).toBe(3);
|
|
37
|
-
expect(addCached(1, 2)).toBe(3);
|
|
38
|
-
expect(addCached(1, 2)).toBe(3);
|
|
39
|
-
expect(addCached(1, 2)).toBe(3);
|
|
40
|
-
|
|
41
|
-
// different key here
|
|
42
|
-
expect(addCached(2, 1)).toBe(3);
|
|
43
|
-
expect(addSyncCount).toBe(2);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("async", async () => {
|
|
47
|
-
const addCachedAsync = MemoizeAsync(addAsync);
|
|
48
|
-
|
|
49
|
-
await Promise.all([
|
|
50
|
-
addCachedAsync(1, 2).then((value) => expect(value).toBe(3)),
|
|
51
|
-
addCachedAsync(1, 2).then((value) => expect(value).toBe(3)),
|
|
52
|
-
addCachedAsync(1, 2).then((value) => expect(value).toBe(3)),
|
|
53
|
-
addCachedAsync(1, 2).then((value) => expect(value).toBe(3)),
|
|
54
|
-
|
|
55
|
-
// different key here
|
|
56
|
-
addCachedAsync(2, 1).then((value) => expect(value).toBe(3)),
|
|
57
|
-
]);
|
|
58
|
-
|
|
59
|
-
expect(addAsyncCount).toBe(2);
|
|
60
|
-
expect(addCachedAsync.cache.size).toBe(2);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("resolver", () => {
|
|
64
|
-
// This its the resolver function to ensure the same value is returned for
|
|
65
|
-
// the same key.
|
|
66
|
-
|
|
67
|
-
const addCachedSameKey = Memoize(add, { resolver: (x, y) => "key" });
|
|
68
|
-
|
|
69
|
-
expect(addCachedSameKey(5, 7)).toBe(12);
|
|
70
|
-
expect(addCachedSameKey(1, 1)).toBe(12);
|
|
71
|
-
expect(addCachedSameKey(5, 1)).toBe(12);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("async error", async () => {
|
|
75
|
-
expect(await asyncThrowError(4, 3)).toBe(-1);
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
describe("Memoization of Class methods", () => {
|
|
80
|
-
class AddClass {
|
|
81
|
-
count: number = 0;
|
|
82
|
-
|
|
83
|
-
constructor() {
|
|
84
|
-
this.add = Memoize(this.add.bind(this));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
add(x: number, y: number) {
|
|
88
|
-
this.count += 1;
|
|
89
|
-
return x + y;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
it("Test1", () => {
|
|
94
|
-
const obj = new AddClass();
|
|
95
|
-
|
|
96
|
-
expect(obj.count).toBe(0);
|
|
97
|
-
expect(obj.add(1, 2)).toBe(3);
|
|
98
|
-
expect(obj.count).toBe(1);
|
|
99
|
-
expect(obj.add(1, 2)).toBe(3);
|
|
100
|
-
expect(obj.count).toBe(1);
|
|
101
|
-
expect(obj.add(5, 2)).toBe(7);
|
|
102
|
-
expect(obj.count).toBe(2);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
describe("Null & Undefined Cases", () => {
|
|
107
|
-
const UndefinedFunc = Memoize((key: string) => undefined, {
|
|
108
|
-
resolver: (key) => key,
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const NullFunc = Memoize((key: string) => undefined, {
|
|
112
|
-
resolver: (key) => key,
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("Undefined", () => {
|
|
116
|
-
expect(UndefinedFunc.cache.has("hello")).toBe(false);
|
|
117
|
-
expect(UndefinedFunc("hello")).toBe(undefined);
|
|
118
|
-
expect(UndefinedFunc.cache.has("hello")).toBe(true);
|
|
119
|
-
expect(UndefinedFunc("hello")).toBe(undefined);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it("Null", () => {
|
|
123
|
-
expect(NullFunc.cache.has("hello")).toBe(false);
|
|
124
|
-
expect(NullFunc("hello")).toBe(undefined);
|
|
125
|
-
expect(NullFunc.cache.has("hello")).toBe(true);
|
|
126
|
-
expect(NullFunc("hello")).toBe(undefined);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe("Object Reference", () => {
|
|
131
|
-
const a = { hello: "world" };
|
|
132
|
-
|
|
133
|
-
const funny = Memoize(() => a);
|
|
134
|
-
|
|
135
|
-
it("Null", () => {
|
|
136
|
-
expect(funny().hello).toBe("world");
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it("Null", () => {
|
|
140
|
-
a.hello = "mars";
|
|
141
|
-
expect(funny().hello).toBe("mars");
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
describe("ShouldCache", () => {
|
|
146
|
-
const doNotCache = "do not cache";
|
|
147
|
-
|
|
148
|
-
const myFunction = Memoize((word: string) => word, {
|
|
149
|
-
shouldCache: (value) => value !== doNotCache,
|
|
150
|
-
resolver: (value) => value,
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
myFunction("hi");
|
|
154
|
-
myFunction(doNotCache);
|
|
155
|
-
|
|
156
|
-
it("should be cached", () => {
|
|
157
|
-
expect(myFunction.cache.has("hi")).toBe(true);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it("should not be cached", () => {
|
|
161
|
-
expect(myFunction.cache.has(doNotCache)).toBe(false);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
describe("Do we need Memoize?", async () => {
|
|
166
|
-
const myFunction = Memoize(async (word: string) => word, {
|
|
167
|
-
resolver: (value) => value,
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it("should be cached", async () => {
|
|
171
|
-
expect(await myFunction("hi")).toBe("hi");
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it("should be cached", async () => {
|
|
175
|
-
expect(await myFunction("hi")).toBe("hi");
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("should be cached", async () => {
|
|
179
|
-
expect(await myFunction("hi2")).toBe("hi2");
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
it("size", () => expect(myFunction.cache.size).toBe(2));
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
describe("Errors", async () => {
|
|
186
|
-
const errorSync = Memoize(() => {
|
|
187
|
-
throw new Error("errorsync");
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
const errorASync = Memoize(async () => {
|
|
191
|
-
throw new Error("errorasync");
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("error sync", () => {
|
|
195
|
-
expect(() => errorSync()).toThrowError("errorsync");
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it("error async", () => {
|
|
199
|
-
expect(errorASync()).rejects.toThrowError("errorasync");
|
|
200
|
-
});
|
|
201
|
-
});
|
package/lib/memoize.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
var e=require("@chriscdn/promise-semaphore");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=/*#__PURE__*/r(require("quick-lru"));exports.Memoize=function(e,r){var t,i,u;void 0===r&&(r={});var l=null!=(t=r.maxSize)?t:1e3,a=null!=(i=r.shouldCache)?i:function(){return!0},o=null!=(u=r.resolver)?u:function(){return JSON.stringify([].slice.call(arguments))},c=new n.default({maxAge:r.maxAge,maxSize:l}),s=function(){var r=[].slice.call(arguments),n=o.apply(void 0,r);if(c.has(n))return c.get(n);var t=e.apply(void 0,r);return a(t,n)&&c.set(n,t),t};return s.cache=c,s},exports.MemoizeAsync=function(r,t){var i,u,l;void 0===t&&(t={});var a=null!=(i=t.maxSize)?i:1e3,o=null!=(u=t.shouldCache)?u:function(){return!0},c=null!=(l=t.resolver)?l:function(){return JSON.stringify([].slice.call(arguments))},s=new n.default({maxAge:t.maxAge,maxSize:a}),f=new e.Semaphore,v=function(){try{var e=[].slice.call(arguments),n=c.apply(void 0,e);return Promise.resolve(function(t,i){try{var u=Promise.resolve(f.acquire(n)).then(function(){return s.has(n)?s.get(n):Promise.resolve(r.apply(void 0,e)).then(function(e){return o(e,n)&&s.set(n,e),e})})}catch(e){return i(!0,e)}return u&&u.then?u.then(i.bind(null,!1),i.bind(null,!0)):i(!1,u)}(0,function(e,r){if(f.release(n),e)throw r;return r}))}catch(e){return Promise.reject(e)}};return v.cache=s,v};
|
|
2
|
-
//# sourceMappingURL=memoize.cjs.map
|
package/lib/memoize.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"memoize.cjs","sources":["../src/index.ts"],"sourcesContent":["import { Semaphore } from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const semaphore = new Semaphore();\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = await cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["cb","options","_options$maxSize","_options$shouldCache","_options$resolver","maxSize","shouldCache","resolver","JSON","stringify","slice","call","arguments","cache","QuickLRU","maxAge","memoizedFunction","args","key","apply","has","get","returnValue","set","_options$maxSize2","_options$shouldCache2","_options$resolver2","semaphore","Semaphore","_arguments","Promise","resolve","acquire","then","_finallyRethrows","_wasThrown","_result","release","e","reject"],"mappings":"gLAegB,SACdA,EACAC,GACEC,IAAAA,EAAAC,EAAAC,WADFH,IAAAA,EAA0C,CAAE,GAE5C,IACMI,EAAyBH,OAAlBA,EAAGD,EAAQI,SAAOH,EAjBT,IAkBhBI,EAAiC,OAAtBH,EAAGF,EAAQK,aAAWH,EAAK,WAAM,OAAA,CAAI,EAEhDI,SAAQH,EAAGH,EAAQM,UAAQH,EAC9B,WAAmB,OAAAI,KAAKC,UAAS,GAAAC,MAAAC,KAAAC,WAAM,EAEpCC,EAAQ,IAAIC,EAAAA,QAAyB,CACzCC,OARiCd,EAAQc,OASzCV,QAAAA,IAGIW,EAAmB,WAAI,IAAAC,EAAU,GAAAP,MAAAC,KAAAC,WAC/BM,EAAMX,EAAQY,WAAIF,EAAAA,GAExB,GAAIJ,EAAMO,IAAIF,GACZ,OAAOL,EAAMQ,IAAIH,GAEjB,IAAMI,EAActB,EAAEmB,WAAIF,EAAAA,GAI1B,OAHIX,EAAYgB,EAAaJ,IAC3BL,EAAMU,IAAIL,EAAKI,GAEVA,CAEX,EAIA,OAFAN,EAAiBH,MAAQA,EAElBG,CACT,uBAKqB,SACnBhB,EACAC,GACEuB,IAAAA,EAAAC,EAAAC,OADwC,IAA1CzB,IAAAA,EAA0C,CAAA,GAE1C,IACMI,EAAyBmB,OAAlBA,EAAGvB,EAAQI,SAAOmB,EAvDT,IAwDhBlB,EAAiCmB,OAAtBA,EAAGxB,EAAQK,aAAWmB,EAAK,WAAA,OAAU,CAAA,EAEhDlB,EAA2B,OAAnBmB,EAAGzB,EAAQM,UAAQmB,EAC9B,WAAmB,OAAAlB,KAAKC,UAASC,GAAAA,MAAAC,KAAAC,WAAM,EAEpCC,EAAQ,IAAIC,EAAQ,QAAiB,CACzCC,OARiCd,EAAQc,OASzCV,QAAAA,IAGIsB,EAAY,IAAIC,EAAAA,UAEhBZ,EAAA,eAA4Da,IAA/BZ,EAAU,GAAAP,MAAAC,KAAqBC,WAC1DM,EAAMX,EAAQY,WAAIF,EAAAA,GAAM,OAAAa,QAAAC,gCAE1BD,QAAAC,QACIJ,EAAUK,QAAQd,IAAIe,KAAA,WAAA,OAExBpB,EAAMO,IAAIF,GACLL,EAAMQ,IAAIH,GAAeY,QAAAC,QAEN/B,EAAEmB,WAAA,EAAIF,IAAKgB,KAAA,SAA/BX,GAIN,OAHIhB,EAAYgB,EAAaJ,IAC3BL,EAAMU,IAAIL,EAAKI,GAEVA,CAAY,EAEtB,4FAd6BY,CAAA,EAc7BC,SAAAA,EAAAC,GACwB,GAAvBT,EAAUU,QAAQnB,GAAKiB,EAAA,MAAAC,EAAAA,OAAAA,CAAA,GAE3B,CAAC,MAAAE,GAAAR,OAAAA,QAAAS,OAAAD,EAAA,CAAA,EAID,OAFAtB,EAAiBH,MAAQA,EAElBG,CACT"}
|
package/lib/memoize.modern.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{Semaphore as e}from"@chriscdn/promise-semaphore";import r from"quick-lru";const n=(e,n={})=>{var a,t,s;const l=null!=(a=n.maxSize)?a:1e3,i=null!=(t=n.shouldCache)?t:()=>!0,c=null!=(s=n.resolver)?s:(...e)=>JSON.stringify(e),o=new r({maxAge:n.maxAge,maxSize:l}),u=(...r)=>{const n=c(...r);if(o.has(n))return o.get(n);{const a=e(...r);return i(a,n)&&o.set(n,a),a}};return u.cache=o,u},a=(n,a={})=>{var t,s,l;const i=null!=(t=a.maxSize)?t:1e3,c=null!=(s=a.shouldCache)?s:()=>!0,o=null!=(l=a.resolver)?l:(...e)=>JSON.stringify(e),u=new r({maxAge:a.maxAge,maxSize:i}),m=new e,h=async(...e)=>{const r=o(...e);try{if(await m.acquire(r),u.has(r))return u.get(r);{const a=await n(...e);return c(a,r)&&u.set(r,a),a}}finally{m.release(r)}};return h.cache=u,h};export{n as Memoize,a as MemoizeAsync};
|
|
2
|
-
//# sourceMappingURL=memoize.modern.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"memoize.modern.js","sources":["../src/index.ts"],"sourcesContent":["import { Semaphore } from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const semaphore = new Semaphore();\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = await cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["Memoize","cb","options","_options$maxSize","_options$shouldCache","_options$resolver","maxSize","shouldCache","resolver","args","JSON","stringify","cache","QuickLRU","maxAge","memoizedFunction","key","has","get","returnValue","set","MemoizeAsync","_options$maxSize2","_options$shouldCache2","_options$resolver2","semaphore","Semaphore","async","acquire","release"],"mappings":"iFAGA,MAYMA,EAAUA,CACdC,EACAC,EAA0C,MACxC,IAAAC,EAAAC,EAAAC,EACF,MACMC,SAAOH,EAAGD,EAAQI,SAAOH,EAjBT,IAkBhBI,EAAiC,OAAtBH,EAAGF,EAAQK,aAAWH,EAAK,KAAM,EAE5CI,EAA2BH,OAAnBA,EAAGH,EAAQM,UAAQH,EAC9B,IAAII,IAAeC,KAAKC,UAAUF,GAE/BG,EAAQ,IAAIC,EAAyB,CACzCC,OARiCZ,EAAQY,OASzCR,YAGIS,EAAmBA,IAAIN,KAC3B,MAAMO,EAAMR,KAAYC,GAExB,GAAIG,EAAMK,IAAID,GACZ,OAAOJ,EAAMM,IAAIF,GACZ,CACL,MAAMG,EAAclB,KAAMQ,GAI1B,OAHIF,EAAYY,EAAaH,IAC3BJ,EAAMQ,IAAIJ,EAAKG,GAEVA,CACR,GAKH,OAFAJ,EAAiBH,MAAQA,EAElBG,GAMHM,EAAeA,CACnBpB,EACAC,EAA0C,CAAE,SAC1CoB,EAAAC,EAAAC,EACF,MACMlB,EAAyB,OAAlBgB,EAAGpB,EAAQI,SAAOgB,EAvDT,IAwDhBf,SAAWgB,EAAGrB,EAAQK,aAAWgB,EAAK,KAAM,EAE5Cf,EAA2B,OAAnBgB,EAAGtB,EAAQM,UAAQgB,EAC9B,IAAIf,IAAeC,KAAKC,UAAUF,GAE/BG,EAAQ,IAAIC,EAAyB,CACzCC,OARiCZ,EAAQY,OASzCR,YAGImB,EAAY,IAAIC,EAEhBX,EAAmBY,SAAUlB,KACjC,MAAMO,EAAMR,KAAYC,GAExB,IAGE,SAFMgB,EAAUG,QAAQZ,GAEpBJ,EAAMK,IAAID,GACZ,OAAOJ,EAAMM,IAAIF,GACZ,CACL,MAAMG,QAAoBlB,KAAMQ,GAIhC,OAHIF,EAAYY,EAAaH,IAC3BJ,EAAMQ,IAAIJ,EAAKG,GAEVA,CACR,CACF,CAAA,QACCM,EAAUI,QAAQb,EACnB,GAKH,OAFAD,EAAiBH,MAAQA,EAElBG"}
|
package/lib/memoize.module.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{Semaphore as e}from"@chriscdn/promise-semaphore";import r from"quick-lru";var n=function(e,n){var t,i,l;void 0===n&&(n={});var u=null!=(t=n.maxSize)?t:1e3,o=null!=(i=n.shouldCache)?i:function(){return!0},a=null!=(l=n.resolver)?l:function(){return JSON.stringify([].slice.call(arguments))},c=new r({maxAge:n.maxAge,maxSize:u}),s=function(){var r=[].slice.call(arguments),n=a.apply(void 0,r);if(c.has(n))return c.get(n);var t=e.apply(void 0,r);return o(t,n)&&c.set(n,t),t};return s.cache=c,s},t=function(n,t){var i,l,u;void 0===t&&(t={});var o=null!=(i=t.maxSize)?i:1e3,a=null!=(l=t.shouldCache)?l:function(){return!0},c=null!=(u=t.resolver)?u:function(){return JSON.stringify([].slice.call(arguments))},s=new r({maxAge:t.maxAge,maxSize:o}),v=new e,f=function(){try{var e=[].slice.call(arguments),r=c.apply(void 0,e);return Promise.resolve(function(t,i){try{var l=Promise.resolve(v.acquire(r)).then(function(){return s.has(r)?s.get(r):Promise.resolve(n.apply(void 0,e)).then(function(e){return a(e,r)&&s.set(r,e),e})})}catch(e){return i(!0,e)}return l&&l.then?l.then(i.bind(null,!1),i.bind(null,!0)):i(!1,l)}(0,function(e,n){if(v.release(r),e)throw n;return n}))}catch(e){return Promise.reject(e)}};return f.cache=s,f};export{n as Memoize,t as MemoizeAsync};
|
|
2
|
-
//# sourceMappingURL=memoize.module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"memoize.module.js","sources":["../src/index.ts"],"sourcesContent":["import { Semaphore } from \"@chriscdn/promise-semaphore\";\nimport QuickLRU from \"quick-lru\";\n\nconst kDefaultMaxSize = 1000;\n\ntype Options<T extends any[], Return> = {\n maxSize: number;\n maxAge?: number;\n shouldCache: (returnValue: Return, key: string) => boolean;\n resolver: (...args: T) => string;\n};\n\n/**\n * Memoize a synchronous function.\n */\nconst Memoize = <Args extends unknown[], Return>(\n cb: (...args: Args) => Return,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const memoizedFunction = (...args: Args): Return => {\n const key = resolver(...args);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\n/**\n * Memoize an asynchronous function.\n */\nconst MemoizeAsync = <Args extends unknown[], Return>(\n cb: (...args: Args) => Promise<Return>,\n options: Partial<Options<Args, Return>> = {},\n) => {\n const maxAge: number | undefined = options.maxAge;\n const maxSize = options.maxSize ?? kDefaultMaxSize;\n const shouldCache = options.shouldCache ?? (() => true);\n\n const resolver = options.resolver ??\n ((...args: Args) => JSON.stringify(args));\n\n const cache = new QuickLRU<string, Return>({\n maxAge,\n maxSize,\n });\n\n const semaphore = new Semaphore();\n\n const memoizedFunction = async (...args: Args): Promise<Return> => {\n const key = resolver(...args);\n\n try {\n await semaphore.acquire(key);\n\n if (cache.has(key)) {\n return cache.get(key) as Return;\n } else {\n const returnValue = await cb(...args);\n if (shouldCache(returnValue, key)) {\n cache.set(key, returnValue);\n }\n return returnValue;\n }\n } finally {\n semaphore.release(key);\n }\n };\n\n memoizedFunction.cache = cache;\n\n return memoizedFunction;\n};\n\nexport { Memoize, MemoizeAsync };\n"],"names":["Memoize","cb","options","_options$maxSize","_options$shouldCache","_options$resolver","maxSize","shouldCache","resolver","JSON","stringify","slice","call","arguments","cache","QuickLRU","maxAge","memoizedFunction","args","key","apply","has","get","returnValue","set","MemoizeAsync","_options$maxSize2","_options$shouldCache2","_options$resolver2","semaphore","Semaphore","_arguments","Promise","resolve","acquire","then","_finallyRethrows","_wasThrown","_result","release","e","reject"],"mappings":"iFAGA,IAYMA,EAAU,SACdC,EACAC,GACEC,IAAAA,EAAAC,EAAAC,WADFH,IAAAA,EAA0C,CAAE,GAE5C,IACMI,EAAyBH,OAAlBA,EAAGD,EAAQI,SAAOH,EAjBT,IAkBhBI,EAAiC,OAAtBH,EAAGF,EAAQK,aAAWH,EAAK,WAAM,OAAA,CAAI,EAEhDI,SAAQH,EAAGH,EAAQM,UAAQH,EAC9B,WAAmB,OAAAI,KAAKC,UAAS,GAAAC,MAAAC,KAAAC,WAAM,EAEpCC,EAAQ,IAAIC,EAAyB,CACzCC,OARiCd,EAAQc,OASzCV,QAAAA,IAGIW,EAAmB,WAAI,IAAAC,EAAU,GAAAP,MAAAC,KA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,CAAA,EAID,OAFAvB,EAAiBH,MAAQA,EAElBG,CACT"}
|
package/src/index.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { Semaphore } from "@chriscdn/promise-semaphore";
|
|
2
|
-
import QuickLRU from "quick-lru";
|
|
3
|
-
|
|
4
|
-
const kDefaultMaxSize = 1000;
|
|
5
|
-
|
|
6
|
-
type Options<T extends any[], Return> = {
|
|
7
|
-
maxSize: number;
|
|
8
|
-
maxAge?: number;
|
|
9
|
-
shouldCache: (returnValue: Return, key: string) => boolean;
|
|
10
|
-
resolver: (...args: T) => string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Memoize a synchronous function.
|
|
15
|
-
*/
|
|
16
|
-
const Memoize = <Args extends unknown[], Return>(
|
|
17
|
-
cb: (...args: Args) => Return,
|
|
18
|
-
options: Partial<Options<Args, Return>> = {},
|
|
19
|
-
) => {
|
|
20
|
-
const maxAge: number | undefined = options.maxAge;
|
|
21
|
-
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
22
|
-
const shouldCache = options.shouldCache ?? (() => true);
|
|
23
|
-
|
|
24
|
-
const resolver = options.resolver ??
|
|
25
|
-
((...args: Args) => JSON.stringify(args));
|
|
26
|
-
|
|
27
|
-
const cache = new QuickLRU<string, Return>({
|
|
28
|
-
maxAge,
|
|
29
|
-
maxSize,
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
const memoizedFunction = (...args: Args): Return => {
|
|
33
|
-
const key = resolver(...args);
|
|
34
|
-
|
|
35
|
-
if (cache.has(key)) {
|
|
36
|
-
return cache.get(key) as Return;
|
|
37
|
-
} else {
|
|
38
|
-
const returnValue = cb(...args);
|
|
39
|
-
if (shouldCache(returnValue, key)) {
|
|
40
|
-
cache.set(key, returnValue);
|
|
41
|
-
}
|
|
42
|
-
return returnValue;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
memoizedFunction.cache = cache;
|
|
47
|
-
|
|
48
|
-
return memoizedFunction;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Memoize an asynchronous function.
|
|
53
|
-
*/
|
|
54
|
-
const MemoizeAsync = <Args extends unknown[], Return>(
|
|
55
|
-
cb: (...args: Args) => Promise<Return>,
|
|
56
|
-
options: Partial<Options<Args, Return>> = {},
|
|
57
|
-
) => {
|
|
58
|
-
const maxAge: number | undefined = options.maxAge;
|
|
59
|
-
const maxSize = options.maxSize ?? kDefaultMaxSize;
|
|
60
|
-
const shouldCache = options.shouldCache ?? (() => true);
|
|
61
|
-
|
|
62
|
-
const resolver = options.resolver ??
|
|
63
|
-
((...args: Args) => JSON.stringify(args));
|
|
64
|
-
|
|
65
|
-
const cache = new QuickLRU<string, Return>({
|
|
66
|
-
maxAge,
|
|
67
|
-
maxSize,
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
const semaphore = new Semaphore();
|
|
71
|
-
|
|
72
|
-
const memoizedFunction = async (...args: Args): Promise<Return> => {
|
|
73
|
-
const key = resolver(...args);
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
await semaphore.acquire(key);
|
|
77
|
-
|
|
78
|
-
if (cache.has(key)) {
|
|
79
|
-
return cache.get(key) as Return;
|
|
80
|
-
} else {
|
|
81
|
-
const returnValue = await cb(...args);
|
|
82
|
-
if (shouldCache(returnValue, key)) {
|
|
83
|
-
cache.set(key, returnValue);
|
|
84
|
-
}
|
|
85
|
-
return returnValue;
|
|
86
|
-
}
|
|
87
|
-
} finally {
|
|
88
|
-
semaphore.release(key);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
memoizedFunction.cache = cache;
|
|
93
|
-
|
|
94
|
-
return memoizedFunction;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export { Memoize, MemoizeAsync };
|