@atproto-labs/simple-store 0.1.1 → 0.2.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 +16 -0
- package/LICENSE.txt +1 -1
- package/dist/cached-getter.d.ts +17 -12
- package/dist/cached-getter.d.ts.map +1 -1
- package/dist/cached-getter.js +5 -8
- package/dist/cached-getter.js.map +1 -1
- package/package.json +2 -3
- package/src/cached-getter.ts +35 -20
- package/tsconfig.build.tsbuildinfo +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @atproto-labs/simple-store
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#3776](https://github.com/bluesky-social/atproto/pull/3776) [`0d77d1b55`](https://github.com/bluesky-social/atproto/commit/0d77d1b550a58117aee8f7f1e2be24d255ade9e4) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Remove `bind` method from `CachedGetter`
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#3776](https://github.com/bluesky-social/atproto/pull/3776) [`0d77d1b55`](https://github.com/bluesky-social/atproto/commit/0d77d1b550a58117aee8f7f1e2be24d255ade9e4) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add `context` getter option to `CachedGetter` class
|
|
12
|
+
|
|
13
|
+
## 0.1.2
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [#3220](https://github.com/bluesky-social/atproto/pull/3220) [`61dc0d60e`](https://github.com/bluesky-social/atproto/commit/61dc0d60e19b88c6427a54c6d95a391b5f4da7bd) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Apply new linting rules regarding import order
|
|
18
|
+
|
|
3
19
|
## 0.1.1
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/LICENSE.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Dual MIT/Apache-2.0 License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022-
|
|
3
|
+
Copyright (c) 2022-2025 Bluesky 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,5 +1,6 @@
|
|
|
1
|
-
import { Awaitable,
|
|
2
|
-
export type
|
|
1
|
+
import { Awaitable, GetOptions as GetStoredOptions, Key, SimpleStore, Value } from './simple-store.js';
|
|
2
|
+
export type { GetStoredOptions };
|
|
3
|
+
export type GetCachedOptions<C = void> = {
|
|
3
4
|
signal?: AbortSignal;
|
|
4
5
|
/**
|
|
5
6
|
* Do not use the cache to get the value. Always get a new value from the
|
|
@@ -18,9 +19,13 @@ export type GetCachedOptions = {
|
|
|
18
19
|
* @default false // If no isStale option was provided to the CachedGetter
|
|
19
20
|
*/
|
|
20
21
|
allowStale?: boolean;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
} & (C extends void ? {
|
|
23
|
+
context?: C;
|
|
24
|
+
} : {
|
|
25
|
+
context: C;
|
|
26
|
+
});
|
|
27
|
+
export type Getter<K extends Key, V extends Value, C = void> = (key: K, options: GetCachedOptions<C>, storedValue: undefined | V) => Awaitable<V>;
|
|
28
|
+
export type CachedGetterOptions<K extends Key, V extends Value> = {
|
|
24
29
|
isStale?: (key: K, value: V) => boolean | PromiseLike<boolean>;
|
|
25
30
|
onStoreError?: (err: unknown, key: K, value: V) => void | PromiseLike<void>;
|
|
26
31
|
deleteOnError?: (err: unknown, key: K, value: V) => boolean | PromiseLike<boolean>;
|
|
@@ -29,15 +34,15 @@ export type CachedGetterOptions<K, V> = {
|
|
|
29
34
|
* Wrapper utility that uses a store to speed up the retrieval of values from an
|
|
30
35
|
* (expensive) getter function.
|
|
31
36
|
*/
|
|
32
|
-
export declare class CachedGetter<K extends Key = string, V extends Value = Value> {
|
|
33
|
-
readonly getter: Getter<K, V>;
|
|
37
|
+
export declare class CachedGetter<K extends Key = string, V extends Value = Value, C = void> {
|
|
38
|
+
readonly getter: Getter<K, V, C>;
|
|
34
39
|
readonly store: SimpleStore<K, V>;
|
|
35
|
-
readonly options?:
|
|
40
|
+
readonly options?: CachedGetterOptions<K, V> | undefined;
|
|
36
41
|
private pending;
|
|
37
|
-
constructor(getter: Getter<K, V>, store: SimpleStore<K, V>, options?:
|
|
38
|
-
get(key: K, options?: GetCachedOptions): Promise<V>;
|
|
39
|
-
|
|
40
|
-
getStored(key: K, options?:
|
|
42
|
+
constructor(getter: Getter<K, V, C>, store: SimpleStore<K, V>, options?: CachedGetterOptions<K, V> | undefined);
|
|
43
|
+
get(key: C extends void ? K : never, options?: GetCachedOptions<C>): Promise<V>;
|
|
44
|
+
get(key: C extends void ? never : K, options: GetCachedOptions<C>): Promise<V>;
|
|
45
|
+
getStored(key: K, options?: GetStoredOptions): Promise<V | undefined>;
|
|
41
46
|
setStored(key: K, value: V): Promise<void>;
|
|
42
47
|
delStored(key: K, _cause?: unknown): Promise<void>;
|
|
43
48
|
}
|
|
@@ -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,EACL,SAAS,EACT,UAAU,IAAI,gBAAgB,EAC9B,GAAG,EACH,WAAW,EACX,KAAK,EACN,MAAM,mBAAmB,CAAA;AAE1B,YAAY,EAAE,gBAAgB,EAAE,CAAA;AAChC,MAAM,MAAM,gBAAgB,CAAC,CAAC,GAAG,IAAI,IAAI;IACvC,MAAM,CAAC,EAAE,WAAW,CAAA;IAEpB;;;;;OAK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AAC,EAAE,CAAC,CAAC;IACjC,QAAQ,CAAC,OAAO,CAAC,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC;IAL9C,OAAO,CAAC,OAAO,CAA+B;gBAGnC,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EACvB,KAAK,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,EACxB,OAAO,CAAC,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,YAAA;IAGxC,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;IAsFP,SAAS,CAAC,GAAG,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAQrE,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
|
@@ -34,12 +34,12 @@ class CachedGetter {
|
|
|
34
34
|
value: new Map()
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
-
async get(key, options) {
|
|
38
|
-
options
|
|
37
|
+
async get(key, options = {}) {
|
|
38
|
+
options.signal?.throwIfAborted();
|
|
39
39
|
const isStale = this.options?.isStale;
|
|
40
|
-
const allowStored = options
|
|
40
|
+
const allowStored = options.noCache
|
|
41
41
|
? returnFalse // Never allow stored values to be returned
|
|
42
|
-
: options
|
|
42
|
+
: options.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
|
|
@@ -61,7 +61,7 @@ class CachedGetter {
|
|
|
61
61
|
// Ignore errors from previous execution flows (they will have been
|
|
62
62
|
// propagated by that flow).
|
|
63
63
|
}
|
|
64
|
-
options
|
|
64
|
+
options.signal?.throwIfAborted();
|
|
65
65
|
}
|
|
66
66
|
const currentExecutionFlow = Promise.resolve()
|
|
67
67
|
.then(async () => {
|
|
@@ -109,9 +109,6 @@ class CachedGetter {
|
|
|
109
109
|
const { value } = await currentExecutionFlow;
|
|
110
110
|
return value;
|
|
111
111
|
}
|
|
112
|
-
bind(key) {
|
|
113
|
-
return async (options) => this.get(key, options);
|
|
114
|
-
}
|
|
115
112
|
async getStored(key, options) {
|
|
116
113
|
try {
|
|
117
114
|
return await this.store.get(key, options);
|
|
@@ -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":";;;AAkDA,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,OAAmC;QAF5C;;;;mBAAS,MAAM;WAAiB;QAChC;;;;mBAAS,KAAK;WAAmB;QACjC;;;;mBAAS,OAAO;WAA4B;QALtC;;;;mBAAU,IAAI,GAAG,EAAqB;WAAA;IAM3C,CAAC;IAUJ,KAAK,CAAC,GAAG,CAAC,GAAM,EAAE,UAAU,EAAyB;QACnD,OAAO,CAAC,MAAM,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AE,CAAC;YAC1B,kEAAkE;YAClE,mEAAmE;YACnE,sEAAsE;YACtE,6CAA6C;YAC7C,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;QACxD,CAAC;QAED,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,oBAAoB,CAAC,CAAA;QAE3C,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,oBAAoB,CAAA;QAC5C,OAAO,KAAK,CAAA;IACd,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,GAAM,EAAE,OAA0B;QAChD,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAC3C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,SAAS,CAAA;QAClB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,GAAM,EAAE,KAAQ;QAC9B,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QAClC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,EAAE,YAAY,CAAA;YAC/C,MAAM,YAAY,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QACvC,CAAC;IACH,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,GAAM,EAAE,MAAgB;QACtC,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC3B,CAAC;CACF;AA9HD,oCA8HC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto-labs/simple-store",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Simple store interfaces & utilities",
|
|
6
6
|
"keywords": [
|
|
@@ -22,9 +22,8 @@
|
|
|
22
22
|
"default": "./dist/index.js"
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
|
-
"dependencies": {},
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"typescript": "^5.
|
|
26
|
+
"typescript": "^5.6.3"
|
|
28
27
|
},
|
|
29
28
|
"scripts": {
|
|
30
29
|
"build": "tsc --build tsconfig.build.json"
|
package/src/cached-getter.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
Awaitable,
|
|
3
|
+
GetOptions as GetStoredOptions,
|
|
4
|
+
Key,
|
|
5
|
+
SimpleStore,
|
|
6
|
+
Value,
|
|
7
|
+
} from './simple-store.js'
|
|
8
|
+
|
|
9
|
+
export type { GetStoredOptions }
|
|
10
|
+
export type GetCachedOptions<C = void> = {
|
|
4
11
|
signal?: AbortSignal
|
|
5
12
|
|
|
6
13
|
/**
|
|
@@ -21,15 +28,15 @@ export type GetCachedOptions = {
|
|
|
21
28
|
* @default false // If no isStale option was provided to the CachedGetter
|
|
22
29
|
*/
|
|
23
30
|
allowStale?: boolean
|
|
24
|
-
}
|
|
31
|
+
} & (C extends void ? { context?: C } : { context: C })
|
|
25
32
|
|
|
26
|
-
export type Getter<K, V> = (
|
|
33
|
+
export type Getter<K extends Key, V extends Value, C = void> = (
|
|
27
34
|
key: K,
|
|
28
|
-
options:
|
|
35
|
+
options: GetCachedOptions<C>,
|
|
29
36
|
storedValue: undefined | V,
|
|
30
37
|
) => Awaitable<V>
|
|
31
38
|
|
|
32
|
-
export type CachedGetterOptions<K, V> = {
|
|
39
|
+
export type CachedGetterOptions<K extends Key, V extends Value> = {
|
|
33
40
|
isStale?: (key: K, value: V) => boolean | PromiseLike<boolean>
|
|
34
41
|
onStoreError?: (err: unknown, key: K, value: V) => void | PromiseLike<void>
|
|
35
42
|
deleteOnError?: (
|
|
@@ -48,23 +55,35 @@ const returnFalse = () => false
|
|
|
48
55
|
* Wrapper utility that uses a store to speed up the retrieval of values from an
|
|
49
56
|
* (expensive) getter function.
|
|
50
57
|
*/
|
|
51
|
-
export class CachedGetter<
|
|
58
|
+
export class CachedGetter<
|
|
59
|
+
K extends Key = string,
|
|
60
|
+
V extends Value = Value,
|
|
61
|
+
C = void,
|
|
62
|
+
> {
|
|
52
63
|
private pending = new Map<K, PendingItem<V>>()
|
|
53
64
|
|
|
54
65
|
constructor(
|
|
55
|
-
readonly getter: Getter<K, V>,
|
|
66
|
+
readonly getter: Getter<K, V, C>,
|
|
56
67
|
readonly store: SimpleStore<K, V>,
|
|
57
|
-
readonly options?:
|
|
68
|
+
readonly options?: CachedGetterOptions<K, V>,
|
|
58
69
|
) {}
|
|
59
70
|
|
|
60
|
-
async get(
|
|
61
|
-
|
|
71
|
+
async get(
|
|
72
|
+
key: C extends void ? K : never,
|
|
73
|
+
options?: GetCachedOptions<C>,
|
|
74
|
+
): Promise<V>
|
|
75
|
+
async get(
|
|
76
|
+
key: C extends void ? never : K,
|
|
77
|
+
options: GetCachedOptions<C>,
|
|
78
|
+
): Promise<V>
|
|
79
|
+
async get(key: K, options = {} as GetCachedOptions<C>): Promise<V> {
|
|
80
|
+
options.signal?.throwIfAborted()
|
|
62
81
|
|
|
63
82
|
const isStale = this.options?.isStale
|
|
64
83
|
|
|
65
|
-
const allowStored: (value: V) => Awaitable<boolean> = options
|
|
84
|
+
const allowStored: (value: V) => Awaitable<boolean> = options.noCache
|
|
66
85
|
? returnFalse // Never allow stored values to be returned
|
|
67
|
-
: options
|
|
86
|
+
: options.allowStale || isStale == null
|
|
68
87
|
? returnTrue // Always allow stored values to be returned
|
|
69
88
|
: async (value: V) => !(await isStale(key, value))
|
|
70
89
|
|
|
@@ -86,7 +105,7 @@ export class CachedGetter<K extends Key = string, V extends Value = Value> {
|
|
|
86
105
|
// propagated by that flow).
|
|
87
106
|
}
|
|
88
107
|
|
|
89
|
-
options
|
|
108
|
+
options.signal?.throwIfAborted()
|
|
90
109
|
}
|
|
91
110
|
|
|
92
111
|
const currentExecutionFlow: PendingItem<V> = Promise.resolve()
|
|
@@ -142,11 +161,7 @@ export class CachedGetter<K extends Key = string, V extends Value = Value> {
|
|
|
142
161
|
return value
|
|
143
162
|
}
|
|
144
163
|
|
|
145
|
-
|
|
146
|
-
return async (options) => this.get(key, options)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async getStored(key: K, options?: GetCachedOptions): Promise<V | undefined> {
|
|
164
|
+
async getStored(key: K, options?: GetStoredOptions): Promise<V | undefined> {
|
|
150
165
|
try {
|
|
151
166
|
return await this.store.get(key, options)
|
|
152
167
|
} catch (err) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/cached-getter.ts","./src/index.ts","./src/simple-store.ts"],"version":"5.8.2"}
|