@common.js/p-memoize 7.1.0 → 7.1.1
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 +1 -1
- package/dist/index.d.ts +4 -4
- package/package.json +10 -9
- package/readme.md +0 -154
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
The [p-memoize](https://www.npmjs.com/package/p-memoize) package exported as CommonJS modules.
|
|
4
4
|
|
|
5
|
-
Exported from [p-memoize@7.1.
|
|
5
|
+
Exported from [p-memoize@7.1.1](https://www.npmjs.com/package/p-memoize/v/7.1.1) using https://github.com/etienne-martin/common.js.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import type { AsyncReturnType } from 'type-fest';
|
|
2
2
|
export declare type AnyAsyncFunction = (...arguments_: readonly any[]) => Promise<unknown | void>;
|
|
3
|
-
export
|
|
3
|
+
export declare type CacheStorage<KeyType, ValueType> = {
|
|
4
4
|
has: (key: KeyType) => Promise<boolean> | boolean;
|
|
5
5
|
get: (key: KeyType) => Promise<ValueType | undefined> | ValueType | undefined;
|
|
6
6
|
set: (key: KeyType, value: ValueType) => Promise<unknown> | unknown;
|
|
7
7
|
delete: (key: KeyType) => unknown;
|
|
8
8
|
clear?: () => unknown;
|
|
9
|
-
}
|
|
10
|
-
export
|
|
9
|
+
};
|
|
10
|
+
export declare type Options<FunctionToMemoize extends AnyAsyncFunction, CacheKeyType> = {
|
|
11
11
|
/**
|
|
12
12
|
Determines the cache key for storing the result based on the function arguments. By default, __only the first argument is considered__ and it only works with [primitives](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
13
13
|
|
|
@@ -41,7 +41,7 @@ export interface Options<FunctionToMemoize extends AnyAsyncFunction, CacheKeyTyp
|
|
|
41
41
|
@example new WeakMap()
|
|
42
42
|
*/
|
|
43
43
|
readonly cache?: CacheStorage<CacheKeyType, AsyncReturnType<FunctionToMemoize>> | false;
|
|
44
|
-
}
|
|
44
|
+
};
|
|
45
45
|
/**
|
|
46
46
|
[Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input.
|
|
47
47
|
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@common.js/p-memoize",
|
|
3
|
-
"version": "7.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "7.1.1",
|
|
4
|
+
"description": "p-memoize package exported as CommonJS modules",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "etienne-martin/common.js",
|
|
7
7
|
"funding": "https://github.com/sindresorhus/p-memoize?sponsor=1",
|
|
8
8
|
"type": "commonjs",
|
|
9
|
-
"types": "dist/index.d.ts",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
10
|
"engines": {
|
|
11
11
|
"node": ">=14.16"
|
|
12
12
|
},
|
|
@@ -18,20 +18,21 @@
|
|
|
18
18
|
"dist"
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@common.js/mimic-fn": "^4.0.0"
|
|
21
|
+
"@common.js/mimic-fn": "^4.0.0",
|
|
22
|
+
"type-fest": "^3.0.0"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
|
24
25
|
"@sindresorhus/tsconfig": "^3.0.1",
|
|
25
26
|
"@types/serialize-javascript": "^5.0.2",
|
|
26
|
-
"ava": "^4.3.
|
|
27
|
-
"del-cli": "^
|
|
27
|
+
"ava": "^4.3.3",
|
|
28
|
+
"del-cli": "^5.0.0",
|
|
28
29
|
"delay": "^5.0.0",
|
|
29
30
|
"p-defer": "^4.0.0",
|
|
30
31
|
"p-state": "^1.0.0",
|
|
31
32
|
"serialize-javascript": "^6.0.0",
|
|
32
|
-
"ts-node": "^10.
|
|
33
|
-
"tsd": "^0.
|
|
34
|
-
"xo": "^0.
|
|
33
|
+
"ts-node": "^10.9.1",
|
|
34
|
+
"tsd": "^0.24.1",
|
|
35
|
+
"xo": "^0.52.4"
|
|
35
36
|
},
|
|
36
37
|
"ava": {
|
|
37
38
|
"extensions": {
|
package/readme.md
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
# p-memoize
|
|
2
|
-
|
|
3
|
-
> [Memoize](https://en.wikipedia.org/wiki/Memoization) promise-returning & async functions
|
|
4
|
-
|
|
5
|
-
Useful for speeding up consecutive function calls by caching the result of calls with identical input.
|
|
6
|
-
|
|
7
|
-
<!-- Please keep this section in sync with https://github.com/sindresorhus/mem/blob/main/readme.md -->
|
|
8
|
-
|
|
9
|
-
By default, **only the memoized function's first argument is considered** via strict equality comparison. If you need to cache multiple arguments or cache `object`s *by value*, have a look at alternative [caching strategies](#caching-strategy) below.
|
|
10
|
-
|
|
11
|
-
This package is similar to [mem](https://github.com/sindresorhus/mem) but with async-specific enhancements; in particular, it allows for asynchronous caches and does not cache rejected promises.
|
|
12
|
-
|
|
13
|
-
## Install
|
|
14
|
-
|
|
15
|
-
```sh
|
|
16
|
-
npm install p-memoize
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Usage
|
|
20
|
-
|
|
21
|
-
```js
|
|
22
|
-
import pMemoize from 'p-memoize';
|
|
23
|
-
import got from 'got';
|
|
24
|
-
|
|
25
|
-
const memoizedGot = pMemoize(got);
|
|
26
|
-
|
|
27
|
-
await memoizedGot('https://sindresorhus.com');
|
|
28
|
-
|
|
29
|
-
// This call is cached
|
|
30
|
-
await memoizedGot('https://sindresorhus.com');
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### Caching strategy
|
|
34
|
-
|
|
35
|
-
Similar to the [caching strategy for `mem`](https://github.com/sindresorhus/mem#options) with the following exceptions:
|
|
36
|
-
|
|
37
|
-
- Promises returned from a memoized function are locally cached until resolving, when their value is added to `cache`. Special properties assigned to a returned promise will not be kept after resolution and every promise may need to resolve with a serializable object if caching results in a database.
|
|
38
|
-
- `.get()`, `.has()` and `.set()` methods on `cache` can run asynchronously by returning a promise.
|
|
39
|
-
- Instead of `.set()` being provided an object with the properties `value` and `maxAge`, it will only be provided `value` as the first argument. If you want to implement time-based expiry, consider [doing so in `cache`](#time-based-cache-expiration).
|
|
40
|
-
|
|
41
|
-
## API
|
|
42
|
-
|
|
43
|
-
### pMemoize(fn, options?)
|
|
44
|
-
|
|
45
|
-
Returns a memoized version of the given function.
|
|
46
|
-
|
|
47
|
-
#### fn
|
|
48
|
-
|
|
49
|
-
Type: `Function`
|
|
50
|
-
|
|
51
|
-
Promise-returning or async function to be memoized.
|
|
52
|
-
|
|
53
|
-
#### options
|
|
54
|
-
|
|
55
|
-
Type: `object`
|
|
56
|
-
|
|
57
|
-
See the [`mem` options](https://github.com/sindresorhus/mem#options) in addition to the below option.
|
|
58
|
-
|
|
59
|
-
##### cacheKey
|
|
60
|
-
|
|
61
|
-
Type: `Function`\
|
|
62
|
-
Default: `arguments_ => arguments_[0]`\
|
|
63
|
-
Example: `arguments_ => JSON.stringify(arguments_)`
|
|
64
|
-
|
|
65
|
-
Determines the cache key for storing the result based on the function arguments. By default, **only the first argument is considered**.
|
|
66
|
-
|
|
67
|
-
A `cacheKey` function can return any type supported by `Map` (or whatever structure you use in the `cache` option).
|
|
68
|
-
|
|
69
|
-
See the [caching strategy](#caching-strategy) section for more information.
|
|
70
|
-
|
|
71
|
-
##### cache
|
|
72
|
-
|
|
73
|
-
Type: `object | false`\
|
|
74
|
-
Default: `new Map()`
|
|
75
|
-
|
|
76
|
-
Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, `.delete(key)`, and optionally `.clear()`. You could for example use a `WeakMap` instead or [`quick-lru`](https://github.com/sindresorhus/quick-lru) for a LRU cache. To disable caching so that only concurrent executions resolve with the same value, pass `false`.
|
|
77
|
-
|
|
78
|
-
See the [caching strategy](https://github.com/sindresorhus/mem#caching-strategy) section in the `mem` package for more information.
|
|
79
|
-
|
|
80
|
-
### pMemoizeDecorator(options)
|
|
81
|
-
|
|
82
|
-
Returns a [decorator](https://github.com/tc39/proposal-decorators) to memoize class methods or static class methods.
|
|
83
|
-
|
|
84
|
-
Notes:
|
|
85
|
-
|
|
86
|
-
- Only class methods and getters/setters can be memoized, not regular functions (they aren't part of the proposal);
|
|
87
|
-
- Only [TypeScript’s decorators](https://www.typescriptlang.org/docs/handbook/decorators.html#parameter-decorators) are supported, not [Babel’s](https://babeljs.io/docs/en/babel-plugin-proposal-decorators), which use a different version of the proposal;
|
|
88
|
-
- Being an experimental feature, they need to be enabled with `--experimentalDecorators`; follow TypeScript’s docs.
|
|
89
|
-
|
|
90
|
-
#### options
|
|
91
|
-
|
|
92
|
-
Type: `object`
|
|
93
|
-
|
|
94
|
-
Same as options for `pMemoize()`.
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
import {pMemoizeDecorator} from 'p-memoize';
|
|
98
|
-
|
|
99
|
-
class Example {
|
|
100
|
-
index = 0
|
|
101
|
-
|
|
102
|
-
@pMemoizeDecorator()
|
|
103
|
-
async counter() {
|
|
104
|
-
return ++this.index;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
class ExampleWithOptions {
|
|
109
|
-
index = 0
|
|
110
|
-
|
|
111
|
-
@pMemoizeDecorator()
|
|
112
|
-
async counter() {
|
|
113
|
-
return ++this.index;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
### pMemoizeClear(memoized)
|
|
119
|
-
|
|
120
|
-
Clear all cached data of a memoized function.
|
|
121
|
-
|
|
122
|
-
It will throw when given a non-memoized function.
|
|
123
|
-
|
|
124
|
-
## Tips
|
|
125
|
-
|
|
126
|
-
### Time-based cache expiration
|
|
127
|
-
|
|
128
|
-
```js
|
|
129
|
-
import pMemoize from 'p-memoize';
|
|
130
|
-
import ExpiryMap from 'expiry-map';
|
|
131
|
-
import got from 'got';
|
|
132
|
-
|
|
133
|
-
const cache = new ExpiryMap(10000); // Cached values expire after 10 seconds
|
|
134
|
-
|
|
135
|
-
const memoizedGot = pMemoize(got, {cache});
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### Caching promise rejections
|
|
139
|
-
|
|
140
|
-
```js
|
|
141
|
-
import pMemoize from 'p-memoize';
|
|
142
|
-
import pReflect from 'p-reflect';
|
|
143
|
-
|
|
144
|
-
const memoizedGot = pMemoize(async (url, options) => pReflect(got(url, options)));
|
|
145
|
-
|
|
146
|
-
await memoizedGot('https://example.com');
|
|
147
|
-
// {isFulfilled: true, isRejected: false, value: '...'}
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
## Related
|
|
151
|
-
|
|
152
|
-
- [p-debounce](https://github.com/sindresorhus/p-debounce) - Debounce promise-returning & async functions
|
|
153
|
-
- [p-throttle](https://github.com/sindresorhus/p-throttle) - Throttle promise-returning & async functions
|
|
154
|
-
- [More…](https://github.com/sindresorhus/promise-fun)
|