@zajno/common 2.7.7 → 2.8.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/cjs/lazy/extensions.js +48 -0
- package/cjs/lazy/extensions.js.map +1 -0
- package/cjs/lazy/lazy.js +35 -14
- package/cjs/lazy/lazy.js.map +1 -1
- package/cjs/lazy/light.js +11 -1
- package/cjs/lazy/light.js.map +1 -1
- package/cjs/lazy/promise.js +139 -18
- package/cjs/lazy/promise.js.map +1 -1
- package/cjs/models/Loading.js.map +1 -1
- package/cjs/structures/extendObject.js +8 -0
- package/cjs/structures/extendObject.js.map +1 -0
- package/cjs/structures/path/utils/indexTrim.js +3 -3
- package/cjs/structures/path/utils/indexTrim.js.map +1 -1
- package/cjs/structures/promiseCache.js +126 -19
- package/cjs/structures/promiseCache.js.map +1 -1
- package/cjs/structures/promiseProxy.js +2 -5
- package/cjs/structures/promiseProxy.js.map +1 -1
- package/cjs/structures/tempoCache.js.map +1 -1
- package/esm/lazy/extensions.js +45 -0
- package/esm/lazy/extensions.js.map +1 -0
- package/esm/lazy/lazy.js +35 -14
- package/esm/lazy/lazy.js.map +1 -1
- package/esm/lazy/light.js +11 -1
- package/esm/lazy/light.js.map +1 -1
- package/esm/lazy/promise.js +139 -18
- package/esm/lazy/promise.js.map +1 -1
- package/esm/models/Loading.js.map +1 -1
- package/esm/structures/extendObject.js +5 -0
- package/esm/structures/extendObject.js.map +1 -0
- package/esm/structures/path/utils/indexTrim.js +3 -3
- package/esm/structures/path/utils/indexTrim.js.map +1 -1
- package/esm/structures/promiseCache.js +126 -19
- package/esm/structures/promiseCache.js.map +1 -1
- package/esm/structures/promiseProxy.js +2 -5
- package/esm/structures/promiseProxy.js.map +1 -1
- package/esm/structures/tempoCache.js.map +1 -1
- package/package.json +1 -1
- package/tsconfig.cjs.tsbuildinfo +1 -1
- package/tsconfig.esm.tsbuildinfo +1 -1
- package/tsconfig.types.tsbuildinfo +1 -1
- package/types/lazy/extensions.d.ts +10 -0
- package/types/lazy/lazy.d.ts +8 -3
- package/types/lazy/light.d.ts +3 -1
- package/types/lazy/promise.d.ts +63 -10
- package/types/lazy/types.d.ts +106 -8
- package/types/models/Loading.d.ts +4 -4
- package/types/models/Model.d.ts +2 -2
- package/types/models/types.d.ts +1 -1
- package/types/structures/extendObject.d.ts +13 -0
- package/types/structures/path/utils/indexTrim.d.ts +2 -1
- package/types/structures/promiseCache.d.ts +127 -12
- package/types/structures/promiseProxy.d.ts +10 -6
- package/types/structures/tempoCache.d.ts +3 -2
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
import { Loggable } from '../logger/loggable.js';
|
|
2
2
|
import type { IMapModel, IValueModel } from '../models/types.js';
|
|
3
|
+
/** Represents a state of a cached item. Holds a references to an actual state. */
|
|
3
4
|
export type DeferredGetter<T> = {
|
|
5
|
+
/** Get current resolved value, if any, or initiates fetching. */
|
|
4
6
|
readonly current: T | null | undefined;
|
|
7
|
+
/** Returns a promise that resolves to the current or fetching value. */
|
|
5
8
|
readonly promise: Promise<T | null>;
|
|
6
|
-
|
|
9
|
+
/** Returns true if the item is currently being fetched. Returns undefined if fetching has not started yet. */
|
|
10
|
+
readonly isLoading: boolean | undefined;
|
|
7
11
|
};
|
|
8
12
|
export declare namespace DeferredGetter {
|
|
13
|
+
/** Empty resolved value. */
|
|
9
14
|
const Empty: {
|
|
10
15
|
readonly current: null;
|
|
11
16
|
readonly promise: Promise<null>;
|
|
12
|
-
readonly
|
|
17
|
+
readonly isLoading: boolean;
|
|
13
18
|
};
|
|
14
19
|
}
|
|
15
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Caches items by a key (string or another type) which are resolved by an async fetcher (`Promise`).
|
|
16
22
|
*
|
|
17
23
|
* Supports:
|
|
18
24
|
* - custom key adapter and parser for non-string keys.
|
|
25
|
+
* - direct manual cache manipulation.
|
|
19
26
|
* - batching of fetches.
|
|
20
27
|
* - auto-invalidation of cached items.
|
|
21
28
|
*/
|
|
@@ -23,22 +30,70 @@ export declare class PromiseCache<T, K = string> extends Loggable {
|
|
|
23
30
|
private readonly fetcher;
|
|
24
31
|
private readonly keyAdapter?;
|
|
25
32
|
private readonly keyParser?;
|
|
33
|
+
/** Stores resolved items in map by id. */
|
|
26
34
|
protected readonly _itemsCache: IMapModel<string, T | null | undefined>;
|
|
35
|
+
/** Stores items loading state (loading or not) in map by id. */
|
|
27
36
|
protected readonly _itemsStatus: IMapModel<string, boolean>;
|
|
28
|
-
|
|
37
|
+
/** Stores items loading count. */
|
|
38
|
+
protected readonly _loadingCount: IValueModel<number>;
|
|
39
|
+
/** Stores items Promises state (if still loading) in map by id. */
|
|
29
40
|
private readonly _fetchCache;
|
|
41
|
+
/** Stores items resolve timestamps (for expiration) in map by id. */
|
|
30
42
|
private readonly _timestamps;
|
|
31
43
|
private _batch;
|
|
32
44
|
private _invalidationTimeMs;
|
|
33
45
|
private _keepInstanceDuringInvalidation;
|
|
34
46
|
private _version;
|
|
47
|
+
/**
|
|
48
|
+
* Creates an instance of PromiseCache.
|
|
49
|
+
* @param fetcher Function to fetch data by key.
|
|
50
|
+
* @param keyAdapter Optional function to adapt non-string keys to strings.
|
|
51
|
+
* @param keyParser Optional function to parse string keys back to their original type.
|
|
52
|
+
*/
|
|
35
53
|
constructor(fetcher: (id: K) => Promise<T>, keyAdapter?: (K extends string ? null : (k: K) => string) | undefined, keyParser?: (K extends string ? null : (id: string) => K) | undefined);
|
|
36
54
|
get busyCount(): number;
|
|
55
|
+
/**
|
|
56
|
+
* @pure @const
|
|
57
|
+
* Creates a model for tracking the loading state. Override to inject own instance, e.g. for observability.
|
|
58
|
+
*
|
|
59
|
+
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
60
|
+
*
|
|
61
|
+
* @returns A value model for the loading count.
|
|
62
|
+
*/
|
|
37
63
|
protected pure_createBusyCount(): IValueModel<number>;
|
|
64
|
+
/**
|
|
65
|
+
* @pure @const
|
|
66
|
+
* Creates a map for caching resolved items by id. Override to inject own instance, e.g. for observability.
|
|
67
|
+
*
|
|
68
|
+
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
69
|
+
*
|
|
70
|
+
* @returns A map model for the items cache.
|
|
71
|
+
*/
|
|
38
72
|
protected pure_createItemsCache(): IMapModel<string, T | null | undefined>;
|
|
73
|
+
/**
|
|
74
|
+
* @pure @const
|
|
75
|
+
* Creates a map for tracking the loading state of items by id. Override to inject own instance, e.g. for observability.
|
|
76
|
+
*
|
|
77
|
+
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
78
|
+
*
|
|
79
|
+
* @returns A map model for the items loading state.
|
|
80
|
+
*/
|
|
39
81
|
protected pure_createItemsStatus(): IMapModel<string, boolean>;
|
|
82
|
+
/**
|
|
83
|
+
* @pure @const
|
|
84
|
+
* Creates a map for caching promises of items by id. Override to inject own instance, e.g. for observability.
|
|
85
|
+
*
|
|
86
|
+
* Warning: as name indicates, this should be "pure"/"const" function, i.e. should not reference `this`/`super`.
|
|
87
|
+
*
|
|
88
|
+
* @returns A map model for the items promises cache.
|
|
89
|
+
*/
|
|
40
90
|
protected pure_createFetchCache(): IMapModel<string, Promise<T | null>>;
|
|
41
91
|
private _pk;
|
|
92
|
+
/**
|
|
93
|
+
* Creates a logger name for this instance.
|
|
94
|
+
* @param name The name of the cache instance.
|
|
95
|
+
* @returns The logger name.
|
|
96
|
+
*/
|
|
42
97
|
protected getLoggerName(name: string | undefined): string;
|
|
43
98
|
/**
|
|
44
99
|
* Provide a fetcher function that takes multiple ids and returns multiple results at once. Will be called with a slight delay to allow multiple ids to be collected.
|
|
@@ -52,40 +107,100 @@ export declare class PromiseCache<T, K = string> extends Loggable {
|
|
|
52
107
|
* Enables auto-invalidation of cached items.
|
|
53
108
|
*
|
|
54
109
|
* @param ms Time in milliseconds after which the item will be considered invalid. If null, auto-invalidation is disabled.
|
|
110
|
+
* @param keepInstance If true, the cached item will not be removed during invalidation, but will be set to `undefined` instead. Defaults to false.
|
|
55
111
|
*/
|
|
56
112
|
useInvalidationTime(ms: number | null, keepInstance?: boolean): this;
|
|
113
|
+
/**
|
|
114
|
+
* Returns a {@link DeferredGetter} object for a specfied key.
|
|
115
|
+
*
|
|
116
|
+
* This can be used to access the current value, promise, and loading state of the item.
|
|
117
|
+
*
|
|
118
|
+
* @param key The key of the item.
|
|
119
|
+
*/
|
|
57
120
|
getDeferred(key: K): DeferredGetter<T>;
|
|
121
|
+
/**
|
|
122
|
+
* Returns the loading state of an item.
|
|
123
|
+
*
|
|
124
|
+
* @param id The id of the item.
|
|
125
|
+
* @returns The loading state of the item: true if loading, false if loading completed, undefined if loading was not started yet for the specified key.
|
|
126
|
+
*/
|
|
58
127
|
getIsBusy(id: K): boolean | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* Returns the current cached value for the specified key, without triggering a fetch.
|
|
130
|
+
* @param id The id of the item.
|
|
131
|
+
* @returns The current cached value of the item.
|
|
132
|
+
*/
|
|
59
133
|
protected _getCurrent(id: K): {
|
|
60
134
|
item: T | null | undefined;
|
|
61
135
|
key: string;
|
|
62
136
|
isInvalid: boolean;
|
|
63
137
|
};
|
|
138
|
+
/**
|
|
139
|
+
* Returns the current cached value for the specified key, optionally triggering a fetch.
|
|
140
|
+
* @param id The id of the item.
|
|
141
|
+
* @param initiateFetch If true, will initiate a fetch if the item is not cached.
|
|
142
|
+
* @returns The current cached value of the item.
|
|
143
|
+
*/
|
|
64
144
|
getCurrent(id: K, initiateFetch?: boolean): T | null | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Returns a promise that resolves to the cached value of the item if loaded already, otherwise starts fetching and the promise will be resolved to the final value.
|
|
147
|
+
*
|
|
148
|
+
* Consequent calls will return the same promise until it resolves.
|
|
149
|
+
*
|
|
150
|
+
* @param id The id of the item.
|
|
151
|
+
* @returns A promise that resolves to the result, whether it's cached or freshly fetched.
|
|
152
|
+
*/
|
|
65
153
|
get(id: K): Promise<T | null>;
|
|
154
|
+
/**
|
|
155
|
+
* Fetches the item asynchronously.
|
|
156
|
+
* @param id The id of the item.
|
|
157
|
+
* @param key The cache key.
|
|
158
|
+
* @returns A promise that resolves to the fetched item.
|
|
159
|
+
*/
|
|
66
160
|
protected _doFetchAsync: (id: K, key: string) => Promise<T | null>;
|
|
161
|
+
/**
|
|
162
|
+
* Instantly invalidates the cached item for the specified id, like it was never fetched/accessed.
|
|
163
|
+
* @param id The id of the item.
|
|
164
|
+
*/
|
|
67
165
|
invalidate(id: K): void;
|
|
166
|
+
/**
|
|
167
|
+
* Updates the cached value for the specified id directly, like it was fetched already. Overrides existing value, if any.
|
|
168
|
+
* @param id The id of the item.
|
|
169
|
+
* @param value The new value to cache.
|
|
170
|
+
*/
|
|
68
171
|
updateValueDirectly(id: K, value: T | null): void;
|
|
172
|
+
/** Returns true if the item is cached or fetching was initiated. Does not initiates fetching. */
|
|
69
173
|
hasKey(id: K): boolean;
|
|
174
|
+
/** Returns an iterator over the keys of the cached items. */
|
|
70
175
|
keys(iterate: true): MapIterator<string>;
|
|
176
|
+
/** Returns an array of the keys of the cached items. */
|
|
71
177
|
keys(): string[];
|
|
178
|
+
/** Returns an iterator over the parsed keys of the cached items. */
|
|
72
179
|
keysParsed(iterate: true): Generator<K> | null;
|
|
180
|
+
/** Returns an array of the parsed keys of the cached items. */
|
|
73
181
|
keysParsed(): K[] | null;
|
|
182
|
+
/** Clears the cache and resets the loading state. */
|
|
74
183
|
clear(): void;
|
|
184
|
+
/** @internal updates all caches states at once. */
|
|
75
185
|
protected _set(key: string, item: T | null | undefined, promise: Promise<T> | undefined, busy: boolean | undefined): void;
|
|
186
|
+
/**
|
|
187
|
+
* Checks if the cached item for the specified key is invalidated (expired).
|
|
188
|
+
* @param key The cache key.
|
|
189
|
+
* @returns True if the item is invalidated, false otherwise.
|
|
190
|
+
*/
|
|
76
191
|
protected getIsInvalidated(key: string): boolean;
|
|
77
|
-
/**
|
|
192
|
+
/** Updates the loading status for the specified key. Override to add a hook. */
|
|
78
193
|
protected setStatus(key: string, status: boolean): void;
|
|
79
|
-
/**
|
|
194
|
+
/** Updates the promise for the specified key. Override to add a hook. */
|
|
80
195
|
protected setPromise(key: string, promise: Promise<T | null>): void;
|
|
81
|
-
/**
|
|
196
|
+
/** Stores the result for the specified key. Override to add a hook. */
|
|
82
197
|
protected storeResult(key: string, res: T | null): void;
|
|
83
|
-
/**
|
|
198
|
+
/** Hooks into the fetch process before it starts. */
|
|
84
199
|
protected onBeforeFetch(_key: string): void;
|
|
85
|
-
/**
|
|
86
|
-
protected prepareResult(res: T | null): NonNullable<T> | null;
|
|
87
|
-
/** @override */
|
|
200
|
+
/** Hooks into the fetch process after it completes. */
|
|
88
201
|
protected onFetchComplete(key: string): void;
|
|
89
|
-
/**
|
|
202
|
+
/** Hooks into the result preparation process, before it's stored into the cache. */
|
|
203
|
+
protected prepareResult(res: T | null): NonNullable<T> | null;
|
|
204
|
+
/** @pure Performs a fetch operation in batch mode if available, otherwise uses the regular fetch. */
|
|
90
205
|
protected tryFetchInBatch(id: K): Promise<T | null>;
|
|
91
206
|
}
|
|
@@ -11,18 +11,22 @@ type ForbiddenKeys = typeof PromiseGetter;
|
|
|
11
11
|
type NoForbiddenKeys<T> = {
|
|
12
12
|
[K in keyof T]: K extends ForbiddenKeys ? never : T[K];
|
|
13
13
|
};
|
|
14
|
-
|
|
14
|
+
interface LazyPromiseCtor<K> {
|
|
15
|
+
new (loader: () => Promise<K>): LazyPromise<K>;
|
|
16
|
+
}
|
|
17
|
+
type Options<T, TFnKeys, TWrap, TLazy extends LazyPromiseCtor<T>> = {
|
|
18
|
+
/** Function returning a promise that resolves to a proxied object */
|
|
15
19
|
loader: () => Promise<T>;
|
|
20
|
+
/** An array of keys that should be treated as functions so that they can be called before the object is resolved */
|
|
16
21
|
fnKeys?: TFnKeys[];
|
|
22
|
+
/** An object that will be used as a wrapper for the proxied object. Can contain any fields that will be copied to the resolved object */
|
|
17
23
|
wrap?: TWrap;
|
|
24
|
+
/** A constructor that creates {@link {LazyPromise}} instance for handling loader */
|
|
18
25
|
lazy?: TLazy;
|
|
19
26
|
};
|
|
20
27
|
/**
|
|
21
|
-
* Creates a proxy object that will be resolved to the object returned by the loader function.
|
|
22
|
-
*
|
|
23
|
-
* @param loader a function returning a promise that resolves to a proxied object
|
|
24
|
-
* @param fnKeys an array of keys that should be treated as functions so that they can be called before the object is resolved
|
|
25
|
-
* @param wrap an object that will be used as a wrapper for the proxied object. can contain any fields that will be copied to the resolved object
|
|
28
|
+
* Creates a proxy object that will be resolved to the object returned by the loader function.
|
|
29
|
+
* @param options - Options for creating the proxy, see {@link Options}
|
|
26
30
|
* @returns a proxy object that will be resolved to the object returned by the loader function
|
|
27
31
|
*/
|
|
28
32
|
export declare function createPromiseProxy<T extends NoForbiddenKeys<T>, TFnKeys extends AllowedFnKeys<T> = never, TWrap extends object = object>(options: Options<T, TFnKeys, TWrap, typeof LazyPromise>): PromiseProxy<T, StringKeys<T>, TFnKeys, TWrap>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ILazy, ILazyPromise } from '../lazy/types.js';
|
|
2
|
+
import type { IResettableModel } from '../models/types.js';
|
|
2
3
|
import { ExpireTracker } from './expire.js';
|
|
3
4
|
/**
|
|
4
5
|
* Calls factory fn to fetch and store some value for a limited time.
|
|
@@ -19,11 +20,11 @@ export declare namespace TempoCache {
|
|
|
19
20
|
*
|
|
20
21
|
* Note a limitation: calling `reset` (or changing the stored value directly in other way) on `lazy` will not affect the value cached by `TempoCache` instance.
|
|
21
22
|
*/
|
|
22
|
-
function createFromLazy<T>(lazy: ILazy<T
|
|
23
|
+
function createFromLazy<T>(lazy: ILazy<T> & IResettableModel, lifetimeMs: number): TempoCache<T>;
|
|
23
24
|
/**
|
|
24
25
|
* Creates a TempoCache instance which caches `ILazyPromise.promise` for a specified time.
|
|
25
26
|
*
|
|
26
27
|
* Note a limitation: calling `reset` (or changing the stored value directly in other way) on `lazy` will not affect the value cached by `TempoCache` instance.
|
|
27
28
|
*/
|
|
28
|
-
function createFromLazyPromise<T>(lazy: ILazyPromise<T
|
|
29
|
+
function createFromLazyPromise<T>(lazy: ILazyPromise<T> & IResettableModel, lifetimeMs: number): TempoCache<Promise<T>>;
|
|
29
30
|
}
|