@asaidimu/utils-artifacts 8.2.11 → 8.2.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/index.d.cts +1037 -0
- package/index.d.ts +541 -610
- package/index.js +2 -1
- package/index.mjs +2 -1
- package/package.json +4 -2
- package/index.d.mts +0 -842
package/index.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* latest payload. Default = no debouncing.
|
|
6
|
-
*/
|
|
7
|
-
debounce?: number;
|
|
8
|
-
}
|
|
1
|
+
import { Issue, Severity, SystemError, SystemError as SystemError$1 } from "@asaidimu/utils-error";
|
|
2
|
+
import { SubscribeOptions, SubscribeOptions as SubscribeOptions$1 } from "@asaidimu/utils-events";
|
|
3
|
+
import { SystemLogger } from "@asaidimu/utils-logger";
|
|
4
|
+
import { EventBus } from "@asaidimu/utils-events/types";
|
|
9
5
|
|
|
6
|
+
//#region src/store/types.d.ts
|
|
10
7
|
/**
|
|
11
8
|
* Utility type for representing partial updates to the state, allowing deep nesting.
|
|
12
9
|
* It makes all properties optional and applies the same transformation recursively
|
|
@@ -14,29 +11,27 @@ interface SubscribeOptions {
|
|
|
14
11
|
* preserving the original structure. It also includes the original type T and
|
|
15
12
|
* undefined as possibilities for the top level and nested values.
|
|
16
13
|
*/
|
|
17
|
-
type DeepPartial<T> = T extends object ? T extends readonly (infer U)[] ? readonly (DeepPartial<U> | undefined)[] | undefined | T : T extends (infer U)[] ? (DeepPartial<U> | undefined)[] | undefined | T : {
|
|
18
|
-
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> | undefined : T[K] | undefined;
|
|
19
|
-
} | undefined | T : T | undefined | symbol;
|
|
14
|
+
type DeepPartial<T> = T extends object ? T extends readonly (infer U)[] ? readonly (DeepPartial<U> | undefined)[] | undefined | T : T extends (infer U)[] ? (DeepPartial<U> | undefined)[] | undefined | T : { [K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> | undefined : T[K] | undefined } | undefined | T : T | undefined | symbol;
|
|
20
15
|
/**
|
|
21
16
|
* Extended store state for monitoring the current execution status (e.g., if an update is in progress).
|
|
22
17
|
*/
|
|
23
18
|
interface StoreExecutionState<T> {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
19
|
+
/** Indicates if a state update process is currently executing. */
|
|
20
|
+
executing: boolean;
|
|
21
|
+
/** The changes (DeepPartial) currently being processed in the execution cycle. Null if none. */
|
|
22
|
+
changes: DeepPartial<T> | null;
|
|
23
|
+
/** A queue of pending state update functions/objects to be applied sequentially. */
|
|
24
|
+
pendingChanges: Array<StateUpdater<T>>;
|
|
25
|
+
/** Names of all currently registered middlewares. */
|
|
26
|
+
middlewares: string[];
|
|
27
|
+
/** Details of the middleware currently running. Null if none. */
|
|
28
|
+
runningMiddleware: {
|
|
29
|
+
id: string;
|
|
30
|
+
name: string;
|
|
31
|
+
startTime: number;
|
|
32
|
+
} | null;
|
|
33
|
+
/** Indicates if the store is currently within an active transaction block. */
|
|
34
|
+
transactionActive: boolean;
|
|
40
35
|
}
|
|
41
36
|
/**
|
|
42
37
|
* Event types emitted by the state store for observability and debugging.
|
|
@@ -63,194 +58,191 @@ type TransformMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<Dee
|
|
|
63
58
|
* It returns a boolean or an object containing a `block` boolean and an optional `error`.
|
|
64
59
|
*/
|
|
65
60
|
type BlockingMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<boolean | {
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
block: boolean;
|
|
62
|
+
error?: Error;
|
|
68
63
|
}> | boolean | {
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
block: boolean;
|
|
65
|
+
error?: Error;
|
|
71
66
|
};
|
|
72
67
|
/**
|
|
73
68
|
* Type representing the configuration object passed to the `use` method to register a middleware.
|
|
74
69
|
*/
|
|
75
70
|
interface MiddlewareConfig<T> {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
71
|
+
/** The middleware function (can be a transform or blocking middleware). */
|
|
72
|
+
action: TransformMiddleware<T> | BlockingMiddleware<T>;
|
|
73
|
+
/** An optional, human-readable name for the middleware. */
|
|
74
|
+
name?: string;
|
|
75
|
+
/** If true, the middleware is treated as a blocking middleware (must return a boolean or `{ block: boolean }`). */
|
|
76
|
+
block?: boolean;
|
|
82
77
|
}
|
|
83
78
|
/**
|
|
84
79
|
* Interface for a reactive selector result, providing access to the value and subscription capabilities.
|
|
85
80
|
*/
|
|
86
81
|
interface ReactiveSelector<S> {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
82
|
+
/** Unique identifier for the selector. */
|
|
83
|
+
id: string;
|
|
84
|
+
/** Function to get the current computed value of the selector. */
|
|
85
|
+
get: () => S;
|
|
86
|
+
/**
|
|
87
|
+
* Subscribes a callback function to run whenever the selector's result changes.
|
|
88
|
+
* Returns an unsubscribe function.
|
|
89
|
+
*/
|
|
90
|
+
subscribe: (callback: (state: S) => void) => () => void;
|
|
96
91
|
}
|
|
97
92
|
interface ActionWatcher {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
93
|
+
/** Unique identifier for the action */
|
|
94
|
+
name: string;
|
|
95
|
+
/** Function to get the current computed value of the action. */
|
|
96
|
+
status: () => boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Subscribes a callback function to run whenever the action's status changes.
|
|
99
|
+
* Returns an unsubscribe function.
|
|
100
|
+
*/
|
|
101
|
+
subscribe: (callback: () => void) => () => void;
|
|
107
102
|
}
|
|
108
103
|
interface TransactionOptions {
|
|
109
|
-
|
|
110
|
-
|
|
104
|
+
/** If true, blocks resolution until changes are fully committed to the database row */
|
|
105
|
+
flush?: boolean;
|
|
111
106
|
}
|
|
112
107
|
/**
|
|
113
108
|
* Interface defining the contract for the core data state store.
|
|
114
109
|
* T must be an object type.
|
|
115
110
|
*/
|
|
116
111
|
interface DataStore<T extends object> {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Gets the current state of the store.
|
|
114
|
+
* @param clone If true, returns a deep clone of the state; otherwise, returns the internal state reference.
|
|
115
|
+
* @returns The current state T.
|
|
116
|
+
*/
|
|
117
|
+
get(clone?: boolean): T;
|
|
118
|
+
/**
|
|
119
|
+
* Gets a subset of the store.
|
|
120
|
+
* @param paths The paths which to include in the returned object.
|
|
121
|
+
* @param separator Optional separator for paths. Defaults to `.` .
|
|
122
|
+
* @returns An object of the shape K mapping paths to their current values.
|
|
123
|
+
*/
|
|
124
|
+
subset<K extends Record<string, any> = Record<string, any>>(paths: Array<string>, separator?: string): K;
|
|
125
|
+
/**
|
|
126
|
+
* Registers a named action function that can modify the state.
|
|
127
|
+
* @param action The action configuration object.
|
|
128
|
+
* @returns A function to unregister the action.
|
|
129
|
+
*/
|
|
130
|
+
register<R extends any[]>(action: {
|
|
131
|
+
name: string;
|
|
132
|
+
fn: (state: T, ...args: R) => DeepPartial<T> | Promise<DeepPartial<T>>;
|
|
133
|
+
debounce?: {
|
|
134
|
+
delay: number;
|
|
135
|
+
condition?: (previous: R, current: R) => boolean;
|
|
136
|
+
};
|
|
137
|
+
}): () => void;
|
|
138
|
+
/**
|
|
139
|
+
* Executes (dispatches) a previously registered action by its name.
|
|
140
|
+
* @param name The name of the action.
|
|
141
|
+
* @param args The parameters to pass to the action function.
|
|
142
|
+
* @returns A promise that resolves to the final state after the action and subsequent updates are complete.
|
|
143
|
+
*/
|
|
144
|
+
dispatch<R extends any[]>(name: string, ...args: R): Promise<T>;
|
|
145
|
+
/**
|
|
146
|
+
* Sets or updates the state using a StateUpdater.
|
|
147
|
+
* @param update The new state, partial state, or a function returning a partial state.
|
|
148
|
+
* @param options Configuration options for the set operation.
|
|
149
|
+
* @returns A promise that resolves to the current state when the update is complete.
|
|
150
|
+
*/
|
|
151
|
+
set(update: StateUpdater<T>, options?: {
|
|
152
|
+
force?: boolean;
|
|
153
|
+
actionId?: string;
|
|
154
|
+
}): Promise<T>;
|
|
155
|
+
/**
|
|
156
|
+
* Creates a reactive selector that computes a derived value and tracks dependencies.
|
|
157
|
+
* @param selector A function to compute the derived state value S from the full state T.
|
|
158
|
+
* @returns A `ReactiveSelector<S>` object.
|
|
159
|
+
*/
|
|
160
|
+
select<S>(selector: (state: T) => S): ReactiveSelector<S>;
|
|
161
|
+
/**
|
|
162
|
+
* Subscribes a callback to run when the data at the specified path(s) changes.
|
|
163
|
+
* @param path A single path string or an array of path strings to watch.
|
|
164
|
+
* @param callback The function to execute when a change occurs in the watched path(s).
|
|
165
|
+
* @param options Extra options to pass to the event bus
|
|
166
|
+
* @returns An unsubscribe function.
|
|
167
|
+
*/
|
|
168
|
+
watch(path: string | Array<string>, callback: (state: T) => void, options?: SubscribeOptions): () => void;
|
|
169
|
+
/**
|
|
170
|
+
* Subscribes to execution‑status changes of a registered action.
|
|
171
|
+
*
|
|
172
|
+
* The provided callback is called **every time** the action transitions
|
|
173
|
+
* between idle and running (i.e. on `action:start`, `action:complete`, or
|
|
174
|
+
* `action:error` for the given name). The current status can also be read
|
|
175
|
+
* synchronously with `isActionRunning(name)`.
|
|
176
|
+
*
|
|
177
|
+
* **Deferred listener teardown**
|
|
178
|
+
* To avoid unnecessary churn when subscriptions are rapidly created and
|
|
179
|
+
* destroyed, the underlying event listeners are not removed immediately
|
|
180
|
+
* on unsubscribe. Instead, a *pending reset* is queued via `queueMicrotask`.
|
|
181
|
+
* If a new subscription for the same action arrives before that microtask
|
|
182
|
+
* executes, the reset is silently cancelled and the already‑established
|
|
183
|
+
* listeners are reused. This keeps the subscription infrastructure stable
|
|
184
|
+
* and prevents cascading notifications that could otherwise arise from
|
|
185
|
+
* repeated subscribe‑unsubscribe‑subscribe cycles.
|
|
186
|
+
*
|
|
187
|
+
* @param name - The name of the action to watch.
|
|
188
|
+
* @returns An ActionWatcher
|
|
189
|
+
*/
|
|
190
|
+
watchAction(name: string): ActionWatcher;
|
|
191
|
+
/**
|
|
192
|
+
* Executes an operation function within a transaction block.
|
|
193
|
+
* All state updates (`set` or actions) within the transaction are batched and applied atomically (all or nothing).
|
|
194
|
+
* @param operation The function containing the state updates.
|
|
195
|
+
* @returns A promise that resolves to the return value of the operation function.
|
|
196
|
+
*/
|
|
197
|
+
transaction<R>(operation: () => R | Promise<R>, options?: TransactionOptions): Promise<R>;
|
|
198
|
+
/**
|
|
199
|
+
* Registers a middleware function to intercept state updates.
|
|
200
|
+
* @param props The middleware configuration.
|
|
201
|
+
* @returns A function to unregister the middleware.
|
|
202
|
+
*/
|
|
203
|
+
use(props: MiddlewareConfig<T>): () => boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Subscribes a listener to a specific store event type.
|
|
206
|
+
* @param event The type of store event to listen for.
|
|
207
|
+
* @param listener The callback function to execute when the event fires.
|
|
208
|
+
* @returns An unsubscribe function.
|
|
209
|
+
*/
|
|
210
|
+
on(event: StoreEvent, listener: (data: any) => void): () => void;
|
|
211
|
+
/**
|
|
212
|
+
* Returns the unique identifier of the store instance.
|
|
213
|
+
*/
|
|
214
|
+
id(): string;
|
|
215
|
+
/**
|
|
216
|
+
* Checks whether the store is fully initialized and ready for use.
|
|
217
|
+
*/
|
|
218
|
+
isReady(): boolean;
|
|
219
|
+
/**
|
|
220
|
+
* Returns a promise that resolves once the store is fully initialised.
|
|
221
|
+
* Safe to call multiple times — all callers share the same latch.
|
|
222
|
+
*
|
|
223
|
+
* @param timeout - Optional maximum wait time in milliseconds.
|
|
224
|
+
* @throws {TimeoutError} If the store does not become ready within the timeout.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* await store.ready();
|
|
228
|
+
* const state = store.get();
|
|
229
|
+
*/
|
|
230
|
+
ready(timeout?: number): Promise<void>;
|
|
231
|
+
/**
|
|
232
|
+
* Returns a readonly snapshot of the current execution state of the store.
|
|
233
|
+
*/
|
|
234
|
+
state(): Readonly<StoreExecutionState<T>>;
|
|
235
|
+
/**
|
|
236
|
+
* Releases all resources held by the store and renders it unusable
|
|
237
|
+
*/
|
|
238
|
+
dispose(): Promise<void>;
|
|
244
239
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
* Defines the lifecycle and sharing strategy for an artifact within the container.
|
|
248
|
-
*/
|
|
249
|
-
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/artifacts/types.d.ts
|
|
250
242
|
/**
|
|
251
243
|
* Defines the lifecycle and sharing strategy for an artifact.
|
|
252
244
|
*/
|
|
253
|
-
type ArtifactScope =
|
|
245
|
+
type ArtifactScope =
|
|
254
246
|
/**
|
|
255
247
|
* **Singleton:** A single instance of the artifact is created and shared across all resolutions
|
|
256
248
|
* within the container. It is created once (often lazily) and reused until invalidated.
|
|
@@ -260,42 +252,42 @@ type ArtifactScope =
|
|
|
260
252
|
* **Transient:** A new instance of the artifact is created every time it is resolved.
|
|
261
253
|
* Transient artifacts do not participate in caching or shared state management.
|
|
262
254
|
*/
|
|
263
|
-
|
|
255
|
+
| "transient";
|
|
264
256
|
/**
|
|
265
257
|
* Enumeration of standard artifact scopes for strongly typed configuration.
|
|
266
258
|
*/
|
|
267
259
|
declare enum ArtifactScopes {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
260
|
+
/** A single instance is shared globally. */
|
|
261
|
+
Singleton = "singleton",
|
|
262
|
+
/** A new instance is created on every resolution. */
|
|
263
|
+
Transient = "transient"
|
|
272
264
|
}
|
|
273
265
|
/**
|
|
274
266
|
* Represents a snapshot of an artifact's state and dependencies for debugging purposes.
|
|
275
267
|
* Provides insight into the artifact's current status and its position within the dependency graph.
|
|
276
268
|
*/
|
|
277
269
|
interface ArtifactDebugNode {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
270
|
+
/** The unique identifier (key) of the artifact. */
|
|
271
|
+
id: string;
|
|
272
|
+
/** The scope of the artifact (Singleton or Transient). */
|
|
273
|
+
scope: ArtifactScope;
|
|
274
|
+
/**
|
|
275
|
+
* The current status of the artifact, indicating its lifecycle phase.
|
|
276
|
+
* - `'active'`: The artifact is successfully built and its instance is available.
|
|
277
|
+
* - `'error'`: The artifact failed to build due to an external (runtime) error.
|
|
278
|
+
* - `'idle'`: The artifact has not yet been built (for lazy singletons) or has been disposed.
|
|
279
|
+
* - `'building'`: The artifact's factory is currently executing.
|
|
280
|
+
* - `'pending'`: The artifact is waiting to be built, often after an invalidation and before any debounce.
|
|
281
|
+
*/
|
|
282
|
+
status: "active" | "error" | "idle" | "building" | "pending" | "debouncing";
|
|
283
|
+
/** A list of artifact keys that this artifact directly depends on. */
|
|
284
|
+
dependencies: string[];
|
|
285
|
+
/** A list of artifact keys that directly depend on this artifact (its consumers). */
|
|
286
|
+
dependents: string[];
|
|
287
|
+
/** A list of state paths (selectors) that this artifact depends on. */
|
|
288
|
+
stateDependencies: string[];
|
|
289
|
+
/** The number of times this artifact's factory has been successfully executed/rebuilt. */
|
|
290
|
+
buildCount: number;
|
|
299
291
|
}
|
|
300
292
|
/**
|
|
301
293
|
* Context provided to the `use` callback within an artifact factory.
|
|
@@ -304,37 +296,37 @@ interface ArtifactDebugNode {
|
|
|
304
296
|
* @template TState The type of the global state managed by the DataStore.
|
|
305
297
|
*/
|
|
306
298
|
interface UseDependencyContext<TRegistry extends Record<string, any>, TState extends object> {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Resolves another artifact from the container and registers a dependency on it.
|
|
301
|
+
* If the dependency changes, the current artifact will be invalidated and rebuilt.
|
|
302
|
+
* @template K The key of the artifact to resolve.
|
|
303
|
+
* @param key The key of the artifact to resolve.
|
|
304
|
+
* @param params Parameters with which to resolve the artifact
|
|
305
|
+
* @returns A Promise that resolves to a `ResolvedArtifact` containing the instance
|
|
306
|
+
* or an external error.
|
|
307
|
+
* @throws {SystemError} if the key is missing or if resolving creates a circular dependency.
|
|
308
|
+
*/
|
|
309
|
+
resolve<K extends keyof TRegistry>(key: K, params?: any): Promise<ResolvedArtifact<TRegistry[K]>>;
|
|
310
|
+
/**
|
|
311
|
+
* Resolves another artifact from the container and registers a dependency on it.
|
|
312
|
+
* If the dependency changes, the current artifact will be invalidated and rebuilt.
|
|
313
|
+
* @template K The key of the artifact to resolve.
|
|
314
|
+
* @param key The key of the artifact to resolve.
|
|
315
|
+
* @param params Parameters with which to resolve the artifact
|
|
316
|
+
* @returns A Promise that resolves to a the resolved instance, unlike
|
|
317
|
+
* resolve, this will throw an error if resolution fails.
|
|
318
|
+
* @throws {SystemError} if any error occurs during resolution.
|
|
319
|
+
*/
|
|
320
|
+
require<K extends keyof TRegistry>(key: K, params?: any): Promise<TRegistry[K]>;
|
|
321
|
+
/**
|
|
322
|
+
* Selects a slice of the global state and registers a dependency on it.
|
|
323
|
+
* If the selected state slice changes (based on a deep comparison), the current
|
|
324
|
+
* artifact will be invalidated and rebuilt.
|
|
325
|
+
* @template S The type of the selected state slice.
|
|
326
|
+
* @param selector A function that takes the full state and returns a slice of state.
|
|
327
|
+
* @returns The selected slice of state.
|
|
328
|
+
*/
|
|
329
|
+
select<S>(selector: (state: TState) => S, options?: SubscribeOptions$1): S;
|
|
338
330
|
}
|
|
339
331
|
/**
|
|
340
332
|
* Context object provided to an artifact stream producer function (`ctx.stream` callback).
|
|
@@ -345,38 +337,38 @@ interface UseDependencyContext<TRegistry extends Record<string, any>, TState ext
|
|
|
345
337
|
* @template TArtifact The type of the artifact being streamed.
|
|
346
338
|
*/
|
|
347
339
|
type ArtifactStreamContext<TState, TArtifact> = {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
340
|
+
/**
|
|
341
|
+
* The current value of the artifact. This is `undefined` before the first value is emitted.
|
|
342
|
+
* Useful for diffing or migrating state during an update.
|
|
343
|
+
* @returns The current artifact value or `undefined`.
|
|
344
|
+
*/
|
|
345
|
+
value: () => TArtifact | undefined;
|
|
346
|
+
/**
|
|
347
|
+
* An AbortSignal that indicates if the stream has been cancelled or aborted
|
|
348
|
+
* from the consumer side (e.g., due to container disposal or artifact invalidation).
|
|
349
|
+
* Producers should check this signal and stop processing when it is set to prevent leaks.
|
|
350
|
+
*/
|
|
351
|
+
signal: AbortSignal;
|
|
352
|
+
/**
|
|
353
|
+
* A function to asynchronously emit a new value of the artifact to the container.
|
|
354
|
+
* Calling this function updates the artifact's resolved value, triggers invalidation
|
|
355
|
+
* for its dependents, and updates the `value()` property for subsequent emissions.
|
|
356
|
+
*
|
|
357
|
+
* @param value The new artifact value to emit.
|
|
358
|
+
* @returns A Promise that resolves when the value has been sent and dependents have been notified.
|
|
359
|
+
*/
|
|
360
|
+
emit: (value: TArtifact) => Promise<void>;
|
|
361
|
+
/**
|
|
362
|
+
* Dispatches a state update to the global store, analogous to a standard `setState`.
|
|
363
|
+
* **Note:** This does not automatically register a dependency for the current artifact.
|
|
364
|
+
* @param update A `StateUpdater` function or a partial state object.
|
|
365
|
+
* @param options Optional settings for the update, including `force` and `actionId`.
|
|
366
|
+
* @returns A Promise that resolves when the state update is complete.
|
|
367
|
+
*/
|
|
368
|
+
set(update: StateUpdater<TState>, options?: {
|
|
369
|
+
force?: boolean;
|
|
370
|
+
actionId?: string;
|
|
371
|
+
}): Promise<any>;
|
|
380
372
|
};
|
|
381
373
|
/**
|
|
382
374
|
* The full context provided to an artifact's factory function.
|
|
@@ -387,85 +379,115 @@ type ArtifactStreamContext<TState, TArtifact> = {
|
|
|
387
379
|
* @template TArtifact The type of the artifact being created by the factory.
|
|
388
380
|
*/
|
|
389
381
|
type ArtifactFactoryContext<TRegistry extends Record<string, any>, TState extends object, TArtifact, TExtra extends Record<string, any> = {}> = TExtra & {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
params?: any;
|
|
382
|
+
/**
|
|
383
|
+
* Returns the current global state object. This is a non-reactive read;
|
|
384
|
+
* changes to the state will not automatically invalidate the artifact
|
|
385
|
+
* unless explicitly selected via `ctx.use` and `ctx.select`.
|
|
386
|
+
* @returns The current global state.
|
|
387
|
+
*/
|
|
388
|
+
state(): TState;
|
|
389
|
+
/**
|
|
390
|
+
* The previous instance of the artifact if it's a Singleton and is being rebuilt
|
|
391
|
+
* after an invalidation. This is `undefined` on the initial build.
|
|
392
|
+
* Useful for diffing, migrating state, or reusing resources during an update.
|
|
393
|
+
*/
|
|
394
|
+
previous?: TArtifact;
|
|
395
|
+
/**
|
|
396
|
+
* Executes a callback within a dependency tracking context.
|
|
397
|
+
* Any `resolve` or `select` calls made inside this callback will register
|
|
398
|
+
* dependencies for the current artifact.
|
|
399
|
+
* @template R The return type of the callback.
|
|
400
|
+
* @param callback The function to execute to resolve dependencies.
|
|
401
|
+
* @returns A Promise resolving to the result of the callback.
|
|
402
|
+
*/
|
|
403
|
+
use<R>(callback: (ctx: UseDependencyContext<TRegistry, TState>) => R | Promise<R>): Promise<R>;
|
|
404
|
+
/**
|
|
405
|
+
* Registers a cleanup function to be executed when the **current instance** of the
|
|
406
|
+
* artifact is invalidated and before its new instance is built. This is useful
|
|
407
|
+
* for releasing resources (e.g., event listeners) specific to the *previous* instance.
|
|
408
|
+
* @param cleanup The cleanup function.
|
|
409
|
+
*/
|
|
410
|
+
onCleanup(cleanup: ArtifactCleanup): void;
|
|
411
|
+
/**
|
|
412
|
+
* Registers a dispose function to be executed when the artifact is
|
|
413
|
+
* permanently removed from the container or the container itself is disposed.
|
|
414
|
+
* This is for final, permanent resource release.
|
|
415
|
+
* @param callback The dispose function.
|
|
416
|
+
*/
|
|
417
|
+
onDispose(callback: ArtifactCleanup): void;
|
|
418
|
+
/**
|
|
419
|
+
* Starts a streaming process for a Singleton artifact.
|
|
420
|
+
* The callback receives an `ArtifactStreamContext` to emit values and manage the
|
|
421
|
+
* stream's lifecycle. It can return a cleanup function (or a Promise resolving
|
|
422
|
+
* to one) to handle resource disposal.
|
|
423
|
+
* * @param callback - The streaming logic implementation. Can be synchronous or
|
|
424
|
+
* asynchronous, optionally returning a cleanup function.
|
|
425
|
+
* @throws {SystemError} If called on a Transient artifact, as they do not
|
|
426
|
+
* support persistent streaming states.
|
|
427
|
+
*/
|
|
428
|
+
stream(callback: (ctx: ArtifactStreamContext<TState, TArtifact>) => (void | (() => void | Promise<void>)) | Promise<void | (() => void | Promise<void>)>): void;
|
|
429
|
+
stream(callback: (ctx: ArtifactStreamContext<TState, TArtifact>) => (void | (() => void | Promise<void>)) | Promise<void | (() => void | Promise<void>)>): void;
|
|
430
|
+
/**
|
|
431
|
+
* An AbortSignal that indicates if the artifacts has been unregistered
|
|
432
|
+
*/
|
|
433
|
+
signal: AbortSignal; /** For parameterized artifacts: the parameters that were passed during resolve/watch. */
|
|
434
|
+
params?: any;
|
|
444
435
|
};
|
|
445
436
|
/**
|
|
446
437
|
* A function that performs cleanup or disposal logic for an artifact, potentially asynchronously.
|
|
447
438
|
* Used for `onCleanup` and `onDispose` callbacks.
|
|
448
439
|
*/
|
|
449
440
|
type ArtifactCleanup = () => void | Promise<void>;
|
|
441
|
+
interface ArtifactLifecycleEventMap {
|
|
442
|
+
"build:start": {
|
|
443
|
+
key: string;
|
|
444
|
+
templateKey?: string;
|
|
445
|
+
};
|
|
446
|
+
"build:complete": {
|
|
447
|
+
key: string;
|
|
448
|
+
instance: unknown;
|
|
449
|
+
};
|
|
450
|
+
"build:error": {
|
|
451
|
+
key: string;
|
|
452
|
+
error: unknown;
|
|
453
|
+
};
|
|
454
|
+
"artifact:invalidated": {
|
|
455
|
+
key: string;
|
|
456
|
+
cascade: boolean;
|
|
457
|
+
replace: boolean;
|
|
458
|
+
};
|
|
459
|
+
"artifact:disposed": {
|
|
460
|
+
key: string;
|
|
461
|
+
};
|
|
462
|
+
"artifact:registered": {
|
|
463
|
+
key: string;
|
|
464
|
+
scope: ArtifactScope;
|
|
465
|
+
};
|
|
466
|
+
"stream:emit": {
|
|
467
|
+
key: string;
|
|
468
|
+
value: unknown;
|
|
469
|
+
};
|
|
470
|
+
"container:dispose": {};
|
|
471
|
+
}
|
|
450
472
|
/**
|
|
451
473
|
* Common properties shared across all possible states of a resolved artifact.
|
|
452
474
|
* @template TArtifact The type of the resolved artifact instance.
|
|
453
475
|
*/
|
|
454
476
|
interface ResolvedArtifactBase {
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
477
|
+
/**
|
|
478
|
+
* A function to manually trigger cleanup associated with this specific
|
|
479
|
+
* resolved instance. This is typically only relevant for Transient artifacts
|
|
480
|
+
* where the consumer is responsible for cleanup.
|
|
481
|
+
*/
|
|
482
|
+
cleanup?: ArtifactCleanup;
|
|
483
|
+
/**
|
|
484
|
+
* Manually invalidates this artifact, triggering its rebuild and
|
|
485
|
+
* cascading invalidations to its dependents.
|
|
486
|
+
* @param replace If `true`, forces immediate rebuild without debounce delay.
|
|
487
|
+
* @param fatal If `true`, the artifact will not be rebuilt until next resolve
|
|
488
|
+
* regardless of the lazy option during registration.
|
|
489
|
+
*/
|
|
490
|
+
invalidate(replace?: boolean, fatal?: boolean): Promise<void>;
|
|
469
491
|
}
|
|
470
492
|
/**
|
|
471
493
|
* State of an artifact that is successfully built and ready for use.
|
|
@@ -473,34 +495,34 @@ interface ResolvedArtifactBase {
|
|
|
473
495
|
* @template TArtifact The type of the resolved artifact instance.
|
|
474
496
|
*/
|
|
475
497
|
interface ReadyArtifact<TArtifact> extends ResolvedArtifactBase {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
498
|
+
/** The successfully resolved instance of the artifact. */
|
|
499
|
+
instance: TArtifact;
|
|
500
|
+
/** Indicates whether the artifact is ready and has an instance. */
|
|
501
|
+
ready: true;
|
|
502
|
+
error?: undefined;
|
|
481
503
|
}
|
|
482
504
|
/**
|
|
483
505
|
* State of an artifact that failed to build due to an external error.
|
|
484
506
|
* The instance is guaranteed to be absent.
|
|
485
507
|
*/
|
|
486
508
|
interface ErrorArtifact extends ResolvedArtifactBase {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
509
|
+
instance?: undefined;
|
|
510
|
+
ready: false;
|
|
511
|
+
/**
|
|
512
|
+
* Any runtime or external error that occurred during the artifact's factory
|
|
513
|
+
* execution (e.g., network fetch failed).
|
|
514
|
+
*/
|
|
515
|
+
error: any;
|
|
494
516
|
}
|
|
495
517
|
/**
|
|
496
518
|
* State of an artifact that is pending, idle, or currently building.
|
|
497
519
|
* Both instance and error are absent.
|
|
498
520
|
*/
|
|
499
521
|
interface PendingArtifact extends ResolvedArtifactBase {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
522
|
+
/** The instance is absent while pending or idle. Always undefined. */
|
|
523
|
+
instance?: undefined;
|
|
524
|
+
ready: false;
|
|
525
|
+
error?: undefined;
|
|
504
526
|
}
|
|
505
527
|
/**
|
|
506
528
|
* The result of an artifact resolution. This is a union type representing
|
|
@@ -512,9 +534,7 @@ interface PendingArtifact extends ResolvedArtifactBase {
|
|
|
512
534
|
* @template TArtifact The type of the resolved artifact instance.
|
|
513
535
|
*/
|
|
514
536
|
type ResolvedArtifact<TArtifact> = ReadyArtifact<TArtifact> | ErrorArtifact | PendingArtifact;
|
|
515
|
-
type KeyedResolvedArtifact<TRegistry, K extends keyof TRegistry> = ResolvedArtifact<TRegistry[K]> & {
|
|
516
|
-
[P in K]?: TRegistry[K];
|
|
517
|
-
};
|
|
537
|
+
type KeyedResolvedArtifact<TRegistry, K extends keyof TRegistry> = ResolvedArtifact<TRegistry[K]> & { [P in K]?: TRegistry[K] };
|
|
518
538
|
/**
|
|
519
539
|
* The factory function responsible for creating an artifact's instance.
|
|
520
540
|
* It receives an `ArtifactFactoryContext` to interact with the container
|
|
@@ -534,37 +554,37 @@ type ArtifactInstance<R> = R extends PromiseLike<infer T> ? T : R;
|
|
|
534
554
|
* @template TArtifact The type of the artifact being watched.
|
|
535
555
|
*/
|
|
536
556
|
interface ArtifactObserver<TRegistry, K extends keyof TRegistry> {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
557
|
+
/** The unique identifier (key) of the artifact being watched. */
|
|
558
|
+
id: string;
|
|
559
|
+
/**
|
|
560
|
+
* Number of active references (watchers) to this observer instance.
|
|
561
|
+
* Incremented on each `watch()` call, decremented on each `dispose()` call.
|
|
562
|
+
*/
|
|
563
|
+
count: number;
|
|
564
|
+
/**
|
|
565
|
+
* Retrieves the current `ResolvedArtifact` for the watched key.
|
|
566
|
+
* @param resolve Flag indicating whether we should resolve the artifact
|
|
567
|
+
* immediately. Defaults to `false`
|
|
568
|
+
* @returns The resolved artifact, including its instance and status.
|
|
569
|
+
*/
|
|
570
|
+
get(resolve?: boolean): KeyedResolvedArtifact<TRegistry, K>;
|
|
571
|
+
/**
|
|
572
|
+
* Subscribes a callback function to be invoked whenever the artifact's
|
|
573
|
+
* state (instance, error, or readiness) changes.
|
|
574
|
+
* @param callback The function to call on updates. It receives the new `ResolvedArtifact`.
|
|
575
|
+
* @param eager a boolean indicating whether we should immediately call the
|
|
576
|
+
* callback after subscription. Defaults to `true`
|
|
577
|
+
* @returns A function to unsubscribe the callback and stop receiving updates.
|
|
578
|
+
*/
|
|
579
|
+
subscribe(callback: (artifact: KeyedResolvedArtifact<TRegistry, K>) => void, eager?: boolean): () => void;
|
|
580
|
+
/**
|
|
581
|
+
* Resolves another artifact from the container and registers a dependency on it.
|
|
582
|
+
* If the dependency changes, the current artifact will be invalidated and rebuilt.
|
|
583
|
+
* @returns A Promise that resolves to a `ResolvedArtifact` containing the instance
|
|
584
|
+
* or an external error.
|
|
585
|
+
* @throws {SystemError} if resolution fails.
|
|
586
|
+
*/
|
|
587
|
+
resolve(): Promise<KeyedResolvedArtifact<TRegistry, K>>;
|
|
568
588
|
}
|
|
569
589
|
/**
|
|
570
590
|
* Configuration options for defining an artifact. These options control
|
|
@@ -574,41 +594,41 @@ interface ArtifactObserver<TRegistry, K extends keyof TRegistry> {
|
|
|
574
594
|
* @template TRegistry The type mapping of all artifacts in the container.
|
|
575
595
|
*/
|
|
576
596
|
interface ArtifactTemplate<TState extends object, TArtifact, TRegistry extends Record<string, any> = Record<string, any>, TExtra extends Record<string, any> = {}> {
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
597
|
+
/** The unique key identifying this artifact within the registry. */
|
|
598
|
+
key: keyof TRegistry;
|
|
599
|
+
/** The factory function responsible for creating the artifact's instance. */
|
|
600
|
+
factory: ArtifactFactory<TRegistry, TState, TArtifact, TExtra>;
|
|
601
|
+
/**
|
|
602
|
+
* The scope of the artifact, determining its lifecycle and sharing strategy.
|
|
603
|
+
* Defaults to `ArtifactScopes.Singleton`.
|
|
604
|
+
*/
|
|
605
|
+
scope?: ArtifactScope;
|
|
606
|
+
/**
|
|
607
|
+
* If `true` (default), the artifact's factory is executed only when the artifact
|
|
608
|
+
* is first requested (lazy instantiation). If `false`, the artifact is built
|
|
609
|
+
* immediately upon registration (only applies to Singleton scopes).
|
|
610
|
+
*/
|
|
611
|
+
lazy?: boolean;
|
|
612
|
+
/**
|
|
613
|
+
* Maximum time in milliseconds allowed for the artifact's factory function
|
|
614
|
+
* to complete execution. If exceeded, the factory will time out.
|
|
615
|
+
*/
|
|
616
|
+
timeout?: number;
|
|
617
|
+
/**
|
|
618
|
+
* Number of times to retry the artifact's factory on failure due to an
|
|
619
|
+
* external (runtime) error (e.g., an exception in an async dependency).
|
|
620
|
+
* SystemErrors (e.g., key not found) are not retried. Defaults to `0`.
|
|
621
|
+
*/
|
|
622
|
+
retries?: number;
|
|
623
|
+
/**
|
|
624
|
+
* Base debounce time in milliseconds for invalidation events originating
|
|
625
|
+
* from this artifact's dependencies. This delays the rebuild process to
|
|
626
|
+
* aggregate multiple rapid changes.
|
|
627
|
+
*/
|
|
628
|
+
debounce?: number;
|
|
629
|
+
/** If defined, the artifact is parameterized. Receives the user‑supplied params and returns a unique string key. */
|
|
630
|
+
paramKey?: (params: Record<string, unknown>) => string;
|
|
631
|
+
virtual?: true;
|
|
612
632
|
}
|
|
613
633
|
/**
|
|
614
634
|
* Extracts the global state type (`TState`) from an `ArtifactTemplate`.
|
|
@@ -622,13 +642,13 @@ type ArtifactRegistryType<T> = T extends ArtifactTemplate<any, any, infer TRegis
|
|
|
622
642
|
* Extracts the key type (`K`) from an `ArtifactTemplate` (as a string literal if possible).
|
|
623
643
|
*/
|
|
624
644
|
type ArtifactKey<T> = T extends {
|
|
625
|
-
|
|
645
|
+
key: infer K;
|
|
626
646
|
} ? K : never;
|
|
627
647
|
/**
|
|
628
648
|
* Extracts the scope type (`S`) from an `ArtifactTemplate`, defaulting to `ArtifactScope` if undefined.
|
|
629
649
|
*/
|
|
630
650
|
type ArtifactScopeType<T> = T extends {
|
|
631
|
-
|
|
651
|
+
scope: infer S;
|
|
632
652
|
} ? S extends ArtifactScope ? S : ArtifactScope : ArtifactScope;
|
|
633
653
|
/**
|
|
634
654
|
* Helper type that creates a properly typed template map where keys correspond to artifact names
|
|
@@ -636,9 +656,7 @@ type ArtifactScopeType<T> = T extends {
|
|
|
636
656
|
* @template TState The type of the global state.
|
|
637
657
|
* @template TRegistry The map of all artifact keys to their resolved types.
|
|
638
658
|
*/
|
|
639
|
-
type ArtifactTemplateMap<TState extends object, TRegistry extends Record<string, any> = Record<string, any>> = {
|
|
640
|
-
[K in keyof TRegistry]: ArtifactTemplate<TState, TRegistry[K], TRegistry>;
|
|
641
|
-
};
|
|
659
|
+
type ArtifactTemplateMap<TState extends object, TRegistry extends Record<string, any> = Record<string, any>> = { [K in keyof TRegistry]: ArtifactTemplate<TState, TRegistry[K], TRegistry> };
|
|
642
660
|
/**
|
|
643
661
|
* Extracts the artifact's resolved value type (`TArtifact`) from an object that structurally
|
|
644
662
|
* resembles an `ArtifactTemplate` (by inspecting the `factory` function's generic arguments).
|
|
@@ -646,7 +664,7 @@ type ArtifactTemplateMap<TState extends object, TRegistry extends Record<string,
|
|
|
646
664
|
* @template T The type that contains a factory property.
|
|
647
665
|
*/
|
|
648
666
|
type ArtifactValue<T> = T extends {
|
|
649
|
-
|
|
667
|
+
factory: ArtifactFactory<any, any, infer TArtifact, any>;
|
|
650
668
|
} ? TArtifact : never;
|
|
651
669
|
/**
|
|
652
670
|
* Infers the complete Artifact Registry type from a map of artifact configuration objects.
|
|
@@ -657,186 +675,99 @@ type ArtifactValue<T> = T extends {
|
|
|
657
675
|
* @template T A map where keys are artifact names and values are their configurations.
|
|
658
676
|
*/
|
|
659
677
|
type InferRegistry<T extends Record<string, {
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
}>> = {
|
|
663
|
-
[K in keyof T]: ArtifactValue<T[K]>;
|
|
664
|
-
};
|
|
678
|
+
factory: (...args: any[]) => any;
|
|
679
|
+
[key: string]: any;
|
|
680
|
+
}>> = { [K in keyof T]: ArtifactValue<T[K]> };
|
|
665
681
|
/**
|
|
666
682
|
* State paths grouped by options passed to the store
|
|
667
683
|
*/
|
|
668
684
|
interface StateGroup {
|
|
669
|
-
|
|
670
|
-
|
|
685
|
+
paths: string[];
|
|
686
|
+
options?: SubscribeOptions$1;
|
|
671
687
|
}
|
|
672
688
|
interface ExportedArtifact {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
689
|
+
key: string;
|
|
690
|
+
instance: any;
|
|
691
|
+
state: {
|
|
692
|
+
groups: Array<{
|
|
693
|
+
paths: string[];
|
|
694
|
+
options?: SubscribeOptions$1;
|
|
695
|
+
}>;
|
|
696
|
+
hash: string;
|
|
697
|
+
};
|
|
698
|
+
dependencies: string[];
|
|
683
699
|
}
|
|
684
700
|
interface ExportedContainerState {
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
701
|
+
version: string;
|
|
702
|
+
timestamp: number;
|
|
703
|
+
artifacts: ExportedArtifact[];
|
|
704
|
+
checksum: string;
|
|
689
705
|
}
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
* A dependency injection container for managing the lifecycle, dependencies,
|
|
693
|
-
* and instances of "artifacts" (any JavaScript/TypeScript object or value).
|
|
694
|
-
*
|
|
695
|
-
* Separated concerns:
|
|
696
|
-
* - ArtifactRegistry: Stores artifact templates (factories + options)
|
|
697
|
-
* - ArtifactCache: Stores resolved singleton instances
|
|
698
|
-
* - ArtifactDependencyGraph: Tracks artifact dependencies using DependencyGraph
|
|
699
|
-
* - ArtifactManager: Handles lifecycle (build, invalidate, dispose)
|
|
700
|
-
* - ArtifactObserverManager: Manages watchers and subscriptions
|
|
701
|
-
*
|
|
702
|
-
* @template TRegistry A type that maps artifact keys to their types
|
|
703
|
-
* @template TState The type of the global state object
|
|
704
|
-
*/
|
|
706
|
+
//#endregion
|
|
707
|
+
//#region src/artifacts/container.d.ts
|
|
705
708
|
declare class ArtifactContainer<TRegistry extends Record<string, any> = Record<string, any>, TState extends object = any> {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
*/
|
|
745
|
-
has<K extends keyof TRegistry>(key: K): boolean;
|
|
746
|
-
/**
|
|
747
|
-
* Unregisters an artifact from the container, disposing of its current instance
|
|
748
|
-
* and removing all associated resources and dependency links.
|
|
749
|
-
*
|
|
750
|
-
* Also evicts the observer watcher cache entry so singleton watchers do not
|
|
751
|
-
* outlive the artifact's registration.
|
|
752
|
-
*
|
|
753
|
-
* @param key The unique identifier of the artifact
|
|
754
|
-
*/
|
|
755
|
-
unregister<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): Promise<void>;
|
|
756
|
-
/**
|
|
757
|
-
* Resolves an artifact by its key, returning its instance or an error.
|
|
758
|
-
* Handles dependency resolution, cycle detection, caching, and retry logic.
|
|
759
|
-
*
|
|
760
|
-
* The keyed index property (`artifact[key]`) is set inside `cache.package()`
|
|
761
|
-
* so it is always consistent with `artifact.instance`. No post-hoc mutation
|
|
762
|
-
* is needed here.
|
|
763
|
-
*
|
|
764
|
-
* @param key The unique identifier for the artifact
|
|
765
|
-
* @param params Parameters with which to resolve the artifact
|
|
766
|
-
* @returns A Promise that resolves to a ResolvedArtifact
|
|
767
|
-
* @throws {ArtifactNotFoundError} if the artifact is not found
|
|
768
|
-
*/
|
|
769
|
-
resolve<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): Promise<KeyedResolvedArtifact<TRegistry, K>>;
|
|
770
|
-
/**
|
|
771
|
-
* Resolves an artifact by its key, returning its instance directly.
|
|
772
|
-
* Throws if resolution fails.
|
|
773
|
-
*
|
|
774
|
-
* @param key The unique identifier for the artifact
|
|
775
|
-
* @param params Parameters with which to resolve the artifact
|
|
776
|
-
* @returns A Promise that resolves to the artifact instance
|
|
777
|
-
* @throws {ArtifactNotFoundError} if the artifact is not found
|
|
778
|
-
* @throws the artifact's error if resolution failed
|
|
779
|
-
*/
|
|
780
|
-
require<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): Promise<TRegistry[K]>;
|
|
781
|
-
/**
|
|
782
|
-
* Returns an ArtifactObserver for a given artifact key.
|
|
783
|
-
* The observer allows subscribing to changes in the artifact's resolved value.
|
|
784
|
-
*
|
|
785
|
-
* @param key The unique identifier for the artifact
|
|
786
|
-
* @param params Parameters with which to resolve the artifact
|
|
787
|
-
* @param ttl Delay before the watcher is cleaned up if we have no subscriber
|
|
788
|
-
* @returns An ArtifactObserver instance
|
|
789
|
-
*/
|
|
790
|
-
watch<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>, ttl?: number): ArtifactObserver<TRegistry, K>;
|
|
791
|
-
/**
|
|
792
|
-
* Peeks at the resolved instance of an artifact without triggering resolution
|
|
793
|
-
* or registering a dependency.
|
|
794
|
-
*
|
|
795
|
-
* @param key The unique identifier for the artifact
|
|
796
|
-
* @returns The artifact instance if already built, otherwise undefined
|
|
797
|
-
*/
|
|
798
|
-
peek<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): TRegistry[K] | undefined;
|
|
799
|
-
/**
|
|
800
|
-
* Invalidates an artifact, triggering rebuild and cascade to dependents.
|
|
801
|
-
*
|
|
802
|
-
* @param key The artifact key to invalidate
|
|
803
|
-
* @param options.replace If true, forces immediate rebuild bypassing debounce
|
|
804
|
-
* @param options.params for parametized artifacts
|
|
805
|
-
*/
|
|
806
|
-
invalidate<K extends keyof TRegistry>(key: K, options?: {
|
|
807
|
-
replace?: boolean;
|
|
808
|
-
params?: Record<string, unknown>;
|
|
809
|
-
}): Promise<void>;
|
|
810
|
-
/**
|
|
811
|
-
* Notifies observers that an artifact has changed.
|
|
812
|
-
* Called by ArtifactManager during stream propagation.
|
|
813
|
-
*
|
|
814
|
-
* @param key The artifact key
|
|
815
|
-
*/
|
|
816
|
-
notifyObservers(key: string): void;
|
|
817
|
-
/**
|
|
818
|
-
* Checks if an artifact has active watchers.
|
|
819
|
-
* Called by ArtifactManager to determine if lazy artifacts should rebuild.
|
|
820
|
-
*
|
|
821
|
-
* @param key The artifact key
|
|
822
|
-
* @returns True if the artifact has active watchers
|
|
823
|
-
*/
|
|
824
|
-
hasWatchers(key: string): boolean;
|
|
825
|
-
/**
|
|
826
|
-
* Disposes of the entire container and all artifacts registered within it.
|
|
827
|
-
* Releases all resources, stops all watchers, and clears all internal state.
|
|
828
|
-
*
|
|
829
|
-
* Returns a Promise that resolves once all artifact teardowns have settled.
|
|
830
|
-
* Callers should await this method to guarantee full resource release.
|
|
831
|
-
*/
|
|
832
|
-
dispose(): Promise<void>;
|
|
833
|
-
export(): Promise<ExportedContainerState>;
|
|
834
|
-
private restore;
|
|
835
|
-
static from<TRegistry extends Record<string, any> = Record<string, any>, TState extends object = any>(options: {
|
|
836
|
-
store: Pick<DataStore<TState>, "watch" | "get" | "set" | "subset">;
|
|
837
|
-
bundle?: ExportedContainerState;
|
|
838
|
-
templates: ArtifactTemplate<TState, any, TRegistry>[];
|
|
839
|
-
}): Promise<ArtifactContainer<TRegistry, TState>>;
|
|
709
|
+
private readonly registry;
|
|
710
|
+
private readonly cache;
|
|
711
|
+
private readonly graph;
|
|
712
|
+
private readonly manager;
|
|
713
|
+
private readonly observer;
|
|
714
|
+
private readonly logger;
|
|
715
|
+
private readonly store;
|
|
716
|
+
readonly events: EventBus<ArtifactLifecycleEventMap>;
|
|
717
|
+
constructor(store: Pick<DataStore<TState>, "watch" | "get" | "set" | "subset">, options?: {
|
|
718
|
+
logger?: SystemLogger;
|
|
719
|
+
});
|
|
720
|
+
debugInfo(): ArtifactDebugNode[];
|
|
721
|
+
register<K extends keyof TRegistry>(params: ArtifactTemplate<TState, TRegistry[K], TRegistry>): () => void;
|
|
722
|
+
has<K extends keyof TRegistry>(key: K): boolean;
|
|
723
|
+
unregister<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): Promise<void>;
|
|
724
|
+
resolve<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): Promise<KeyedResolvedArtifact<TRegistry, K>>;
|
|
725
|
+
require<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): Promise<TRegistry[K]>;
|
|
726
|
+
watch<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>, ttl?: number): ArtifactObserver<TRegistry, K>;
|
|
727
|
+
peek<K extends keyof TRegistry>(key: K, params?: Record<string, unknown>): TRegistry[K] | undefined;
|
|
728
|
+
invalidate<K extends keyof TRegistry>(key: K, options?: {
|
|
729
|
+
replace?: boolean;
|
|
730
|
+
params?: Record<string, unknown>;
|
|
731
|
+
}): Promise<void>;
|
|
732
|
+
on<TEventName extends keyof ArtifactLifecycleEventMap>(eventName: TEventName, callback: (payload: ArtifactLifecycleEventMap[TEventName]) => void): () => void;
|
|
733
|
+
on(eventName: "*", callback: (payload: ArtifactLifecycleEventMap[keyof ArtifactLifecycleEventMap], event: keyof ArtifactLifecycleEventMap) => void): () => void;
|
|
734
|
+
once<TEventName extends keyof ArtifactLifecycleEventMap>(eventName: TEventName, callback: (payload: ArtifactLifecycleEventMap[TEventName]) => void): () => void;
|
|
735
|
+
once(eventName: "*", callback: (payload: ArtifactLifecycleEventMap[keyof ArtifactLifecycleEventMap], event: keyof ArtifactLifecycleEventMap) => void): () => void;
|
|
736
|
+
notifyObservers(key: string): void;
|
|
737
|
+
hasWatchers(key: string): boolean;
|
|
738
|
+
dispose(): Promise<void>;
|
|
739
|
+
export(): Promise<ExportedContainerState>;
|
|
740
|
+
private restore;
|
|
741
|
+
static from<TRegistry extends Record<string, any> = Record<string, any>, TState extends object = any>(options: {
|
|
742
|
+
store: Pick<DataStore<TState>, "watch" | "get" | "set" | "subset">;
|
|
743
|
+
bundle?: ExportedContainerState;
|
|
744
|
+
templates: ArtifactTemplate<TState, any, TRegistry>[];
|
|
745
|
+
logger?: SystemLogger;
|
|
746
|
+
}): Promise<ArtifactContainer<TRegistry, TState>>;
|
|
840
747
|
}
|
|
841
|
-
|
|
842
|
-
|
|
748
|
+
//#endregion
|
|
749
|
+
//#region src/artifacts/errors.d.ts
|
|
750
|
+
declare const ErrorCodes: {
|
|
751
|
+
readonly NOT_FOUND: "DB-001-NF";
|
|
752
|
+
readonly DUPLICATE_KEY: "DB-002-DUP";
|
|
753
|
+
readonly INVALID_COMMAND: "BUS-001";
|
|
754
|
+
readonly INTERNAL_ERROR: "SYS-001";
|
|
755
|
+
readonly CONCURRENCY_ERROR: "CON-001";
|
|
756
|
+
};
|
|
757
|
+
declare function artifactNotFound(key: string): SystemError$1;
|
|
758
|
+
declare function cycleDetected(path: string[]): SystemError$1;
|
|
759
|
+
declare function illegalScope(message: string): SystemError$1;
|
|
760
|
+
declare function watcherDisposed(key: string): SystemError$1;
|
|
761
|
+
declare function timeoutError(message?: string): SystemError$1;
|
|
762
|
+
declare function keyConflict(key?: string): SystemError$1;
|
|
763
|
+
declare function notParameterized(key: string): SystemError$1;
|
|
764
|
+
declare function paramKeyCollision(key: string, computedKey: string): SystemError$1;
|
|
765
|
+
declare function selfDependency(key: string): SystemError$1;
|
|
766
|
+
declare function buildStaleAfterRetries(depKey: string): SystemError$1;
|
|
767
|
+
declare function invalidImport(message: string): SystemError$1;
|
|
768
|
+
declare function invalidExport(key: string): SystemError$1;
|
|
769
|
+
declare class SupersededBuildError extends Error {
|
|
770
|
+
constructor();
|
|
771
|
+
}
|
|
772
|
+
//#endregion
|
|
773
|
+
export { ArtifactCleanup, ArtifactContainer, ArtifactDebugNode, ArtifactFactory, ArtifactFactoryContext, ArtifactInstance, ArtifactKey, ArtifactLifecycleEventMap, ArtifactObserver, ArtifactRegistryType, ArtifactScope, ArtifactScopeType, ArtifactScopes, ArtifactStateType, ArtifactStreamContext, ArtifactTemplate, ArtifactTemplateMap, ArtifactValue, ErrorArtifact, ErrorCodes, ExportedArtifact, ExportedContainerState, InferRegistry, type Issue, KeyedResolvedArtifact, PendingArtifact, ReadyArtifact, ResolvedArtifact, ResolvedArtifactBase, type Severity, StateGroup, SupersededBuildError, SystemError, UseDependencyContext, artifactNotFound, buildStaleAfterRetries, cycleDetected, illegalScope, invalidExport, invalidImport, keyConflict, notParameterized, paramKeyCollision, selfDependency, timeoutError, watcherDisposed };
|