@asaidimu/utils-artifacts 3.1.3 → 3.1.4
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.mts +99 -16
- package/index.d.ts +99 -16
- package/package.json +1 -1
package/index.d.mts
CHANGED
|
@@ -1,38 +1,57 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Utility type for representing partial updates to the state.
|
|
2
|
+
* Utility type for representing partial updates to the state, allowing deep nesting.
|
|
3
|
+
* It makes all properties optional and applies the same transformation recursively
|
|
4
|
+
* to nested objects and array elements, allowing for selective updates while
|
|
5
|
+
* preserving the original structure. It also includes the original type T and
|
|
6
|
+
* undefined as possibilities for the top level and nested values.
|
|
3
7
|
*/
|
|
4
8
|
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 : {
|
|
5
9
|
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> | undefined : T[K] | undefined;
|
|
6
10
|
} | undefined | T : T | undefined;
|
|
7
11
|
/**
|
|
8
|
-
* Extended store state for monitoring execution status
|
|
12
|
+
* Extended store state for monitoring the current execution status (e.g., if an update is in progress).
|
|
9
13
|
*/
|
|
10
14
|
interface StoreExecutionState<T> {
|
|
15
|
+
/** Indicates if a state update process is currently executing. */
|
|
11
16
|
executing: boolean;
|
|
17
|
+
/** The changes (DeepPartial) currently being processed in the execution cycle. Null if none. */
|
|
12
18
|
changes: DeepPartial<T> | null;
|
|
19
|
+
/** A queue of pending state update functions/objects to be applied sequentially. */
|
|
13
20
|
pendingChanges: Array<StateUpdater<T>>;
|
|
21
|
+
/** Names of all currently registered middlewares. */
|
|
14
22
|
middlewares: string[];
|
|
23
|
+
/** Details of the middleware currently running. Null if none. */
|
|
15
24
|
runningMiddleware: {
|
|
16
25
|
id: string;
|
|
17
26
|
name: string;
|
|
18
27
|
startTime: number;
|
|
19
28
|
} | null;
|
|
29
|
+
/** Indicates if the store is currently within an active transaction block. */
|
|
20
30
|
transactionActive: boolean;
|
|
21
31
|
}
|
|
22
32
|
/**
|
|
23
|
-
* Event types emitted by the state for observability
|
|
33
|
+
* Event types emitted by the state store for observability and debugging.
|
|
24
34
|
*/
|
|
25
35
|
type StoreEvent = "update:start" | "update:complete" | "middleware:start" | "middleware:complete" | "middleware:error" | "middleware:blocked" | "middleware:executed" | "transaction:start" | "transaction:complete" | "transaction:error" | "persistence:ready" | "persistence:queued" | "persistence:success" | "persistence:retry" | "persistence:failed" | "persistence:queue_cleared" | "persistence:init_error" | "action:start" | "action:complete" | "action:error" | "selector:accessed" | "selector:changed";
|
|
26
36
|
/**
|
|
27
|
-
* Represents a state update, which can be
|
|
37
|
+
* Represents a state update, which can be:
|
|
38
|
+
* 1. The full new state (`T`).
|
|
39
|
+
* 2. A partial update object (`DeepPartial<T>`).
|
|
40
|
+
* 3. A function that receives the current state and returns a partial update (sync or async).
|
|
28
41
|
*/
|
|
29
42
|
type StateUpdater<T> = T | DeepPartial<T> | ((state: T) => DeepPartial<T> | Promise<DeepPartial<T>>);
|
|
30
43
|
/**
|
|
31
44
|
* Core types for the reactive data store
|
|
32
45
|
*/
|
|
46
|
+
/**
|
|
47
|
+
* Type for a Transform Middleware function.
|
|
48
|
+
* It modifies (transforms) the incoming changes and must return a `DeepPartial<T>`.
|
|
49
|
+
*/
|
|
33
50
|
type TransformMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<DeepPartial<T>> | DeepPartial<T>;
|
|
34
51
|
/**
|
|
35
|
-
* Type for
|
|
52
|
+
* Type for a Blocking Middleware function.
|
|
53
|
+
* It determines whether the state update should proceed or be blocked.
|
|
54
|
+
* It returns a boolean or an object containing a `block` boolean and an optional `error`.
|
|
36
55
|
*/
|
|
37
56
|
type BlockingMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<boolean | {
|
|
38
57
|
block: boolean;
|
|
@@ -42,23 +61,46 @@ type BlockingMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<bool
|
|
|
42
61
|
error?: Error;
|
|
43
62
|
};
|
|
44
63
|
/**
|
|
45
|
-
* Type representing the configuration object passed to `use
|
|
64
|
+
* Type representing the configuration object passed to the `use` method to register a middleware.
|
|
46
65
|
*/
|
|
47
66
|
interface MiddlewareConfig<T> {
|
|
67
|
+
/** The middleware function (can be a transform or blocking middleware). */
|
|
48
68
|
action: TransformMiddleware<T> | BlockingMiddleware<T>;
|
|
69
|
+
/** An optional, human-readable name for the middleware. */
|
|
49
70
|
name?: string;
|
|
71
|
+
/** If true, the middleware is treated as a blocking middleware (must return a boolean or `{ block: boolean }`). */
|
|
50
72
|
block?: boolean;
|
|
51
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Interface for a reactive selector result, providing access to the value and subscription capabilities.
|
|
76
|
+
*/
|
|
52
77
|
interface ReactiveSelector<S> {
|
|
78
|
+
/** Unique identifier for the selector. */
|
|
53
79
|
id: string;
|
|
80
|
+
/** Function to get the current computed value of the selector. */
|
|
54
81
|
get: () => S;
|
|
82
|
+
/**
|
|
83
|
+
* Subscribes a callback function to run whenever the selector's result changes.
|
|
84
|
+
* Returns an unsubscribe function.
|
|
85
|
+
*/
|
|
55
86
|
subscribe: (callback: (state: S) => void) => () => void;
|
|
56
87
|
}
|
|
57
88
|
/**
|
|
58
|
-
* Interface defining the contract for the data state.
|
|
89
|
+
* Interface defining the contract for the core data state store.
|
|
90
|
+
* T must be an object type.
|
|
59
91
|
*/
|
|
60
92
|
interface DataStore<T extends object> {
|
|
93
|
+
/**
|
|
94
|
+
* Gets the current state of the store.
|
|
95
|
+
* @param clone If true, returns a deep clone of the state; otherwise, returns the internal state reference.
|
|
96
|
+
* @returns The current state T.
|
|
97
|
+
*/
|
|
61
98
|
get(clone?: boolean): T;
|
|
99
|
+
/**
|
|
100
|
+
* Registers a named action function that can modify the state.
|
|
101
|
+
* @param action The action configuration object.
|
|
102
|
+
* @returns A function to unregister the action.
|
|
103
|
+
*/
|
|
62
104
|
register<R extends any[]>(action: {
|
|
63
105
|
name: string;
|
|
64
106
|
fn: (state: T, ...args: R) => DeepPartial<T> | Promise<DeepPartial<T>>;
|
|
@@ -67,26 +109,67 @@ interface DataStore<T extends object> {
|
|
|
67
109
|
condition?: (previous: R, current: R) => boolean;
|
|
68
110
|
};
|
|
69
111
|
}): () => void;
|
|
112
|
+
/**
|
|
113
|
+
* Executes (dispatches) a previously registered action by its name.
|
|
114
|
+
* @param name The name of the action.
|
|
115
|
+
* @param args The parameters to pass to the action function.
|
|
116
|
+
* @returns A promise that resolves to the final state after the action and subsequent updates are complete.
|
|
117
|
+
*/
|
|
70
118
|
dispatch<R extends any[]>(name: string, ...args: R): Promise<T>;
|
|
119
|
+
/**
|
|
120
|
+
* Sets or updates the state using a StateUpdater.
|
|
121
|
+
* @param update The new state, partial state, or a function returning a partial state.
|
|
122
|
+
* @param options Configuration options for the set operation.
|
|
123
|
+
* @returns A promise that resolves to the current state when the update is complete.
|
|
124
|
+
*/
|
|
71
125
|
set(update: StateUpdater<T>, options?: {
|
|
72
126
|
force?: boolean;
|
|
73
127
|
actionId?: string;
|
|
74
|
-
}): Promise<
|
|
128
|
+
}): Promise<T>;
|
|
129
|
+
/**
|
|
130
|
+
* Creates a reactive selector that computes a derived value and tracks dependencies.
|
|
131
|
+
* @param selector A function to compute the derived state value S from the full state T.
|
|
132
|
+
* @returns A `ReactiveSelector<S>` object.
|
|
133
|
+
*/
|
|
75
134
|
select<S>(selector: (state: T) => S): ReactiveSelector<S>;
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Subscribes a callback to run when the data at the specified path(s) changes.
|
|
137
|
+
* @param path A single path string or an array of path strings to watch.
|
|
138
|
+
* @param callback The function to execute when a change occurs in the watched path(s).
|
|
139
|
+
* @returns An unsubscribe function.
|
|
140
|
+
*/
|
|
80
141
|
watch(path: string | Array<string>, callback: (state: T) => void): () => void;
|
|
142
|
+
/**
|
|
143
|
+
* Executes an operation function within a transaction block.
|
|
144
|
+
* All state updates (`set` or actions) within the transaction are batched and applied atomically (all or nothing).
|
|
145
|
+
* @param operation The function containing the state updates.
|
|
146
|
+
* @returns A promise that resolves to the return value of the operation function.
|
|
147
|
+
*/
|
|
81
148
|
transaction<R>(operation: () => R | Promise<R>): Promise<R>;
|
|
149
|
+
/**
|
|
150
|
+
* Registers a middleware function to intercept state updates.
|
|
151
|
+
* @param props The middleware configuration.
|
|
152
|
+
* @returns A function to unregister the middleware.
|
|
153
|
+
*/
|
|
82
154
|
use(props: MiddlewareConfig<T>): () => boolean;
|
|
83
|
-
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
|
|
155
|
+
/**
|
|
156
|
+
* Subscribes a listener to a specific store event type.
|
|
157
|
+
* @param event The type of store event to listen for.
|
|
158
|
+
* @param listener The callback function to execute when the event fires.
|
|
159
|
+
* @returns An unsubscribe function.
|
|
160
|
+
*/
|
|
87
161
|
on(event: StoreEvent, listener: (data: any) => void): () => void;
|
|
162
|
+
/**
|
|
163
|
+
* Returns the unique identifier of the store instance.
|
|
164
|
+
*/
|
|
88
165
|
id(): string;
|
|
166
|
+
/**
|
|
167
|
+
* Checks whether the store is fully initialized and ready for use.
|
|
168
|
+
*/
|
|
89
169
|
isReady(): boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Returns a readonly snapshot of the current execution state of the store.
|
|
172
|
+
*/
|
|
90
173
|
state(): Readonly<StoreExecutionState<T>>;
|
|
91
174
|
}
|
|
92
175
|
|
package/index.d.ts
CHANGED
|
@@ -1,38 +1,57 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Utility type for representing partial updates to the state.
|
|
2
|
+
* Utility type for representing partial updates to the state, allowing deep nesting.
|
|
3
|
+
* It makes all properties optional and applies the same transformation recursively
|
|
4
|
+
* to nested objects and array elements, allowing for selective updates while
|
|
5
|
+
* preserving the original structure. It also includes the original type T and
|
|
6
|
+
* undefined as possibilities for the top level and nested values.
|
|
3
7
|
*/
|
|
4
8
|
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 : {
|
|
5
9
|
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> | undefined : T[K] | undefined;
|
|
6
10
|
} | undefined | T : T | undefined;
|
|
7
11
|
/**
|
|
8
|
-
* Extended store state for monitoring execution status
|
|
12
|
+
* Extended store state for monitoring the current execution status (e.g., if an update is in progress).
|
|
9
13
|
*/
|
|
10
14
|
interface StoreExecutionState<T> {
|
|
15
|
+
/** Indicates if a state update process is currently executing. */
|
|
11
16
|
executing: boolean;
|
|
17
|
+
/** The changes (DeepPartial) currently being processed in the execution cycle. Null if none. */
|
|
12
18
|
changes: DeepPartial<T> | null;
|
|
19
|
+
/** A queue of pending state update functions/objects to be applied sequentially. */
|
|
13
20
|
pendingChanges: Array<StateUpdater<T>>;
|
|
21
|
+
/** Names of all currently registered middlewares. */
|
|
14
22
|
middlewares: string[];
|
|
23
|
+
/** Details of the middleware currently running. Null if none. */
|
|
15
24
|
runningMiddleware: {
|
|
16
25
|
id: string;
|
|
17
26
|
name: string;
|
|
18
27
|
startTime: number;
|
|
19
28
|
} | null;
|
|
29
|
+
/** Indicates if the store is currently within an active transaction block. */
|
|
20
30
|
transactionActive: boolean;
|
|
21
31
|
}
|
|
22
32
|
/**
|
|
23
|
-
* Event types emitted by the state for observability
|
|
33
|
+
* Event types emitted by the state store for observability and debugging.
|
|
24
34
|
*/
|
|
25
35
|
type StoreEvent = "update:start" | "update:complete" | "middleware:start" | "middleware:complete" | "middleware:error" | "middleware:blocked" | "middleware:executed" | "transaction:start" | "transaction:complete" | "transaction:error" | "persistence:ready" | "persistence:queued" | "persistence:success" | "persistence:retry" | "persistence:failed" | "persistence:queue_cleared" | "persistence:init_error" | "action:start" | "action:complete" | "action:error" | "selector:accessed" | "selector:changed";
|
|
26
36
|
/**
|
|
27
|
-
* Represents a state update, which can be
|
|
37
|
+
* Represents a state update, which can be:
|
|
38
|
+
* 1. The full new state (`T`).
|
|
39
|
+
* 2. A partial update object (`DeepPartial<T>`).
|
|
40
|
+
* 3. A function that receives the current state and returns a partial update (sync or async).
|
|
28
41
|
*/
|
|
29
42
|
type StateUpdater<T> = T | DeepPartial<T> | ((state: T) => DeepPartial<T> | Promise<DeepPartial<T>>);
|
|
30
43
|
/**
|
|
31
44
|
* Core types for the reactive data store
|
|
32
45
|
*/
|
|
46
|
+
/**
|
|
47
|
+
* Type for a Transform Middleware function.
|
|
48
|
+
* It modifies (transforms) the incoming changes and must return a `DeepPartial<T>`.
|
|
49
|
+
*/
|
|
33
50
|
type TransformMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<DeepPartial<T>> | DeepPartial<T>;
|
|
34
51
|
/**
|
|
35
|
-
* Type for
|
|
52
|
+
* Type for a Blocking Middleware function.
|
|
53
|
+
* It determines whether the state update should proceed or be blocked.
|
|
54
|
+
* It returns a boolean or an object containing a `block` boolean and an optional `error`.
|
|
36
55
|
*/
|
|
37
56
|
type BlockingMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<boolean | {
|
|
38
57
|
block: boolean;
|
|
@@ -42,23 +61,46 @@ type BlockingMiddleware<T> = (state: T, changes: DeepPartial<T>) => Promise<bool
|
|
|
42
61
|
error?: Error;
|
|
43
62
|
};
|
|
44
63
|
/**
|
|
45
|
-
* Type representing the configuration object passed to `use
|
|
64
|
+
* Type representing the configuration object passed to the `use` method to register a middleware.
|
|
46
65
|
*/
|
|
47
66
|
interface MiddlewareConfig<T> {
|
|
67
|
+
/** The middleware function (can be a transform or blocking middleware). */
|
|
48
68
|
action: TransformMiddleware<T> | BlockingMiddleware<T>;
|
|
69
|
+
/** An optional, human-readable name for the middleware. */
|
|
49
70
|
name?: string;
|
|
71
|
+
/** If true, the middleware is treated as a blocking middleware (must return a boolean or `{ block: boolean }`). */
|
|
50
72
|
block?: boolean;
|
|
51
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Interface for a reactive selector result, providing access to the value and subscription capabilities.
|
|
76
|
+
*/
|
|
52
77
|
interface ReactiveSelector<S> {
|
|
78
|
+
/** Unique identifier for the selector. */
|
|
53
79
|
id: string;
|
|
80
|
+
/** Function to get the current computed value of the selector. */
|
|
54
81
|
get: () => S;
|
|
82
|
+
/**
|
|
83
|
+
* Subscribes a callback function to run whenever the selector's result changes.
|
|
84
|
+
* Returns an unsubscribe function.
|
|
85
|
+
*/
|
|
55
86
|
subscribe: (callback: (state: S) => void) => () => void;
|
|
56
87
|
}
|
|
57
88
|
/**
|
|
58
|
-
* Interface defining the contract for the data state.
|
|
89
|
+
* Interface defining the contract for the core data state store.
|
|
90
|
+
* T must be an object type.
|
|
59
91
|
*/
|
|
60
92
|
interface DataStore<T extends object> {
|
|
93
|
+
/**
|
|
94
|
+
* Gets the current state of the store.
|
|
95
|
+
* @param clone If true, returns a deep clone of the state; otherwise, returns the internal state reference.
|
|
96
|
+
* @returns The current state T.
|
|
97
|
+
*/
|
|
61
98
|
get(clone?: boolean): T;
|
|
99
|
+
/**
|
|
100
|
+
* Registers a named action function that can modify the state.
|
|
101
|
+
* @param action The action configuration object.
|
|
102
|
+
* @returns A function to unregister the action.
|
|
103
|
+
*/
|
|
62
104
|
register<R extends any[]>(action: {
|
|
63
105
|
name: string;
|
|
64
106
|
fn: (state: T, ...args: R) => DeepPartial<T> | Promise<DeepPartial<T>>;
|
|
@@ -67,26 +109,67 @@ interface DataStore<T extends object> {
|
|
|
67
109
|
condition?: (previous: R, current: R) => boolean;
|
|
68
110
|
};
|
|
69
111
|
}): () => void;
|
|
112
|
+
/**
|
|
113
|
+
* Executes (dispatches) a previously registered action by its name.
|
|
114
|
+
* @param name The name of the action.
|
|
115
|
+
* @param args The parameters to pass to the action function.
|
|
116
|
+
* @returns A promise that resolves to the final state after the action and subsequent updates are complete.
|
|
117
|
+
*/
|
|
70
118
|
dispatch<R extends any[]>(name: string, ...args: R): Promise<T>;
|
|
119
|
+
/**
|
|
120
|
+
* Sets or updates the state using a StateUpdater.
|
|
121
|
+
* @param update The new state, partial state, or a function returning a partial state.
|
|
122
|
+
* @param options Configuration options for the set operation.
|
|
123
|
+
* @returns A promise that resolves to the current state when the update is complete.
|
|
124
|
+
*/
|
|
71
125
|
set(update: StateUpdater<T>, options?: {
|
|
72
126
|
force?: boolean;
|
|
73
127
|
actionId?: string;
|
|
74
|
-
}): Promise<
|
|
128
|
+
}): Promise<T>;
|
|
129
|
+
/**
|
|
130
|
+
* Creates a reactive selector that computes a derived value and tracks dependencies.
|
|
131
|
+
* @param selector A function to compute the derived state value S from the full state T.
|
|
132
|
+
* @returns A `ReactiveSelector<S>` object.
|
|
133
|
+
*/
|
|
75
134
|
select<S>(selector: (state: T) => S): ReactiveSelector<S>;
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
|
|
135
|
+
/**
|
|
136
|
+
* Subscribes a callback to run when the data at the specified path(s) changes.
|
|
137
|
+
* @param path A single path string or an array of path strings to watch.
|
|
138
|
+
* @param callback The function to execute when a change occurs in the watched path(s).
|
|
139
|
+
* @returns An unsubscribe function.
|
|
140
|
+
*/
|
|
80
141
|
watch(path: string | Array<string>, callback: (state: T) => void): () => void;
|
|
142
|
+
/**
|
|
143
|
+
* Executes an operation function within a transaction block.
|
|
144
|
+
* All state updates (`set` or actions) within the transaction are batched and applied atomically (all or nothing).
|
|
145
|
+
* @param operation The function containing the state updates.
|
|
146
|
+
* @returns A promise that resolves to the return value of the operation function.
|
|
147
|
+
*/
|
|
81
148
|
transaction<R>(operation: () => R | Promise<R>): Promise<R>;
|
|
149
|
+
/**
|
|
150
|
+
* Registers a middleware function to intercept state updates.
|
|
151
|
+
* @param props The middleware configuration.
|
|
152
|
+
* @returns A function to unregister the middleware.
|
|
153
|
+
*/
|
|
82
154
|
use(props: MiddlewareConfig<T>): () => boolean;
|
|
83
|
-
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
|
|
155
|
+
/**
|
|
156
|
+
* Subscribes a listener to a specific store event type.
|
|
157
|
+
* @param event The type of store event to listen for.
|
|
158
|
+
* @param listener The callback function to execute when the event fires.
|
|
159
|
+
* @returns An unsubscribe function.
|
|
160
|
+
*/
|
|
87
161
|
on(event: StoreEvent, listener: (data: any) => void): () => void;
|
|
162
|
+
/**
|
|
163
|
+
* Returns the unique identifier of the store instance.
|
|
164
|
+
*/
|
|
88
165
|
id(): string;
|
|
166
|
+
/**
|
|
167
|
+
* Checks whether the store is fully initialized and ready for use.
|
|
168
|
+
*/
|
|
89
169
|
isReady(): boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Returns a readonly snapshot of the current execution state of the store.
|
|
172
|
+
*/
|
|
90
173
|
state(): Readonly<StoreExecutionState<T>>;
|
|
91
174
|
}
|
|
92
175
|
|