@atproto-labs/simple-store 0.2.0 → 0.3.0
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/CHANGELOG.md +8 -0
- package/LICENSE.txt +1 -1
- package/dist/cached-getter.d.ts +15 -13
- package/dist/cached-getter.d.ts.map +1 -1
- package/dist/cached-getter.js +21 -14
- package/dist/cached-getter.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/simple-store.d.ts +3 -3
- package/dist/simple-store.d.ts.map +1 -1
- package/dist/util.d.ts +7 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +3 -0
- package/dist/util.js.map +1 -0
- package/package.json +1 -1
- package/src/cached-getter.ts +47 -30
- package/src/index.ts +1 -0
- package/src/simple-store.ts +3 -3
- package/src/util.ts +5 -0
- package/tsconfig.build.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @atproto-labs/simple-store
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#4108](https://github.com/bluesky-social/atproto/pull/4108) [`f9dc9aa4c`](https://github.com/bluesky-social/atproto/commit/f9dc9aa4c9eaf2f82d140fbf011a9015e7f1a00d) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Allow `Key` type to be anything (except nullable values)
|
|
8
|
+
|
|
9
|
+
- [#4108](https://github.com/bluesky-social/atproto/pull/4108) [`f9dc9aa4c`](https://github.com/bluesky-social/atproto/commit/f9dc9aa4c9eaf2f82d140fbf011a9015e7f1a00d) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Update `Getter` function's option argument to `GetterOptions`
|
|
10
|
+
|
|
3
11
|
## 0.2.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
package/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Dual MIT/Apache-2.0 License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022-2025 Bluesky PBC, and Contributors
|
|
3
|
+
Copyright (c) 2022-2025 Bluesky Social PBC, and Contributors
|
|
4
4
|
|
|
5
5
|
Except as otherwise noted in individual files, this software is licensed under the MIT license (<http://opensource.org/licenses/MIT>), or the Apache License, Version 2.0 (<http://www.apache.org/licenses/LICENSE-2.0>).
|
|
6
6
|
|
package/dist/cached-getter.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export type
|
|
1
|
+
import { GetOptions, Key, SimpleStore, Value } from './simple-store.js';
|
|
2
|
+
import { Awaitable, ContextOptions } from './util.js';
|
|
3
|
+
export type { GetOptions };
|
|
4
|
+
export type GetCachedOptions<C = void> = ContextOptions<C> & {
|
|
4
5
|
signal?: AbortSignal;
|
|
5
6
|
/**
|
|
6
7
|
* Do not use the cache to get the value. Always get a new value from the
|
|
@@ -19,12 +20,13 @@ export type GetCachedOptions<C = void> = {
|
|
|
19
20
|
* @default false // If no isStale option was provided to the CachedGetter
|
|
20
21
|
*/
|
|
21
22
|
allowStale?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
};
|
|
24
|
+
export type GetterOptions<C = void> = {
|
|
25
|
+
context: C extends void ? undefined : C;
|
|
26
|
+
noCache: boolean;
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
};
|
|
29
|
+
export type Getter<K extends Key, V extends Value, C = void> = (key: K, options: GetterOptions<C>, storedValue: undefined | V) => Awaitable<V>;
|
|
28
30
|
export type CachedGetterOptions<K extends Key, V extends Value> = {
|
|
29
31
|
isStale?: (key: K, value: V) => boolean | PromiseLike<boolean>;
|
|
30
32
|
onStoreError?: (err: unknown, key: K, value: V) => void | PromiseLike<void>;
|
|
@@ -37,12 +39,12 @@ export type CachedGetterOptions<K extends Key, V extends Value> = {
|
|
|
37
39
|
export declare class CachedGetter<K extends Key = string, V extends Value = Value, C = void> {
|
|
38
40
|
readonly getter: Getter<K, V, C>;
|
|
39
41
|
readonly store: SimpleStore<K, V>;
|
|
40
|
-
readonly options
|
|
41
|
-
private pending;
|
|
42
|
-
constructor(getter: Getter<K, V, C>, store: SimpleStore<K, V>, options?: CachedGetterOptions<K, V>
|
|
42
|
+
readonly options: CachedGetterOptions<K, V>;
|
|
43
|
+
private readonly pending;
|
|
44
|
+
constructor(getter: Getter<K, V, C>, store: SimpleStore<K, V>, options?: CachedGetterOptions<K, V>);
|
|
43
45
|
get(key: C extends void ? K : never, options?: GetCachedOptions<C>): Promise<V>;
|
|
44
46
|
get(key: C extends void ? never : K, options: GetCachedOptions<C>): Promise<V>;
|
|
45
|
-
getStored(key: K, options?:
|
|
47
|
+
getStored(key: K, options?: GetOptions): Promise<V | undefined>;
|
|
46
48
|
setStored(key: K, value: V): Promise<void>;
|
|
47
49
|
delStored(key: K, _cause?: unknown): Promise<void>;
|
|
48
50
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cached-getter.d.ts","sourceRoot":"","sources":["../src/cached-getter.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"cached-getter.d.ts","sourceRoot":"","sources":["../src/cached-getter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AACvE,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAErD,YAAY,EAAE,UAAU,EAAE,CAAA;AAC1B,MAAM,MAAM,gBAAgB,CAAC,CAAC,GAAG,IAAI,IAAI,cAAc,CAAC,CAAC,CAAC,GAAG;IAC3D,MAAM,CAAC,EAAE,WAAW,CAAA;IAEpB;;;;;OAKG;IACH,OAAO,CAAC,EAA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ChC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC;IAL7C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA+B;gBAG5C,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EACvB,KAAK,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EACxB,OAAO,GAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAM;IAG5C,GAAG,CACP,GAAG,EAAE,CAAC,SAAS,IAAI,GAAG,CAAC,GAAG,KAAK,EAC/B,OAAO,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAC5B,OAAO,CAAC,CAAC,CAAC;IACP,GAAG,CACP,GAAG,EAAE,CAAC,SAAS,IAAI,GAAG,KAAK,GAAG,CAAC,EAC/B,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAC3B,OAAO,CAAC,CAAC,CAAC;IAsGP,SAAS,CAAC,GAAG,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAQ/D,SAAS,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAS1C,SAAS,CAAC,GAAG,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAGzD"}
|
package/dist/cached-getter.js
CHANGED
|
@@ -8,7 +8,7 @@ const returnFalse = () => false;
|
|
|
8
8
|
* (expensive) getter function.
|
|
9
9
|
*/
|
|
10
10
|
class CachedGetter {
|
|
11
|
-
constructor(getter, store, options) {
|
|
11
|
+
constructor(getter, store, options = {}) {
|
|
12
12
|
Object.defineProperty(this, "getter", {
|
|
13
13
|
enumerable: true,
|
|
14
14
|
configurable: true,
|
|
@@ -34,26 +34,30 @@ class CachedGetter {
|
|
|
34
34
|
value: new Map()
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
-
async get(key,
|
|
38
|
-
|
|
39
|
-
const isStale = this.options
|
|
40
|
-
const allowStored =
|
|
37
|
+
async get(key, { signal, context, allowStale = false, noCache = false, } = {}) {
|
|
38
|
+
signal?.throwIfAborted();
|
|
39
|
+
const { isStale, deleteOnError } = this.options;
|
|
40
|
+
const allowStored = noCache
|
|
41
41
|
? returnFalse // Never allow stored values to be returned
|
|
42
|
-
:
|
|
42
|
+
: allowStale || isStale == null
|
|
43
43
|
? returnTrue // Always allow stored values to be returned
|
|
44
44
|
: async (value) => !(await isStale(key, value));
|
|
45
45
|
// As long as concurrent requests are made for the same key, only one
|
|
46
|
-
// request will be made to the
|
|
47
|
-
// because there is no async operation between the while() loop and
|
|
48
|
-
// pending.set() call. Because of the
|
|
46
|
+
// request will be made to the getStored & getter functions at a time. This
|
|
47
|
+
// works because there is no async operation between the while() loop and
|
|
48
|
+
// the pending.set() call below. Because of the single threaded nature of
|
|
49
49
|
// JavaScript, the pending item will be set before the next iteration of the
|
|
50
|
-
// while loop.
|
|
50
|
+
// while loop of any concurrent request.
|
|
51
51
|
let previousExecutionFlow;
|
|
52
52
|
while ((previousExecutionFlow = this.pending.get(key))) {
|
|
53
53
|
try {
|
|
54
|
+
// If a concurrent request is already in progress, wait for it to finish
|
|
54
55
|
const { isFresh, value } = await previousExecutionFlow;
|
|
56
|
+
// Use the concurrent request's result if it is fresh
|
|
55
57
|
if (isFresh)
|
|
56
58
|
return value;
|
|
59
|
+
// Use the concurrent request's result if not fresh (loaded from the
|
|
60
|
+
// store), and matches the conditions for using a stored value.
|
|
57
61
|
if (await allowStored(value))
|
|
58
62
|
return value;
|
|
59
63
|
}
|
|
@@ -61,11 +65,12 @@ class CachedGetter {
|
|
|
61
65
|
// Ignore errors from previous execution flows (they will have been
|
|
62
66
|
// propagated by that flow).
|
|
63
67
|
}
|
|
64
|
-
|
|
68
|
+
// Break the loop if the signal was aborted
|
|
69
|
+
signal?.throwIfAborted();
|
|
65
70
|
}
|
|
66
71
|
const currentExecutionFlow = Promise.resolve()
|
|
67
72
|
.then(async () => {
|
|
68
|
-
const storedValue = await this.getStored(key,
|
|
73
|
+
const storedValue = await this.getStored(key, { signal });
|
|
69
74
|
if (storedValue !== undefined && (await allowStored(storedValue))) {
|
|
70
75
|
// Use the stored value as return value for the current execution
|
|
71
76
|
// flow. Notify other concurrent execution flows (that should be
|
|
@@ -74,11 +79,13 @@ class CachedGetter {
|
|
|
74
79
|
return { isFresh: false, value: storedValue };
|
|
75
80
|
}
|
|
76
81
|
return Promise.resolve()
|
|
77
|
-
.then(async () =>
|
|
82
|
+
.then(async () => {
|
|
83
|
+
const options = { signal, noCache, context };
|
|
84
|
+
return this.getter.call(null, key, options, storedValue);
|
|
85
|
+
})
|
|
78
86
|
.catch(async (err) => {
|
|
79
87
|
if (storedValue !== undefined) {
|
|
80
88
|
try {
|
|
81
|
-
const deleteOnError = this.options?.deleteOnError;
|
|
82
89
|
if (await deleteOnError?.(err, key, storedValue)) {
|
|
83
90
|
await this.delStored(key, err);
|
|
84
91
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cached-getter.js","sourceRoot":"","sources":["../src/cached-getter.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"cached-getter.js","sourceRoot":"","sources":["../src/cached-getter.ts"],"names":[],"mappings":";;;AAmDA,MAAM,UAAU,GAAG,GAAG,EAAE,CAAC,IAAI,CAAA;AAC7B,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,KAAK,CAAA;AAE/B;;;GAGG;AACH,MAAa,YAAY;IAOvB,YACW,MAAuB,EACvB,KAAwB,EACxB,UAAqC,EAAE;QAFhD;;;;mBAAS,MAAM;WAAiB;QAChC;;;;mBAAS,KAAK;WAAmB;QACjC;;;;mBAAS,OAAO;WAAgC;QALjC;;;;mBAAU,IAAI,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KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC3B,CAAC;CACF;AA9ID,oCA8IC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,WAAW,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./cached-getter.js"), exports);
|
|
18
18
|
__exportStar(require("./simple-store.js"), exports);
|
|
19
|
+
__exportStar(require("./util.js"), exports);
|
|
19
20
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,oDAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAkC;AAClC,oDAAiC;AACjC,4CAAyB"}
|
package/dist/simple-store.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
export type Key =
|
|
1
|
+
import { Awaitable } from './util.js';
|
|
2
|
+
export type Key = NonNullable<unknown>;
|
|
3
3
|
export type Value = NonNullable<unknown> | null;
|
|
4
4
|
export type GetOptions = {
|
|
5
5
|
signal?: AbortSignal;
|
|
6
6
|
};
|
|
7
|
-
export interface SimpleStore<K extends Key
|
|
7
|
+
export interface SimpleStore<K extends Key, V extends Value> {
|
|
8
8
|
/**
|
|
9
9
|
* @return undefined if the key is not in the store (which is why Value cannot contain "undefined").
|
|
10
10
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"simple-store.d.ts","sourceRoot":"","sources":["../src/simple-store.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"simple-store.d.ts","sourceRoot":"","sources":["../src/simple-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAA;AAErC,MAAM,MAAM,GAAG,GAAG,WAAW,CAAC,OAAO,CAAC,CAAA;AACtC,MAAM,MAAM,KAAK,GAAG,WAAW,CAAC,OAAO,CAAC,GAAG,IAAI,CAAA;AAE/C,MAAM,MAAM,UAAU,GAAG;IAAE,MAAM,CAAC,EAAE,WAAW,CAAA;CAAE,CAAA;AAEjD,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,KAAK;IACzD;;OAEG;IACH,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,UAAU,KAAK,SAAS,CAAC,SAAS,GAAG,CAAC,CAAC,CAAA;IAC/D,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,KAAK,SAAS,CAAC,IAAI,CAAC,CAAA;IAC1C,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,CAAC,IAAI,CAAC,CAAA;IAChC,KAAK,CAAC,EAAE,MAAM,SAAS,CAAC,IAAI,CAAC,CAAA;CAC9B"}
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;AAE7C,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,GAAG,SAAS,GACtD;IAAE,OAAO,CAAC,EAAE,SAAS,CAAA;CAAE,GACvB;IAAE,OAAO,EAAE,CAAC,CAAA;CAAE,CAAA"}
|
package/dist/util.js
ADDED
package/dist/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
package/src/cached-getter.ts
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Value,
|
|
7
|
-
} from './simple-store.js'
|
|
8
|
-
|
|
9
|
-
export type { GetStoredOptions }
|
|
10
|
-
export type GetCachedOptions<C = void> = {
|
|
1
|
+
import { GetOptions, Key, SimpleStore, Value } from './simple-store.js'
|
|
2
|
+
import { Awaitable, ContextOptions } from './util.js'
|
|
3
|
+
|
|
4
|
+
export type { GetOptions }
|
|
5
|
+
export type GetCachedOptions<C = void> = ContextOptions<C> & {
|
|
11
6
|
signal?: AbortSignal
|
|
12
7
|
|
|
13
8
|
/**
|
|
@@ -28,11 +23,17 @@ export type GetCachedOptions<C = void> = {
|
|
|
28
23
|
* @default false // If no isStale option was provided to the CachedGetter
|
|
29
24
|
*/
|
|
30
25
|
allowStale?: boolean
|
|
31
|
-
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type GetterOptions<C = void> = {
|
|
29
|
+
context: C extends void ? undefined : C
|
|
30
|
+
noCache: boolean
|
|
31
|
+
signal?: AbortSignal
|
|
32
|
+
}
|
|
32
33
|
|
|
33
34
|
export type Getter<K extends Key, V extends Value, C = void> = (
|
|
34
35
|
key: K,
|
|
35
|
-
options:
|
|
36
|
+
options: GetterOptions<C>,
|
|
36
37
|
storedValue: undefined | V,
|
|
37
38
|
) => Awaitable<V>
|
|
38
39
|
|
|
@@ -60,12 +61,12 @@ export class CachedGetter<
|
|
|
60
61
|
V extends Value = Value,
|
|
61
62
|
C = void,
|
|
62
63
|
> {
|
|
63
|
-
private pending = new Map<K, PendingItem<V>>()
|
|
64
|
+
private readonly pending = new Map<K, PendingItem<V>>()
|
|
64
65
|
|
|
65
66
|
constructor(
|
|
66
67
|
readonly getter: Getter<K, V, C>,
|
|
67
68
|
readonly store: SimpleStore<K, V>,
|
|
68
|
-
readonly options
|
|
69
|
+
readonly options: CachedGetterOptions<K, V> = {},
|
|
69
70
|
) {}
|
|
70
71
|
|
|
71
72
|
async get(
|
|
@@ -76,41 +77,55 @@ export class CachedGetter<
|
|
|
76
77
|
key: C extends void ? never : K,
|
|
77
78
|
options: GetCachedOptions<C>,
|
|
78
79
|
): Promise<V>
|
|
79
|
-
async get(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
async get(
|
|
81
|
+
key: K,
|
|
82
|
+
{
|
|
83
|
+
signal,
|
|
84
|
+
context,
|
|
85
|
+
allowStale = false,
|
|
86
|
+
noCache = false,
|
|
87
|
+
} = {} as GetCachedOptions<C>,
|
|
88
|
+
): Promise<V> {
|
|
89
|
+
signal?.throwIfAborted()
|
|
90
|
+
|
|
91
|
+
const { isStale, deleteOnError } = this.options
|
|
92
|
+
|
|
93
|
+
const allowStored: (value: V) => Awaitable<boolean> = noCache
|
|
85
94
|
? returnFalse // Never allow stored values to be returned
|
|
86
|
-
:
|
|
95
|
+
: allowStale || isStale == null
|
|
87
96
|
? returnTrue // Always allow stored values to be returned
|
|
88
97
|
: async (value: V) => !(await isStale(key, value))
|
|
89
98
|
|
|
90
99
|
// As long as concurrent requests are made for the same key, only one
|
|
91
|
-
// request will be made to the
|
|
92
|
-
// because there is no async operation between the while() loop and
|
|
93
|
-
// pending.set() call. Because of the
|
|
100
|
+
// request will be made to the getStored & getter functions at a time. This
|
|
101
|
+
// works because there is no async operation between the while() loop and
|
|
102
|
+
// the pending.set() call below. Because of the single threaded nature of
|
|
94
103
|
// JavaScript, the pending item will be set before the next iteration of the
|
|
95
|
-
// while loop.
|
|
104
|
+
// while loop of any concurrent request.
|
|
96
105
|
let previousExecutionFlow: undefined | PendingItem<V>
|
|
97
106
|
while ((previousExecutionFlow = this.pending.get(key))) {
|
|
98
107
|
try {
|
|
108
|
+
// If a concurrent request is already in progress, wait for it to finish
|
|
99
109
|
const { isFresh, value } = await previousExecutionFlow
|
|
100
110
|
|
|
111
|
+
// Use the concurrent request's result if it is fresh
|
|
101
112
|
if (isFresh) return value
|
|
113
|
+
// Use the concurrent request's result if not fresh (loaded from the
|
|
114
|
+
// store), and matches the conditions for using a stored value.
|
|
102
115
|
if (await allowStored(value)) return value
|
|
103
116
|
} catch {
|
|
104
117
|
// Ignore errors from previous execution flows (they will have been
|
|
105
118
|
// propagated by that flow).
|
|
106
119
|
}
|
|
107
120
|
|
|
108
|
-
|
|
121
|
+
// Break the loop if the signal was aborted
|
|
122
|
+
signal?.throwIfAborted()
|
|
109
123
|
}
|
|
110
124
|
|
|
111
125
|
const currentExecutionFlow: PendingItem<V> = Promise.resolve()
|
|
112
126
|
.then(async () => {
|
|
113
|
-
const storedValue = await this.getStored(key,
|
|
127
|
+
const storedValue = await this.getStored(key, { signal })
|
|
128
|
+
|
|
114
129
|
if (storedValue !== undefined && (await allowStored(storedValue))) {
|
|
115
130
|
// Use the stored value as return value for the current execution
|
|
116
131
|
// flow. Notify other concurrent execution flows (that should be
|
|
@@ -120,11 +135,13 @@ export class CachedGetter<
|
|
|
120
135
|
}
|
|
121
136
|
|
|
122
137
|
return Promise.resolve()
|
|
123
|
-
.then(async () =>
|
|
138
|
+
.then(async () => {
|
|
139
|
+
const options = { signal, noCache, context } as GetterOptions<C>
|
|
140
|
+
return this.getter.call(null, key, options, storedValue)
|
|
141
|
+
})
|
|
124
142
|
.catch(async (err) => {
|
|
125
143
|
if (storedValue !== undefined) {
|
|
126
144
|
try {
|
|
127
|
-
const deleteOnError = this.options?.deleteOnError
|
|
128
145
|
if (await deleteOnError?.(err, key, storedValue)) {
|
|
129
146
|
await this.delStored(key, err)
|
|
130
147
|
}
|
|
@@ -161,7 +178,7 @@ export class CachedGetter<
|
|
|
161
178
|
return value
|
|
162
179
|
}
|
|
163
180
|
|
|
164
|
-
async getStored(key: K, options?:
|
|
181
|
+
async getStored(key: K, options?: GetOptions): Promise<V | undefined> {
|
|
165
182
|
try {
|
|
166
183
|
return await this.store.get(key, options)
|
|
167
184
|
} catch (err) {
|
package/src/index.ts
CHANGED
package/src/simple-store.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { Awaitable } from './util.js'
|
|
2
2
|
|
|
3
|
-
export type Key =
|
|
3
|
+
export type Key = NonNullable<unknown>
|
|
4
4
|
export type Value = NonNullable<unknown> | null
|
|
5
5
|
|
|
6
6
|
export type GetOptions = { signal?: AbortSignal }
|
|
7
7
|
|
|
8
|
-
export interface SimpleStore<K extends Key
|
|
8
|
+
export interface SimpleStore<K extends Key, V extends Value> {
|
|
9
9
|
/**
|
|
10
10
|
* @return undefined if the key is not in the store (which is why Value cannot contain "undefined").
|
|
11
11
|
*/
|
package/src/util.ts
ADDED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["./src/cached-getter.ts","./src/index.ts","./src/simple-store.ts"],"version":"5.8.2"}
|
|
1
|
+
{"root":["./src/cached-getter.ts","./src/index.ts","./src/simple-store.ts","./src/util.ts"],"version":"5.8.2"}
|