@sladg/apex-state 3.0.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/dist/index.d.ts +1527 -0
- package/dist/index.js +2990 -0
- package/dist/index.js.map +1 -0
- package/package.json +92 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1527 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hash key type for Record-based paths
|
|
7
|
+
*
|
|
8
|
+
* Use in template strings to construct paths through Record/HashMap properties.
|
|
9
|
+
* This type represents the literal string '[*]' used for indexing into Records.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* type Path = `users.${HASH_KEY}.name` // "users.[*].name"
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
type HASH_KEY = '[*]';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* DeepKey utility type
|
|
20
|
+
*
|
|
21
|
+
* Generates a union of all possible dot-notation paths for nested objects.
|
|
22
|
+
* Supports nested objects up to depth 20 to handle deeply nested data structures.
|
|
23
|
+
* Handles arrays and complex object hierarchies.
|
|
24
|
+
*
|
|
25
|
+
* Examples of supported paths:
|
|
26
|
+
* - Simple: "name", "email"
|
|
27
|
+
* - Nested: "user.address.street"
|
|
28
|
+
* - Deep: "g.p.data.optionsCommon.base.ccyPair.forCurrency.id" (11 levels)
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* type User = {
|
|
33
|
+
* name: string
|
|
34
|
+
* address: {
|
|
35
|
+
* street: string
|
|
36
|
+
* city: string
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* // DeepKey<User> = "name" | "address" | "address.street" | "address.city"
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
type Primitive = string | number | boolean | bigint | symbol | null | undefined;
|
|
45
|
+
type IsAny$1<T> = 0 extends 1 & T ? true : false;
|
|
46
|
+
type DeepKey<T, Depth extends number = 20> = Depth extends 0 ? never : IsAny$1<T> extends true ? never : T extends Primitive ? never : T extends readonly any[] ? never : string extends keyof T ? HASH_KEY | (T[string] extends Primitive ? never : T[string] extends readonly any[] ? never : DeepKey<T[string], Prev<Depth>> extends infer DK ? DK extends string ? `${HASH_KEY}.${DK}` : never : never) : {
|
|
47
|
+
[K in keyof T & (string | number)]: `${K & string}` | (T[K] extends Primitive ? never : T[K] extends readonly any[] ? never : DeepKey<T[K], Prev<Depth>> extends infer DK ? DK extends string ? `${K & string}.${DK}` : never : never);
|
|
48
|
+
}[keyof T & (string | number)];
|
|
49
|
+
type Prev<N extends number> = N extends 20 ? 19 : N extends 19 ? 18 : N extends 18 ? 17 : N extends 17 ? 16 : N extends 16 ? 15 : N extends 15 ? 14 : N extends 14 ? 13 : N extends 13 ? 12 : N extends 12 ? 11 : N extends 11 ? 10 : N extends 10 ? 9 : N extends 9 ? 8 : N extends 8 ? 7 : N extends 7 ? 6 : N extends 6 ? 5 : N extends 5 ? 4 : N extends 4 ? 3 : N extends 3 ? 2 : N extends 2 ? 1 : N extends 1 ? 0 : never;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Boolean logic DSL type definitions
|
|
53
|
+
*
|
|
54
|
+
* Type-safe conditional expression DSL used by concerns and side effects
|
|
55
|
+
* for reactive condition checking against state.
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Primitive types that can be compared in BoolLogic expressions
|
|
60
|
+
*/
|
|
61
|
+
type ComparableValue = string | number | boolean | null | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Boolean logic DSL for conditional expressions
|
|
64
|
+
*
|
|
65
|
+
* Provides a declarative way to express conditions against state paths.
|
|
66
|
+
* Used by concerns (disabledWhen, visibleWhen) and side effects.
|
|
67
|
+
*
|
|
68
|
+
* Operators:
|
|
69
|
+
* - IS_EQUAL: Compare path value to expected value
|
|
70
|
+
* - EXISTS: Check if path value is not null/undefined
|
|
71
|
+
* - IS_EMPTY: Check if path value is empty (string/array/object)
|
|
72
|
+
* - AND/OR/NOT: Boolean combinators
|
|
73
|
+
* - GT/LT/GTE/LTE: Numeric comparisons
|
|
74
|
+
* - IN: Check if path value is in allowed list
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* // Simple equality check
|
|
79
|
+
* const isAdmin: BoolLogic<State> = { IS_EQUAL: ['user.role', 'admin'] }
|
|
80
|
+
*
|
|
81
|
+
* // Combined conditions
|
|
82
|
+
* const canEdit: BoolLogic<State> = {
|
|
83
|
+
* AND: [
|
|
84
|
+
* { IS_EQUAL: ['user.role', 'editor'] },
|
|
85
|
+
* { EXISTS: 'document.id' },
|
|
86
|
+
* { NOT: { IS_EQUAL: ['document.status', 'locked'] } }
|
|
87
|
+
* ]
|
|
88
|
+
* }
|
|
89
|
+
*
|
|
90
|
+
* // Numeric comparison
|
|
91
|
+
* const isExpensive: BoolLogic<State> = { GT: ['product.price', 100] }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
type BoolLogic<STATE> = {
|
|
95
|
+
IS_EQUAL: [DeepKey<STATE>, ComparableValue];
|
|
96
|
+
} | {
|
|
97
|
+
EXISTS: DeepKey<STATE>;
|
|
98
|
+
} | {
|
|
99
|
+
IS_EMPTY: DeepKey<STATE>;
|
|
100
|
+
} | {
|
|
101
|
+
AND: BoolLogic<STATE>[];
|
|
102
|
+
} | {
|
|
103
|
+
OR: BoolLogic<STATE>[];
|
|
104
|
+
} | {
|
|
105
|
+
NOT: BoolLogic<STATE>;
|
|
106
|
+
} | {
|
|
107
|
+
GT: [DeepKey<STATE>, number];
|
|
108
|
+
} | {
|
|
109
|
+
LT: [DeepKey<STATE>, number];
|
|
110
|
+
} | {
|
|
111
|
+
GTE: [DeepKey<STATE>, number];
|
|
112
|
+
} | {
|
|
113
|
+
LTE: [DeepKey<STATE>, number];
|
|
114
|
+
} | {
|
|
115
|
+
IN: [DeepKey<STATE>, ComparableValue[]];
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* DeepValue utility type
|
|
120
|
+
*
|
|
121
|
+
* Extracts the value type for a given dot-notation path string.
|
|
122
|
+
* Handles nested objects, arrays, and optional properties.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* type User = {
|
|
127
|
+
* address: {
|
|
128
|
+
* street: string
|
|
129
|
+
* city: string
|
|
130
|
+
* }
|
|
131
|
+
* }
|
|
132
|
+
*
|
|
133
|
+
* // DeepValue<User, "address.street"> = string
|
|
134
|
+
* // DeepValue<User, "address"> = { street: string, city: string }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
|
|
138
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
139
|
+
type DeepValue<T, Path extends string> = IsAny<T> extends true ? never : T extends readonly any[] ? T[number] : Path extends `${infer First}.${infer Rest}` ? First extends keyof T ? DeepValue<T[First], Rest> : string extends keyof T ? First extends HASH_KEY ? DeepValue<T[string], Rest> : unknown : unknown : Path extends HASH_KEY ? string extends keyof T ? T[string] : unknown : Path extends keyof T ? T[Path] : unknown;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* GenericMeta interface
|
|
143
|
+
*
|
|
144
|
+
* Base metadata type for change tracking with standard properties.
|
|
145
|
+
* Can be extended with custom properties for specific use cases.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* interface CustomMeta extends GenericMeta {
|
|
150
|
+
* timestamp: number
|
|
151
|
+
* userId: string
|
|
152
|
+
* }
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
interface GenericMeta {
|
|
156
|
+
/**
|
|
157
|
+
* Indicates if the change originated from a sync path side-effect.
|
|
158
|
+
* Used to track cascading changes triggered by synchronization logic.
|
|
159
|
+
*/
|
|
160
|
+
isSyncPathChange?: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Indicates if the change originated from a flip path side-effect.
|
|
163
|
+
* Used to track bidirectional synchronization changes.
|
|
164
|
+
*/
|
|
165
|
+
isFlipPathChange?: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Indicates if the change was triggered programmatically rather than by user action.
|
|
168
|
+
* Useful for distinguishing between automated and manual state updates.
|
|
169
|
+
*/
|
|
170
|
+
isProgramaticChange?: boolean;
|
|
171
|
+
/**
|
|
172
|
+
* Indicates if the change originated from an aggregation side-effect.
|
|
173
|
+
* Used to track changes triggered by aggregation logic.
|
|
174
|
+
*/
|
|
175
|
+
isAggregationChange?: boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Indicates if the change originated from a listener side-effect.
|
|
178
|
+
* Used to track changes triggered by listener callbacks.
|
|
179
|
+
*/
|
|
180
|
+
isListenerChange?: boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Identifies the originator of the change.
|
|
183
|
+
* Can be a user ID, component name, or any identifier string.
|
|
184
|
+
*/
|
|
185
|
+
sender?: string;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* ArrayOfChanges type
|
|
190
|
+
*
|
|
191
|
+
* Represents an array of changes with paths, values, and metadata.
|
|
192
|
+
* Each change is a tuple of [path, value, metadata].
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* type User = {
|
|
197
|
+
* name: string
|
|
198
|
+
* age: number
|
|
199
|
+
* }
|
|
200
|
+
*
|
|
201
|
+
* const changes: ArrayOfChanges<User, GenericMeta> = [
|
|
202
|
+
* ["name", "John", { sender: "user-123" }],
|
|
203
|
+
* ["age", 30, { isProgramaticChange: true }]
|
|
204
|
+
* ]
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Represents an array of change tuples.
|
|
210
|
+
* Each tuple contains:
|
|
211
|
+
* - path: A valid deep key path for the data structure
|
|
212
|
+
* - value: The value at that path (properly typed based on the path)
|
|
213
|
+
* - meta: Metadata about the change
|
|
214
|
+
*/
|
|
215
|
+
type ArrayOfChanges<DATA, META extends GenericMeta = GenericMeta> = {
|
|
216
|
+
[K in DeepKey<DATA>]: [K, DeepValue<DATA, K>, META];
|
|
217
|
+
}[DeepKey<DATA>][];
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Concern type utilities
|
|
221
|
+
*
|
|
222
|
+
* Generic type helpers for working with concern arrays and extracting
|
|
223
|
+
* their return types for proper type-safe concern registration and reading.
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Extract the return type from a concern's evaluate function
|
|
228
|
+
*
|
|
229
|
+
* Used internally to determine what a concern evaluates to,
|
|
230
|
+
* enabling type-safe concern result objects.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* type MyConcern = { evaluate: (...args: any[]) => boolean }
|
|
235
|
+
* type Result = ExtractEvaluateReturn<MyConcern> // boolean
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
type ExtractEvaluateReturn<T> = T extends {
|
|
239
|
+
evaluate: (...args: any[]) => infer R;
|
|
240
|
+
} ? R : never;
|
|
241
|
+
/**
|
|
242
|
+
* Dynamically build an evaluated concerns object from a CONCERNS array
|
|
243
|
+
*
|
|
244
|
+
* Maps each concern's name to its return type, creating a properly typed
|
|
245
|
+
* object that represents all concerns that can be registered/evaluated.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* const concerns = [
|
|
250
|
+
* { name: 'validationState', evaluate: () => ({ isError: boolean, errors: [] }) },
|
|
251
|
+
* { name: 'tooltip', evaluate: () => string }
|
|
252
|
+
* ] as const
|
|
253
|
+
*
|
|
254
|
+
* type Evaluated = EvaluatedConcerns<typeof concerns>
|
|
255
|
+
* // { validationState?: { isError: boolean, errors: [] }, tooltip?: string }
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
type EvaluatedConcerns<CONCERNS extends readonly any[]> = {
|
|
259
|
+
[K in CONCERNS[number] as K['name']]?: ExtractEvaluateReturn<K>;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Maps field paths to per-concern config objects.
|
|
263
|
+
*
|
|
264
|
+
* Used as the `registration` parameter for `useConcerns` / `registerConcernEffects`.
|
|
265
|
+
* Each key is a `DeepKey<DATA>` path, and the value maps concern names to their config.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* const registration: ConcernRegistrationMap<MyFormState> = {
|
|
270
|
+
* email: { validationState: { schema: z.string().email() } },
|
|
271
|
+
* name: { validationState: { schema: z.string().min(1) } },
|
|
272
|
+
* }
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
type ConcernRegistrationMap<DATA extends object> = Partial<Record<DeepKey<DATA>, Partial<Record<string, unknown>>>>;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* DeepKeyFiltered - Filters paths by their resolved value type
|
|
279
|
+
*
|
|
280
|
+
* Returns only paths from DeepKey<T> that resolve to a specific type U.
|
|
281
|
+
* Useful for type-safe filtering of state paths by their value types.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```typescript
|
|
285
|
+
* type Data = {
|
|
286
|
+
* isActive: boolean
|
|
287
|
+
* name: string
|
|
288
|
+
* count: number
|
|
289
|
+
* user: { isAdmin: boolean }
|
|
290
|
+
* }
|
|
291
|
+
*
|
|
292
|
+
* // Only boolean paths
|
|
293
|
+
* type BoolPaths = DeepKeyFiltered<Data, boolean>
|
|
294
|
+
* // Result: "isActive" | "user.isAdmin"
|
|
295
|
+
*
|
|
296
|
+
* // Only string paths
|
|
297
|
+
* type StrPaths = DeepKeyFiltered<Data, string>
|
|
298
|
+
* // Result: "name"
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Filters DeepKey<T> to only include paths that resolve to type U.
|
|
304
|
+
*
|
|
305
|
+
* Uses mapped type with conditional filtering - maps over all possible paths,
|
|
306
|
+
* keeps those matching the target type, and extracts the union.
|
|
307
|
+
*/
|
|
308
|
+
type DeepKeyFiltered<T, U> = {
|
|
309
|
+
[K in DeepKey<T>]: DeepValue<T, K> extends U ? K : never;
|
|
310
|
+
}[DeepKey<T>];
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* PathsOfSameValue - Type-safe path tuples for side effects
|
|
314
|
+
*
|
|
315
|
+
* Simple tuple-based API for syncPaths, flipPaths, and aggregations.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```typescript
|
|
319
|
+
* type State = {
|
|
320
|
+
* user: { email: string }
|
|
321
|
+
* profile: { email: string }
|
|
322
|
+
* count: number
|
|
323
|
+
* }
|
|
324
|
+
*
|
|
325
|
+
* // ✓ Valid: both paths are string
|
|
326
|
+
* const sync: SyncPair<State> = ['user.email', 'profile.email']
|
|
327
|
+
*
|
|
328
|
+
* // ✗ Error: 'user.email' (string) can't sync with 'count' (number)
|
|
329
|
+
* const invalid: SyncPair<State> = ['user.email', 'count']
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Get all paths in DATA that have the same value type as the value at PATH
|
|
335
|
+
*/
|
|
336
|
+
type PathsWithSameValueAs<DATA extends object, PATH extends DeepKey<DATA>> = {
|
|
337
|
+
[K in DeepKey<DATA>]: DeepValue<DATA, K> extends DeepValue<DATA, PATH> ? DeepValue<DATA, PATH> extends DeepValue<DATA, K> ? K : never : never;
|
|
338
|
+
}[DeepKey<DATA>];
|
|
339
|
+
/**
|
|
340
|
+
* A tuple of two paths that must have the same value type.
|
|
341
|
+
* Format: [path1, path2]
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* const pair: SyncPair<State> = ['user.email', 'profile.email']
|
|
345
|
+
*/
|
|
346
|
+
type SyncPair<DATA extends object> = {
|
|
347
|
+
[P1 in DeepKey<DATA>]: [P1, PathsWithSameValueAs<DATA, P1>];
|
|
348
|
+
}[DeepKey<DATA>];
|
|
349
|
+
/**
|
|
350
|
+
* A tuple of two paths for flip (alias for SyncPair)
|
|
351
|
+
* Format: [path1, path2]
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* const pair: FlipPair<State> = ['isActive', 'isInactive']
|
|
355
|
+
*/
|
|
356
|
+
type FlipPair<DATA extends object> = SyncPair<DATA>;
|
|
357
|
+
/**
|
|
358
|
+
* A tuple for aggregation: [target, source]
|
|
359
|
+
* First element (left) is ALWAYS the target (aggregated) path.
|
|
360
|
+
* Second element is a source path.
|
|
361
|
+
*
|
|
362
|
+
* Multiple pairs can point to same target for multi-source aggregation.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* // target <- source (target is always first/left)
|
|
366
|
+
* const aggs: AggregationPair<State>[] = [
|
|
367
|
+
* ['total', 'price1'],
|
|
368
|
+
* ['total', 'price2'],
|
|
369
|
+
* ]
|
|
370
|
+
*/
|
|
371
|
+
type AggregationPair<DATA extends object> = SyncPair<DATA>;
|
|
372
|
+
|
|
373
|
+
/** Recursively makes all properties required, stripping undefined */
|
|
374
|
+
type DeepRequired<T> = {
|
|
375
|
+
[K in keyof T]-?: NonNullable<T[K]> extends object ? DeepRequired<NonNullable<T[K]>> : NonNullable<T[K]>;
|
|
376
|
+
};
|
|
377
|
+
/** Recursively makes all properties optional (allows undefined values) */
|
|
378
|
+
type DeepPartial<T> = {
|
|
379
|
+
[K in keyof T]?: NonNullable<T[K]> extends object ? DeepPartial<NonNullable<T[K]>> | undefined : T[K] | undefined;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
interface BaseConcernProps<STATE, PATH extends string> {
|
|
383
|
+
state: STATE;
|
|
384
|
+
path: PATH;
|
|
385
|
+
value: unknown;
|
|
386
|
+
}
|
|
387
|
+
interface ConcernType<EXTRA_PROPS = Record<string, unknown>, RETURN_TYPE = unknown> {
|
|
388
|
+
name: string;
|
|
389
|
+
description: string;
|
|
390
|
+
/** Evaluated inside effect() - all state accesses are tracked */
|
|
391
|
+
evaluate: (props: BaseConcernProps<Record<string, unknown>, string> & EXTRA_PROPS) => RETURN_TYPE;
|
|
392
|
+
}
|
|
393
|
+
interface ConcernRegistration {
|
|
394
|
+
id: string;
|
|
395
|
+
path: string;
|
|
396
|
+
concernName: string;
|
|
397
|
+
concern: ConcernType;
|
|
398
|
+
config: Record<string, unknown>;
|
|
399
|
+
dispose: () => void;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Debug Timing Utilities
|
|
404
|
+
*
|
|
405
|
+
* Provides timing measurement for concerns and listeners
|
|
406
|
+
* to detect slow operations during development.
|
|
407
|
+
*/
|
|
408
|
+
type TimingType = 'concerns' | 'listeners' | 'registration';
|
|
409
|
+
interface TimingMeta {
|
|
410
|
+
path: string;
|
|
411
|
+
name: string;
|
|
412
|
+
}
|
|
413
|
+
interface Timing {
|
|
414
|
+
run: <T>(type: TimingType, fn: () => T, meta: TimingMeta) => T;
|
|
415
|
+
reportBatch: (type: TimingType) => void;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* WASM Bridge — Thin namespace over Rust/WASM exports.
|
|
420
|
+
*
|
|
421
|
+
* Uses serde-wasm-bindgen for hot-path functions (processChanges,
|
|
422
|
+
* shadowInit) — JS objects cross the boundary directly without JSON
|
|
423
|
+
* string intermediary. Registration functions still use JSON strings
|
|
424
|
+
* (cold path, simpler).
|
|
425
|
+
*
|
|
426
|
+
* Loading is handled by `wasm/lifecycle.ts`. After loading, all bridge
|
|
427
|
+
* functions are synchronous.
|
|
428
|
+
*
|
|
429
|
+
* @module wasm/bridge
|
|
430
|
+
*/
|
|
431
|
+
|
|
432
|
+
/** A single state change (input or output). */
|
|
433
|
+
interface Change {
|
|
434
|
+
path: string;
|
|
435
|
+
value: unknown;
|
|
436
|
+
}
|
|
437
|
+
/** A single dispatch entry with sequential ID and input change references. */
|
|
438
|
+
interface DispatchEntry {
|
|
439
|
+
dispatch_id: number;
|
|
440
|
+
subscriber_id: number;
|
|
441
|
+
scope_path: string;
|
|
442
|
+
/** Indexes into ProcessResult.changes array. */
|
|
443
|
+
input_change_ids: number[];
|
|
444
|
+
}
|
|
445
|
+
/** A group of dispatches to execute sequentially. */
|
|
446
|
+
interface DispatchGroup {
|
|
447
|
+
dispatches: DispatchEntry[];
|
|
448
|
+
}
|
|
449
|
+
/** A target for propagating produced changes from child to parent dispatch. */
|
|
450
|
+
interface PropagationTarget {
|
|
451
|
+
target_dispatch_id: number;
|
|
452
|
+
/** Prefix to prepend to child's relative paths for the target's scope. */
|
|
453
|
+
remap_prefix: string;
|
|
454
|
+
}
|
|
455
|
+
/** Pre-computed execution plan with propagation map. */
|
|
456
|
+
interface FullExecutionPlan {
|
|
457
|
+
groups: DispatchGroup[];
|
|
458
|
+
/** propagation_map[dispatch_id] = targets to forward produced changes to. */
|
|
459
|
+
propagation_map: PropagationTarget[][];
|
|
460
|
+
}
|
|
461
|
+
/** Validator dispatch info for JS-side execution. */
|
|
462
|
+
interface ValidatorDispatch {
|
|
463
|
+
validator_id: number;
|
|
464
|
+
output_path: string;
|
|
465
|
+
dependency_values: Record<string, string>;
|
|
466
|
+
}
|
|
467
|
+
/** Consolidated registration input for side effects (sync, flip, aggregation, clear, listeners). */
|
|
468
|
+
interface SideEffectsRegistration {
|
|
469
|
+
registration_id: string;
|
|
470
|
+
sync_pairs?: [string, string][];
|
|
471
|
+
flip_pairs?: [string, string][];
|
|
472
|
+
aggregation_pairs?: [string, string][];
|
|
473
|
+
clear_paths?: {
|
|
474
|
+
triggers: string[];
|
|
475
|
+
targets: string[];
|
|
476
|
+
}[];
|
|
477
|
+
listeners?: {
|
|
478
|
+
subscriber_id: number;
|
|
479
|
+
topic_path: string;
|
|
480
|
+
scope_path: string;
|
|
481
|
+
}[];
|
|
482
|
+
}
|
|
483
|
+
/** Consolidated registration output from side effects registration. */
|
|
484
|
+
interface SideEffectsResult {
|
|
485
|
+
sync_changes: Change[];
|
|
486
|
+
aggregation_changes: Change[];
|
|
487
|
+
registered_listener_ids: number[];
|
|
488
|
+
}
|
|
489
|
+
/** Consolidated registration input for concerns (BoolLogic, validators, and ValueLogic). */
|
|
490
|
+
interface ConcernsRegistration {
|
|
491
|
+
registration_id: string;
|
|
492
|
+
bool_logics?: {
|
|
493
|
+
output_path: string;
|
|
494
|
+
tree_json: string;
|
|
495
|
+
}[];
|
|
496
|
+
validators?: {
|
|
497
|
+
validator_id: number;
|
|
498
|
+
output_path: string;
|
|
499
|
+
dependency_paths: string[];
|
|
500
|
+
scope: string;
|
|
501
|
+
}[];
|
|
502
|
+
value_logics?: {
|
|
503
|
+
output_path: string;
|
|
504
|
+
tree_json: string;
|
|
505
|
+
}[];
|
|
506
|
+
}
|
|
507
|
+
/** Consolidated registration output from concerns registration. */
|
|
508
|
+
interface ConcernsResult {
|
|
509
|
+
bool_logic_changes: Change[];
|
|
510
|
+
registered_logic_ids: number[];
|
|
511
|
+
registered_validator_ids: number[];
|
|
512
|
+
value_logic_changes: Change[];
|
|
513
|
+
registered_value_logic_ids: number[];
|
|
514
|
+
}
|
|
515
|
+
/** Result of processChanges (Phase 1). */
|
|
516
|
+
interface ProcessChangesResult {
|
|
517
|
+
state_changes: Change[];
|
|
518
|
+
changes: Change[];
|
|
519
|
+
validators_to_run: ValidatorDispatch[];
|
|
520
|
+
execution_plan: FullExecutionPlan | null;
|
|
521
|
+
has_work: boolean;
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* An isolated WASM pipeline instance.
|
|
525
|
+
* Each store gets its own pipeline so multiple Providers don't interfere.
|
|
526
|
+
* All methods are pre-bound to the pipeline's ID — consumers never pass IDs.
|
|
527
|
+
*/
|
|
528
|
+
interface WasmPipeline {
|
|
529
|
+
readonly id: number;
|
|
530
|
+
shadowInit: (state: object) => void;
|
|
531
|
+
shadowDump: () => unknown;
|
|
532
|
+
processChanges: (changes: Change[]) => ProcessChangesResult;
|
|
533
|
+
pipelineFinalize: (jsChanges: Change[]) => {
|
|
534
|
+
state_changes: Change[];
|
|
535
|
+
};
|
|
536
|
+
registerSideEffects: (reg: SideEffectsRegistration) => SideEffectsResult;
|
|
537
|
+
unregisterSideEffects: (registrationId: string) => void;
|
|
538
|
+
registerConcerns: (reg: ConcernsRegistration) => ConcernsResult;
|
|
539
|
+
unregisterConcerns: (registrationId: string) => void;
|
|
540
|
+
registerBoolLogic: (outputPath: string, tree: unknown) => number;
|
|
541
|
+
unregisterBoolLogic: (logicId: number) => void;
|
|
542
|
+
pipelineReset: () => void;
|
|
543
|
+
destroy: () => void;
|
|
544
|
+
/** Per-instance storage for Zod schemas (can't cross WASM boundary). */
|
|
545
|
+
validatorSchemas: Map<number, z.ZodSchema>;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* PathGroups Data Structure
|
|
550
|
+
*
|
|
551
|
+
* A custom data structure that provides O(1) connected component lookups
|
|
552
|
+
* instead of O(V+E) recomputation on every change like graphology's connectedComponents.
|
|
553
|
+
*
|
|
554
|
+
* When wasmGraphType is set ('sync' | 'flip'), addEdge/removeEdge automatically
|
|
555
|
+
* mirror registrations to WASM bridge for pipeline processing.
|
|
556
|
+
*
|
|
557
|
+
* Operations complexity:
|
|
558
|
+
* - getAllGroups(): O(G) where G is number of groups (typically small)
|
|
559
|
+
* - getGroupPaths(path): O(1) lookup
|
|
560
|
+
* - addEdge(p1, p2): O(n) where n is smaller group size (merge)
|
|
561
|
+
* - removeEdge(p1, p2): O(n) for affected component
|
|
562
|
+
* - hasPath(path): O(1)
|
|
563
|
+
* - hasEdge(p1, p2): O(1)
|
|
564
|
+
*/
|
|
565
|
+
/** Graph type for WASM mirroring. When set, addEdge/removeEdge mirror to WASM. */
|
|
566
|
+
type WasmGraphType = 'sync' | 'flip';
|
|
567
|
+
/**
|
|
568
|
+
* PathGroups maintains connected components with O(1) lookups.
|
|
569
|
+
*
|
|
570
|
+
* Internal structure:
|
|
571
|
+
* - pathToGroup: Maps each path to its group ID
|
|
572
|
+
* - groupToPaths: Maps each group ID to the set of paths in that group
|
|
573
|
+
* - edges: Set of edge keys for edge existence checks and proper removal
|
|
574
|
+
* - adjacency: Maps each path to its direct neighbors (for split detection on removal)
|
|
575
|
+
* - nextGroupId: Counter for generating unique group IDs
|
|
576
|
+
*/
|
|
577
|
+
interface PathGroups {
|
|
578
|
+
pathToGroup: Map<string, number>;
|
|
579
|
+
groupToPaths: Map<number, Set<string>>;
|
|
580
|
+
edges: Set<string>;
|
|
581
|
+
adjacency: Map<string, Set<string>>;
|
|
582
|
+
nextGroupId: number;
|
|
583
|
+
wasmGraphType?: WasmGraphType;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Graph Type Definitions
|
|
588
|
+
*
|
|
589
|
+
* Type aliases for the PathGroups data structure used in sync and flip operations.
|
|
590
|
+
* These aliases maintain backward compatibility with the previous graphology-based API.
|
|
591
|
+
*/
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Sync graph: Paths that must have synchronized values
|
|
595
|
+
* Type alias for PathGroups - maintains backward compatibility
|
|
596
|
+
*/
|
|
597
|
+
type SyncGraph = PathGroups;
|
|
598
|
+
/**
|
|
599
|
+
* Flip graph: Paths with inverted boolean values
|
|
600
|
+
* Type alias for PathGroups - maintains backward compatibility
|
|
601
|
+
*/
|
|
602
|
+
type FlipGraph = PathGroups;
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Core Store Types
|
|
606
|
+
*
|
|
607
|
+
* Foundational type definitions for the store instance.
|
|
608
|
+
* These types are used throughout the library.
|
|
609
|
+
*/
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Debug configuration for development tooling
|
|
613
|
+
*/
|
|
614
|
+
interface DebugConfig {
|
|
615
|
+
/** Enable timing measurement for concerns and listeners */
|
|
616
|
+
timing?: boolean;
|
|
617
|
+
/** Threshold in milliseconds for slow operation warnings (default: 5ms) */
|
|
618
|
+
timingThreshold?: number;
|
|
619
|
+
/** Enable tracking of processChanges calls and applied changes for testing/debugging */
|
|
620
|
+
track?: boolean;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* A single recorded processChanges invocation
|
|
624
|
+
*/
|
|
625
|
+
interface DebugTrackEntry {
|
|
626
|
+
/** Input changes passed to processChanges as [path, value, meta] tuples */
|
|
627
|
+
input: [string, unknown, unknown][];
|
|
628
|
+
/** Changes actually applied to state proxy */
|
|
629
|
+
applied: {
|
|
630
|
+
path: string;
|
|
631
|
+
value: unknown;
|
|
632
|
+
}[];
|
|
633
|
+
/** Changes applied to _concerns proxy */
|
|
634
|
+
appliedConcerns: {
|
|
635
|
+
path: string;
|
|
636
|
+
value: unknown;
|
|
637
|
+
}[];
|
|
638
|
+
/** Timestamp of the call */
|
|
639
|
+
timestamp: number;
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Debug tracking data exposed on StoreInstance when debug.track is enabled.
|
|
643
|
+
* Provides an append-only log of all processChanges calls and their effects.
|
|
644
|
+
*/
|
|
645
|
+
interface DebugTrack {
|
|
646
|
+
/** All recorded processChanges calls (append-only) */
|
|
647
|
+
calls: DebugTrackEntry[];
|
|
648
|
+
/** Reset all tracking data */
|
|
649
|
+
clear: () => void;
|
|
650
|
+
}
|
|
651
|
+
interface StoreConfig {
|
|
652
|
+
/** Error storage path (default: "_errors") */
|
|
653
|
+
errorStorePath?: string;
|
|
654
|
+
/** Max iterations for change processing (default: 100) */
|
|
655
|
+
maxIterations?: number;
|
|
656
|
+
/** Debug configuration for development tooling */
|
|
657
|
+
debug?: DebugConfig;
|
|
658
|
+
/** Use legacy TypeScript implementation instead of WASM (default: false) */
|
|
659
|
+
useLegacyImplementation?: boolean;
|
|
660
|
+
}
|
|
661
|
+
interface ProviderProps<DATA extends object> {
|
|
662
|
+
initialState: DATA;
|
|
663
|
+
children: ReactNode;
|
|
664
|
+
}
|
|
665
|
+
interface Aggregation {
|
|
666
|
+
id?: string;
|
|
667
|
+
targetPath: string;
|
|
668
|
+
sourcePaths: string[];
|
|
669
|
+
}
|
|
670
|
+
/** Reacts to scoped changes - receives relative paths and scoped state. Only fires for NESTED paths, not the path itself. */
|
|
671
|
+
type OnStateListener<DATA extends object = object, SUB_STATE = DATA, META extends GenericMeta = GenericMeta> = (changes: ArrayOfChanges<SUB_STATE, META>, state: SUB_STATE) => ArrayOfChanges<DATA, META> | undefined;
|
|
672
|
+
/**
|
|
673
|
+
* Listener registration with path (what to watch) and scope (how to present data)
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* ```typescript
|
|
677
|
+
* // Watch user.profile.name, get full state
|
|
678
|
+
* {
|
|
679
|
+
* path: 'user.profile.name',
|
|
680
|
+
* scope: null,
|
|
681
|
+
* fn: (changes, state) => {
|
|
682
|
+
* // changes: [['user.profile.name', 'Alice', {}]] - FULL path
|
|
683
|
+
* // state: full DATA object
|
|
684
|
+
* }
|
|
685
|
+
* }
|
|
686
|
+
*
|
|
687
|
+
* // Watch user.profile.*, get scoped state
|
|
688
|
+
* {
|
|
689
|
+
* path: 'user.profile',
|
|
690
|
+
* scope: 'user.profile',
|
|
691
|
+
* fn: (changes, state) => {
|
|
692
|
+
* // changes: [['name', 'Alice', {}]] - RELATIVE to scope
|
|
693
|
+
* // state: user.profile object
|
|
694
|
+
* }
|
|
695
|
+
* }
|
|
696
|
+
*
|
|
697
|
+
* // Watch deep path, get parent scope
|
|
698
|
+
* {
|
|
699
|
+
* path: 'p.123.g.abc.data.strike',
|
|
700
|
+
* scope: 'p.123.g.abc',
|
|
701
|
+
* fn: (changes, state) => {
|
|
702
|
+
* // changes: [['data.strike', value, {}]] - relative to scope
|
|
703
|
+
* // state: p.123.g.abc object
|
|
704
|
+
* }
|
|
705
|
+
* }
|
|
706
|
+
* ```
|
|
707
|
+
*/
|
|
708
|
+
interface ListenerRegistration<DATA extends object = object, META extends GenericMeta = GenericMeta> {
|
|
709
|
+
/**
|
|
710
|
+
* Path to watch - only changes under this path will trigger the listener
|
|
711
|
+
* null = watch all top-level paths
|
|
712
|
+
*/
|
|
713
|
+
path: DeepKey<DATA> | null;
|
|
714
|
+
/**
|
|
715
|
+
* Scope for state and changes presentation
|
|
716
|
+
* - If null: state is full DATA, changes use FULL paths
|
|
717
|
+
* - If set: state is value at scope, changes use paths RELATIVE to scope
|
|
718
|
+
*
|
|
719
|
+
* Note: Changes are filtered based on `path`, even when scope is null
|
|
720
|
+
*/
|
|
721
|
+
scope: DeepKey<DATA> | null;
|
|
722
|
+
fn: OnStateListener<DATA, any, META>;
|
|
723
|
+
}
|
|
724
|
+
interface ListenerHandlerRef {
|
|
725
|
+
scope: string | null;
|
|
726
|
+
fn: (...args: unknown[]) => unknown;
|
|
727
|
+
}
|
|
728
|
+
interface SideEffectGraphs<DATA extends object = object, META extends GenericMeta = GenericMeta> {
|
|
729
|
+
sync: SyncGraph;
|
|
730
|
+
flip: FlipGraph;
|
|
731
|
+
listeners: Map<string, ListenerRegistration<DATA, META>[]>;
|
|
732
|
+
sortedListenerPaths: string[];
|
|
733
|
+
/** O(1) lookup: subscriber_id -> handler ref. Populated by registerListener. */
|
|
734
|
+
listenerHandlers: Map<number, ListenerHandlerRef>;
|
|
735
|
+
}
|
|
736
|
+
interface Registrations {
|
|
737
|
+
concerns: Map<string, ConcernType[]>;
|
|
738
|
+
effectCleanups: Set<() => void>;
|
|
739
|
+
sideEffectCleanups: Map<string, () => void>;
|
|
740
|
+
aggregations: Map<string, Aggregation[]>;
|
|
741
|
+
}
|
|
742
|
+
interface ProcessingState<DATA extends object = object, META extends GenericMeta = GenericMeta> {
|
|
743
|
+
queue: ArrayOfChanges<DATA, META>;
|
|
744
|
+
}
|
|
745
|
+
/** Internal store state (NOT tracked - wrapped in ref()) */
|
|
746
|
+
interface InternalState<DATA extends object = object, META extends GenericMeta = GenericMeta> {
|
|
747
|
+
graphs: SideEffectGraphs<DATA, META>;
|
|
748
|
+
registrations: Registrations;
|
|
749
|
+
processing: ProcessingState<DATA, META>;
|
|
750
|
+
timing: Timing;
|
|
751
|
+
config: DeepRequired<StoreConfig>;
|
|
752
|
+
/** Per-store WASM pipeline instance (null when using legacy implementation). */
|
|
753
|
+
pipeline: WasmPipeline | null;
|
|
754
|
+
}
|
|
755
|
+
type ConcernValues = Record<string, Record<string, unknown>>;
|
|
756
|
+
/** Two-proxy pattern: state and _concerns are independent to prevent infinite loops */
|
|
757
|
+
interface StoreInstance<DATA extends object, META extends GenericMeta = GenericMeta> {
|
|
758
|
+
state: DATA;
|
|
759
|
+
_concerns: ConcernValues;
|
|
760
|
+
_internal: InternalState<DATA, META>;
|
|
761
|
+
/** Debug tracking data, only populated when debug.track is enabled */
|
|
762
|
+
_debug: DebugTrack | null;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
interface ValidationError {
|
|
766
|
+
field?: string;
|
|
767
|
+
message: string;
|
|
768
|
+
}
|
|
769
|
+
interface ValidationStateResult {
|
|
770
|
+
isError: boolean;
|
|
771
|
+
errors: ValidationError[];
|
|
772
|
+
}
|
|
773
|
+
type ValidationStateInput<SUB_STATE, PATH extends DeepKey<SUB_STATE>> = {
|
|
774
|
+
schema: z.ZodSchema<DeepValue<SUB_STATE, PATH>>;
|
|
775
|
+
} | {
|
|
776
|
+
[SCOPE in DeepKey<SUB_STATE>]: {
|
|
777
|
+
scope: SCOPE;
|
|
778
|
+
schema: z.ZodSchema<DeepValue<SUB_STATE, SCOPE>>;
|
|
779
|
+
};
|
|
780
|
+
}[DeepKey<SUB_STATE>];
|
|
781
|
+
interface ValidationStateConcern {
|
|
782
|
+
name: 'validationState';
|
|
783
|
+
description: string;
|
|
784
|
+
evaluate: <SUB_STATE, PATH extends DeepKey<SUB_STATE>>(props: BaseConcernProps<SUB_STATE, PATH> & ValidationStateInput<SUB_STATE, PATH>) => ValidationStateResult;
|
|
785
|
+
}
|
|
786
|
+
declare const validationState: ValidationStateConcern;
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Concern lookup by name
|
|
790
|
+
*
|
|
791
|
+
* @param name The concern name to look up
|
|
792
|
+
* @param concerns Optional array of concerns to search (defaults to prebuilts)
|
|
793
|
+
* @returns The concern definition, or undefined if not found
|
|
794
|
+
*/
|
|
795
|
+
declare const findConcern: (name: string, concerns?: readonly any[]) => ConcernType | undefined;
|
|
796
|
+
/**
|
|
797
|
+
* Default concerns provided by apex-state
|
|
798
|
+
*/
|
|
799
|
+
declare const defaultConcerns: readonly [ValidationStateConcern, ConcernType<{
|
|
800
|
+
boolLogic: BoolLogic<any>;
|
|
801
|
+
}, boolean>, ConcernType<{
|
|
802
|
+
boolLogic: BoolLogic<any>;
|
|
803
|
+
}, boolean>, ConcernType<{
|
|
804
|
+
boolLogic: BoolLogic<any>;
|
|
805
|
+
}, boolean>, ConcernType<{
|
|
806
|
+
template: string;
|
|
807
|
+
}, string>, ConcernType<{
|
|
808
|
+
template: string;
|
|
809
|
+
}, string>, ConcernType<{
|
|
810
|
+
template: string;
|
|
811
|
+
}, string>];
|
|
812
|
+
|
|
813
|
+
declare const disabledWhen: ConcernType<{
|
|
814
|
+
boolLogic: BoolLogic<any>;
|
|
815
|
+
}, boolean>;
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Read-only condition concern
|
|
819
|
+
*
|
|
820
|
+
* Evaluates a boolean logic expression to determine if a field should be read-only.
|
|
821
|
+
* Automatically tracks all state paths accessed in the condition.
|
|
822
|
+
*
|
|
823
|
+
* Returns true if read-only, false if editable.
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* ```typescript
|
|
827
|
+
* store.useConcerns('my-concerns', {
|
|
828
|
+
* 'productId': {
|
|
829
|
+
* readonlyWhen: { boolLogic: { IS_EQUAL: ['order.status', 'completed'] } }
|
|
830
|
+
* }
|
|
831
|
+
* })
|
|
832
|
+
* // Returns: true if order status is 'completed', false otherwise
|
|
833
|
+
* ```
|
|
834
|
+
*/
|
|
835
|
+
|
|
836
|
+
declare const readonlyWhen: ConcernType<{
|
|
837
|
+
boolLogic: BoolLogic<any>;
|
|
838
|
+
}, boolean>;
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Visibility condition concern
|
|
842
|
+
*
|
|
843
|
+
* Evaluates a boolean logic expression to determine if a field should be visible.
|
|
844
|
+
* Automatically tracks all state paths accessed in the condition.
|
|
845
|
+
*
|
|
846
|
+
* Returns true if visible, false if hidden.
|
|
847
|
+
*
|
|
848
|
+
* @example
|
|
849
|
+
* ```typescript
|
|
850
|
+
* store.useConcerns('my-concerns', {
|
|
851
|
+
* 'advancedOptions': {
|
|
852
|
+
* visibleWhen: { boolLogic: { IS_EQUAL: ['settings.mode', 'advanced'] } }
|
|
853
|
+
* }
|
|
854
|
+
* })
|
|
855
|
+
* // Returns: true if settings.mode is 'advanced', false otherwise
|
|
856
|
+
* ```
|
|
857
|
+
*/
|
|
858
|
+
|
|
859
|
+
declare const visibleWhen: ConcernType<{
|
|
860
|
+
boolLogic: BoolLogic<any>;
|
|
861
|
+
}, boolean>;
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* Dynamic label template concern
|
|
865
|
+
*
|
|
866
|
+
* Interpolates a template string with values from state.
|
|
867
|
+
* Automatically tracks all state paths referenced in the template.
|
|
868
|
+
*
|
|
869
|
+
* Returns the interpolated string.
|
|
870
|
+
*
|
|
871
|
+
* @example
|
|
872
|
+
* ```typescript
|
|
873
|
+
* store.useConcerns('my-concerns', {
|
|
874
|
+
* 'priceField': {
|
|
875
|
+
* dynamicLabel: { template: "Price: ${{product.price}}" }
|
|
876
|
+
* }
|
|
877
|
+
* })
|
|
878
|
+
* // Result: "Price: $99.99"
|
|
879
|
+
* ```
|
|
880
|
+
*/
|
|
881
|
+
|
|
882
|
+
declare const dynamicLabel: ConcernType<{
|
|
883
|
+
template: string;
|
|
884
|
+
}, string>;
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* Dynamic placeholder template concern
|
|
888
|
+
*
|
|
889
|
+
* Interpolates a template string with values from state.
|
|
890
|
+
* Automatically tracks all state paths referenced in the template.
|
|
891
|
+
*
|
|
892
|
+
* Returns the interpolated string.
|
|
893
|
+
*
|
|
894
|
+
* @example
|
|
895
|
+
* ```typescript
|
|
896
|
+
* store.useConcerns('my-concerns', {
|
|
897
|
+
* 'inputField': {
|
|
898
|
+
* dynamicPlaceholder: { template: "Enter {{field.name}}" }
|
|
899
|
+
* }
|
|
900
|
+
* })
|
|
901
|
+
* // Result: "Enter email address"
|
|
902
|
+
* ```
|
|
903
|
+
*/
|
|
904
|
+
|
|
905
|
+
declare const dynamicPlaceholder: ConcernType<{
|
|
906
|
+
template: string;
|
|
907
|
+
}, string>;
|
|
908
|
+
|
|
909
|
+
/**
|
|
910
|
+
* Dynamic tooltip template concern
|
|
911
|
+
*
|
|
912
|
+
* Interpolates a template string with values from state.
|
|
913
|
+
* Automatically tracks all state paths referenced in the template.
|
|
914
|
+
*
|
|
915
|
+
* Returns the interpolated string.
|
|
916
|
+
*
|
|
917
|
+
* @example
|
|
918
|
+
* ```typescript
|
|
919
|
+
* store.useConcerns('my-concerns', {
|
|
920
|
+
* 'strikePrice': {
|
|
921
|
+
* dynamicTooltip: { template: "Strike at {{market.spot}}" }
|
|
922
|
+
* }
|
|
923
|
+
* })
|
|
924
|
+
* // Result: "Strike at 105"
|
|
925
|
+
* ```
|
|
926
|
+
*/
|
|
927
|
+
|
|
928
|
+
declare const dynamicTooltip: ConcernType<{
|
|
929
|
+
template: string;
|
|
930
|
+
}, string>;
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* All pre-built concerns as a tuple (for use with findConcern)
|
|
934
|
+
*/
|
|
935
|
+
declare const prebuilts: readonly [ValidationStateConcern, ConcernType<{
|
|
936
|
+
boolLogic: BoolLogic<any>;
|
|
937
|
+
}, boolean>, ConcernType<{
|
|
938
|
+
boolLogic: BoolLogic<any>;
|
|
939
|
+
}, boolean>, ConcernType<{
|
|
940
|
+
boolLogic: BoolLogic<any>;
|
|
941
|
+
}, boolean>, ConcernType<{
|
|
942
|
+
template: string;
|
|
943
|
+
}, string>, ConcernType<{
|
|
944
|
+
template: string;
|
|
945
|
+
}, string>, ConcernType<{
|
|
946
|
+
template: string;
|
|
947
|
+
}, string>];
|
|
948
|
+
/**
|
|
949
|
+
* Namespace style access for pre-builts
|
|
950
|
+
*/
|
|
951
|
+
declare const prebuiltsNamespace: {
|
|
952
|
+
validationState: ValidationStateConcern;
|
|
953
|
+
disabledWhen: ConcernType<{
|
|
954
|
+
boolLogic: BoolLogic<any>;
|
|
955
|
+
}, boolean>;
|
|
956
|
+
readonlyWhen: ConcernType<{
|
|
957
|
+
boolLogic: BoolLogic<any>;
|
|
958
|
+
}, boolean>;
|
|
959
|
+
visibleWhen: ConcernType<{
|
|
960
|
+
boolLogic: BoolLogic<any>;
|
|
961
|
+
}, boolean>;
|
|
962
|
+
dynamicTooltip: ConcernType<{
|
|
963
|
+
template: string;
|
|
964
|
+
}, string>;
|
|
965
|
+
dynamicLabel: ConcernType<{
|
|
966
|
+
template: string;
|
|
967
|
+
}, string>;
|
|
968
|
+
dynamicPlaceholder: ConcernType<{
|
|
969
|
+
template: string;
|
|
970
|
+
}, string>;
|
|
971
|
+
};
|
|
972
|
+
|
|
973
|
+
type index_ValidationError = ValidationError;
|
|
974
|
+
type index_ValidationStateResult = ValidationStateResult;
|
|
975
|
+
declare const index_disabledWhen: typeof disabledWhen;
|
|
976
|
+
declare const index_dynamicLabel: typeof dynamicLabel;
|
|
977
|
+
declare const index_dynamicPlaceholder: typeof dynamicPlaceholder;
|
|
978
|
+
declare const index_dynamicTooltip: typeof dynamicTooltip;
|
|
979
|
+
declare const index_prebuilts: typeof prebuilts;
|
|
980
|
+
declare const index_prebuiltsNamespace: typeof prebuiltsNamespace;
|
|
981
|
+
declare const index_readonlyWhen: typeof readonlyWhen;
|
|
982
|
+
declare const index_validationState: typeof validationState;
|
|
983
|
+
declare const index_visibleWhen: typeof visibleWhen;
|
|
984
|
+
declare namespace index {
|
|
985
|
+
export { type index_ValidationError as ValidationError, type index_ValidationStateResult as ValidationStateResult, index_disabledWhen as disabledWhen, index_dynamicLabel as dynamicLabel, index_dynamicPlaceholder as dynamicPlaceholder, index_dynamicTooltip as dynamicTooltip, index_prebuilts as prebuilts, index_prebuiltsNamespace as prebuiltsNamespace, index_readonlyWhen as readonlyWhen, index_validationState as validationState, index_visibleWhen as visibleWhen };
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
/**
|
|
989
|
+
* SideEffects type definition
|
|
990
|
+
*
|
|
991
|
+
* Configuration types for side effects passed to useSideEffects hook.
|
|
992
|
+
* Simple tuple-based API: [path1, path2]
|
|
993
|
+
*/
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Clear path rule - "when trigger paths change, set target paths to null"
|
|
997
|
+
*
|
|
998
|
+
* Format: [triggers[], targets[], options?]
|
|
999
|
+
* - triggers: paths that activate the rule
|
|
1000
|
+
* - targets: paths to set to null
|
|
1001
|
+
* - expandMatch: if true, [*] in targets expands to ALL keys (not just matched key)
|
|
1002
|
+
*/
|
|
1003
|
+
type ClearPathRule<DATA extends object> = [
|
|
1004
|
+
DeepKey<DATA>[],
|
|
1005
|
+
DeepKey<DATA>[],
|
|
1006
|
+
{
|
|
1007
|
+
expandMatch?: boolean;
|
|
1008
|
+
}?
|
|
1009
|
+
];
|
|
1010
|
+
/**
|
|
1011
|
+
* Side effects configuration for useSideEffects hook
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* ```typescript
|
|
1015
|
+
* useSideEffects('my-effects', {
|
|
1016
|
+
* syncPaths: [
|
|
1017
|
+
* ['user.email', 'profile.email'],
|
|
1018
|
+
* ],
|
|
1019
|
+
* flipPaths: [
|
|
1020
|
+
* ['isActive', 'isInactive'],
|
|
1021
|
+
* ],
|
|
1022
|
+
* aggregations: [
|
|
1023
|
+
* ['total', 'price1'], // target <- source (target always first)
|
|
1024
|
+
* ['total', 'price2'],
|
|
1025
|
+
* ],
|
|
1026
|
+
* listeners: [
|
|
1027
|
+
* {
|
|
1028
|
+
* path: 'user.profile', // Watch user.profile.* changes
|
|
1029
|
+
* scope: 'user.profile', // Receive scoped state
|
|
1030
|
+
* fn: (changes, state) => {
|
|
1031
|
+
* // changes: [['name', 'Alice', {}]] // RELATIVE to scope
|
|
1032
|
+
* // state: user.profile sub-object
|
|
1033
|
+
* return [['status', 'updated', {}]] // Return FULL paths
|
|
1034
|
+
* }
|
|
1035
|
+
* },
|
|
1036
|
+
* {
|
|
1037
|
+
* path: 'user.profile.name', // Watch specific path
|
|
1038
|
+
* scope: null, // Get full state
|
|
1039
|
+
* fn: (changes, state) => {
|
|
1040
|
+
* // changes: [['user.profile.name', 'Alice', {}]] // FULL path
|
|
1041
|
+
* // state: full DATA object
|
|
1042
|
+
* return undefined
|
|
1043
|
+
* }
|
|
1044
|
+
* },
|
|
1045
|
+
* {
|
|
1046
|
+
* path: 'p.123.g.abc.data.strike', // Watch deep path
|
|
1047
|
+
* scope: 'p.123.g.abc', // Get parent scope
|
|
1048
|
+
* fn: (changes, state) => {
|
|
1049
|
+
* // changes: [['data.strike', value, {}]] // RELATIVE to scope
|
|
1050
|
+
* // state: p.123.g.abc object
|
|
1051
|
+
* return [['some.value.elsewhere', computed, {}]] // FULL path
|
|
1052
|
+
* }
|
|
1053
|
+
* }
|
|
1054
|
+
* ]
|
|
1055
|
+
* })
|
|
1056
|
+
* ```
|
|
1057
|
+
*/
|
|
1058
|
+
interface SideEffects<DATA extends object, META extends GenericMeta = GenericMeta> {
|
|
1059
|
+
/**
|
|
1060
|
+
* Sync paths - keeps specified paths synchronized
|
|
1061
|
+
* Format: [path1, path2] - both paths stay in sync
|
|
1062
|
+
*/
|
|
1063
|
+
syncPaths?: SyncPair<DATA>[];
|
|
1064
|
+
/**
|
|
1065
|
+
* Flip paths - keeps specified paths with opposite values
|
|
1066
|
+
* Format: [path1, path2] - paths have inverse boolean values
|
|
1067
|
+
*/
|
|
1068
|
+
flipPaths?: FlipPair<DATA>[];
|
|
1069
|
+
/**
|
|
1070
|
+
* Aggregations - aggregates sources into target
|
|
1071
|
+
* Format: [target, source] - target is ALWAYS first (left)
|
|
1072
|
+
* Multiple pairs can point to same target for multi-source aggregation
|
|
1073
|
+
*/
|
|
1074
|
+
aggregations?: AggregationPair<DATA>[];
|
|
1075
|
+
/**
|
|
1076
|
+
* Clear paths - "when X changes, set Y to null"
|
|
1077
|
+
* Format: [triggers[], targets[], { expandMatch?: boolean }?]
|
|
1078
|
+
* - Default: [*] in target correlates with trigger's [*] (same key)
|
|
1079
|
+
* - expandMatch: true → [*] in target expands to ALL keys
|
|
1080
|
+
*/
|
|
1081
|
+
clearPaths?: ClearPathRule<DATA>[];
|
|
1082
|
+
/**
|
|
1083
|
+
* Listeners - react to state changes with scoped state
|
|
1084
|
+
*/
|
|
1085
|
+
listeners?: ListenerRegistration<DATA, META>[];
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
declare const createGenericStore: <DATA extends object, META extends GenericMeta = GenericMeta, CONCERNS extends readonly any[] = typeof defaultConcerns>(config?: StoreConfig) => {
|
|
1089
|
+
Provider: {
|
|
1090
|
+
({ initialState: rawInitialState, children, }: ProviderProps<DATA>): react_jsx_runtime.JSX.Element | null;
|
|
1091
|
+
displayName: string;
|
|
1092
|
+
};
|
|
1093
|
+
useFieldStore: <P extends DeepKey<DATA>>(path: P) => {
|
|
1094
|
+
value: DeepValue<DATA, P>;
|
|
1095
|
+
setValue: (newValue: DeepValue<DATA, P>, meta?: META) => void;
|
|
1096
|
+
} & Record<string, unknown>;
|
|
1097
|
+
useStore: <P extends DeepKey<DATA>>(path: P) => [DeepValue<DATA, P>, (value: DeepValue<DATA, P>, meta?: META) => void];
|
|
1098
|
+
useJitStore: () => {
|
|
1099
|
+
proxyValue: DATA;
|
|
1100
|
+
setChanges: (changes: ArrayOfChanges<DATA, META>) => void;
|
|
1101
|
+
getState: () => DATA;
|
|
1102
|
+
};
|
|
1103
|
+
useSideEffects: (id: string, effects: SideEffects<DATA, META>) => void;
|
|
1104
|
+
useConcerns: (id: string, registration: ConcernRegistrationMap<DATA>, customConcerns?: readonly ConcernType<any, any>[]) => void;
|
|
1105
|
+
withConcerns: <SELECTION extends Partial<Record<string, boolean>>>(selection: SELECTION) => {
|
|
1106
|
+
useFieldStore: <P extends DeepKey<DATA>>(path: P) => {
|
|
1107
|
+
value: DATA extends readonly any[] ? DATA[number] : P extends `${infer First}.${infer Rest}` ? First extends keyof DATA ? DeepValue<DATA[First], Rest> : string extends keyof DATA ? First extends "[*]" ? DeepValue<DATA[keyof DATA & string], Rest> : unknown : unknown : P extends "[*]" ? string extends keyof DATA ? DATA[keyof DATA & string] : unknown : P extends keyof DATA ? DATA[P] : unknown;
|
|
1108
|
+
setValue: (newValue: DATA extends readonly any[] ? DATA[number] : P extends `${infer First}.${infer Rest}` ? First extends keyof DATA ? DeepValue<DATA[First], Rest> : string extends keyof DATA ? First extends "[*]" ? DeepValue<DATA[keyof DATA & string], Rest> : unknown : unknown : P extends "[*]" ? string extends keyof DATA ? DATA[keyof DATA & string] : unknown : P extends keyof DATA ? DATA[P] : unknown, meta?: META) => void;
|
|
1109
|
+
} & { [K in keyof SELECTION as SELECTION[K] extends true ? K : never]?: K extends CONCERNS[number]["name"] ? EvaluatedConcerns<CONCERNS>[K] : never; };
|
|
1110
|
+
};
|
|
1111
|
+
};
|
|
1112
|
+
|
|
1113
|
+
/**
|
|
1114
|
+
* Minimal field interface that useBufferedField accepts
|
|
1115
|
+
*/
|
|
1116
|
+
interface FieldInput$2<T> {
|
|
1117
|
+
value: T;
|
|
1118
|
+
setValue: (v: T) => void;
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* Extended interface with buffering capabilities
|
|
1122
|
+
*/
|
|
1123
|
+
interface BufferedField<T> extends FieldInput$2<T> {
|
|
1124
|
+
commit: () => void;
|
|
1125
|
+
cancel: () => void;
|
|
1126
|
+
isDirty: boolean;
|
|
1127
|
+
}
|
|
1128
|
+
/**
|
|
1129
|
+
* Adds buffered editing to any field hook.
|
|
1130
|
+
* Local changes are held until explicitly committed or cancelled.
|
|
1131
|
+
*
|
|
1132
|
+
* @param field - Field hook with { value, setValue }
|
|
1133
|
+
* @returns Buffered field with commit/cancel/isDirty
|
|
1134
|
+
*
|
|
1135
|
+
* @example
|
|
1136
|
+
* ```typescript
|
|
1137
|
+
* const field = useFieldStore('user.name')
|
|
1138
|
+
* const buffered = useBufferedField(field)
|
|
1139
|
+
*
|
|
1140
|
+
* // User types - updates local only
|
|
1141
|
+
* buffered.setValue('new value')
|
|
1142
|
+
*
|
|
1143
|
+
* // Enter/Tab - commit to store
|
|
1144
|
+
* buffered.commit()
|
|
1145
|
+
*
|
|
1146
|
+
* // Esc - revert to stored value
|
|
1147
|
+
* buffered.cancel()
|
|
1148
|
+
*
|
|
1149
|
+
* // Check if user has unsaved changes
|
|
1150
|
+
* if (buffered.isDirty) { ... }
|
|
1151
|
+
* ```
|
|
1152
|
+
*/
|
|
1153
|
+
declare const useBufferedField: <T>(field: FieldInput$2<T>) => BufferedField<T>;
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* Option for keyboard selection
|
|
1157
|
+
*/
|
|
1158
|
+
interface SelectOption<T> {
|
|
1159
|
+
value: T;
|
|
1160
|
+
label: string;
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Configuration for keyboard select behavior
|
|
1164
|
+
*/
|
|
1165
|
+
interface KeyboardSelectConfig<T> {
|
|
1166
|
+
/** Available options to select from */
|
|
1167
|
+
options: SelectOption<T>[];
|
|
1168
|
+
/** Time window to accumulate keystrokes (ms). Default: 500 */
|
|
1169
|
+
debounceMs?: number;
|
|
1170
|
+
/** Match from start of label only. Default: true */
|
|
1171
|
+
matchFromStart?: boolean;
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Minimal field interface that useKeyboardSelect accepts
|
|
1175
|
+
*/
|
|
1176
|
+
interface FieldInput$1<T> {
|
|
1177
|
+
value: T;
|
|
1178
|
+
setValue: (v: T) => void;
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Adds keyboard-driven selection to any field hook.
|
|
1182
|
+
* Typing letters auto-selects matching options.
|
|
1183
|
+
*
|
|
1184
|
+
* @param field - Field hook with { value, setValue, ...rest }
|
|
1185
|
+
* @param config - Options and behavior configuration
|
|
1186
|
+
* @returns Field with onKeyDown handler added
|
|
1187
|
+
*
|
|
1188
|
+
* @example
|
|
1189
|
+
* ```typescript
|
|
1190
|
+
* const field = useFieldStore('user.country')
|
|
1191
|
+
* const { onKeyDown, ...rest } = useKeyboardSelect(field, {
|
|
1192
|
+
* options: [
|
|
1193
|
+
* { value: 'us', label: 'United States' },
|
|
1194
|
+
* { value: 'uk', label: 'United Kingdom' },
|
|
1195
|
+
* { value: 'ca', label: 'Canada' },
|
|
1196
|
+
* ]
|
|
1197
|
+
* })
|
|
1198
|
+
*
|
|
1199
|
+
* // User types "u" -> selects "United States"
|
|
1200
|
+
* // User types "un" quickly -> still "United States"
|
|
1201
|
+
* // User types "c" -> selects "Canada"
|
|
1202
|
+
*
|
|
1203
|
+
* <input onKeyDown={onKeyDown} {...rest} />
|
|
1204
|
+
* ```
|
|
1205
|
+
*/
|
|
1206
|
+
declare const useKeyboardSelect: <T, TField extends FieldInput$1<T>>(field: TField, config: KeyboardSelectConfig<T>) => TField & {
|
|
1207
|
+
onKeyDown: (e: React.KeyboardEvent) => void;
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
/**
|
|
1211
|
+
* Minimal field interface for throttling
|
|
1212
|
+
* Supports setValue with optional additional arguments (e.g., meta)
|
|
1213
|
+
*/
|
|
1214
|
+
interface ThrottleFieldInput<T, Args extends unknown[] = unknown[]> {
|
|
1215
|
+
value: T;
|
|
1216
|
+
setValue: (v: T, ...args: Args) => void;
|
|
1217
|
+
}
|
|
1218
|
+
/**
|
|
1219
|
+
* Throttle configuration
|
|
1220
|
+
*/
|
|
1221
|
+
interface ThrottleConfig {
|
|
1222
|
+
/** Minimum milliseconds between setValue calls to the underlying field */
|
|
1223
|
+
ms: number;
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Adds throttling to any field hook.
|
|
1227
|
+
* First setValue executes immediately, subsequent calls are rate-limited.
|
|
1228
|
+
* Last value wins when multiple calls occur within the throttle window.
|
|
1229
|
+
* Preserves the full setValue signature including additional arguments like meta.
|
|
1230
|
+
*
|
|
1231
|
+
* @param field - Field hook with { value, setValue, ...rest }
|
|
1232
|
+
* @param config - Throttle configuration { ms }
|
|
1233
|
+
* @returns Field with throttled setValue, plus any passthrough props
|
|
1234
|
+
*
|
|
1235
|
+
* @example
|
|
1236
|
+
* ```typescript
|
|
1237
|
+
* const field = useFieldStore('spotPrice')
|
|
1238
|
+
* const throttled = useThrottledField(field, { ms: 100 })
|
|
1239
|
+
*
|
|
1240
|
+
* // Rapid updates from WebSocket
|
|
1241
|
+
* throttled.setValue(1.234) // Immediate
|
|
1242
|
+
* throttled.setValue(1.235) // Buffered
|
|
1243
|
+
* throttled.setValue(1.236) // Buffered (replaces 1.235)
|
|
1244
|
+
* // After 100ms: 1.236 is applied
|
|
1245
|
+
* ```
|
|
1246
|
+
*
|
|
1247
|
+
* @example
|
|
1248
|
+
* ```typescript
|
|
1249
|
+
* // With meta argument
|
|
1250
|
+
* const field = useFieldStore('price')
|
|
1251
|
+
* const throttled = useThrottledField(field, { ms: 100 })
|
|
1252
|
+
* throttled.setValue(42, { source: 'websocket' })
|
|
1253
|
+
* ```
|
|
1254
|
+
*
|
|
1255
|
+
* @example
|
|
1256
|
+
* ```typescript
|
|
1257
|
+
* // Composable with other wrappers
|
|
1258
|
+
* const field = useFieldStore('price')
|
|
1259
|
+
* const transformed = useTransformedField(field, {
|
|
1260
|
+
* to: (cents) => cents / 100,
|
|
1261
|
+
* from: (dollars) => Math.round(dollars * 100)
|
|
1262
|
+
* })
|
|
1263
|
+
* const throttled = useThrottledField(transformed, { ms: 50 })
|
|
1264
|
+
* ```
|
|
1265
|
+
*/
|
|
1266
|
+
declare const useThrottledField: <T, Args extends unknown[] = unknown[], TField extends ThrottleFieldInput<T, Args> = ThrottleFieldInput<T, Args>>(field: TField, config: ThrottleConfig) => TField;
|
|
1267
|
+
|
|
1268
|
+
/**
|
|
1269
|
+
* Transform configuration for field values
|
|
1270
|
+
*/
|
|
1271
|
+
interface TransformConfig<TStored, TDisplay> {
|
|
1272
|
+
/** Transform from stored value to display value */
|
|
1273
|
+
to: (stored: TStored) => TDisplay;
|
|
1274
|
+
/** Transform from display value to stored value */
|
|
1275
|
+
from: (display: TDisplay) => TStored;
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Minimal field interface that useTransformedField accepts
|
|
1279
|
+
*/
|
|
1280
|
+
interface FieldInput<T> {
|
|
1281
|
+
value: T;
|
|
1282
|
+
setValue: (v: T) => void;
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* Adds value transformation to any field hook.
|
|
1286
|
+
* Converts between storage format and display format.
|
|
1287
|
+
* Passes through any additional properties from the input field.
|
|
1288
|
+
*
|
|
1289
|
+
* @param field - Field hook with { value, setValue, ...rest }
|
|
1290
|
+
* @param config - Transform functions { to, from }
|
|
1291
|
+
* @returns Field with transformed types, plus any passthrough props
|
|
1292
|
+
*
|
|
1293
|
+
* @example
|
|
1294
|
+
* ```typescript
|
|
1295
|
+
* const field = useFieldStore('user.birthdate')
|
|
1296
|
+
* const formatted = useTransformedField(field, {
|
|
1297
|
+
* to: (iso) => format(new Date(iso), 'MM/dd/yyyy'),
|
|
1298
|
+
* from: (display) => parse(display, 'MM/dd/yyyy').toISOString()
|
|
1299
|
+
* })
|
|
1300
|
+
*
|
|
1301
|
+
* // formatted.value is "01/15/2024"
|
|
1302
|
+
* // formatted.setValue("01/20/2024") stores ISO string
|
|
1303
|
+
* ```
|
|
1304
|
+
*
|
|
1305
|
+
* @example
|
|
1306
|
+
* ```typescript
|
|
1307
|
+
* // Works with buffered fields - passes through commit/cancel/isDirty
|
|
1308
|
+
* const field = useFieldStore('price')
|
|
1309
|
+
* const buffered = useBufferedField(field)
|
|
1310
|
+
* const formatted = useTransformedField(buffered, {
|
|
1311
|
+
* to: (cents) => (cents / 100).toFixed(2),
|
|
1312
|
+
* from: (dollars) => Math.round(parseFloat(dollars) * 100)
|
|
1313
|
+
* })
|
|
1314
|
+
*
|
|
1315
|
+
* formatted.setValue("15.99") // local only
|
|
1316
|
+
* formatted.commit() // stores 1599
|
|
1317
|
+
* ```
|
|
1318
|
+
*/
|
|
1319
|
+
declare const useTransformedField: <TStored, TDisplay, TField extends FieldInput<TStored>>(field: TField, config: TransformConfig<TStored, TDisplay>) => Omit<TField, "value" | "setValue"> & FieldInput<TDisplay>;
|
|
1320
|
+
|
|
1321
|
+
/** Legacy JS implementation - uses JS graph structure */
|
|
1322
|
+
declare const registerFlipPair: <DATA extends object, META extends GenericMeta = GenericMeta>(store: StoreInstance<DATA, META>, path1: string & {}, path2: string & {}) => (() => void);
|
|
1323
|
+
|
|
1324
|
+
/** Legacy JS implementation - uses JS listener maps and sorted paths */
|
|
1325
|
+
declare const registerListenerLegacy: <DATA extends object, META extends GenericMeta = GenericMeta>(store: StoreInstance<DATA, META>, registration: ListenerRegistration<DATA, META>) => (() => void);
|
|
1326
|
+
|
|
1327
|
+
/**
|
|
1328
|
+
* Legacy batch version of registerSyncPair. Adds all edges first, then computes
|
|
1329
|
+
* initial sync changes across all final groups and calls processChanges once.
|
|
1330
|
+
* This avoids cascading effect re-evaluations when registering many pairs.
|
|
1331
|
+
*/
|
|
1332
|
+
declare const registerSyncPairsBatch: <DATA extends object, META extends GenericMeta = GenericMeta>(store: StoreInstance<DATA, META>, pairs: [string & {}, string & {}][]) => (() => void);
|
|
1333
|
+
|
|
1334
|
+
declare const registerSideEffects: <DATA extends object, META extends GenericMeta = GenericMeta>(store: StoreInstance<DATA, META>, id: string, effects: SideEffects<DATA, META>) => (() => void);
|
|
1335
|
+
|
|
1336
|
+
declare const evaluateBoolLogic: <STATE extends object>(logic: BoolLogic<STATE>, state: STATE) => boolean;
|
|
1337
|
+
|
|
1338
|
+
/**
|
|
1339
|
+
* Template string interpolation utilities
|
|
1340
|
+
*
|
|
1341
|
+
* Core utility for interpolating state values into template strings.
|
|
1342
|
+
* Used by concerns and side effects for dynamic text generation.
|
|
1343
|
+
*/
|
|
1344
|
+
/**
|
|
1345
|
+
* Extract all {{path}} placeholders from a template string
|
|
1346
|
+
*
|
|
1347
|
+
* @param template The template string with {{path}} placeholders
|
|
1348
|
+
* @returns Array of path strings found in the template
|
|
1349
|
+
*
|
|
1350
|
+
* @example
|
|
1351
|
+
* extractPlaceholders("Hello {{user.name}}, you have {{count}} messages")
|
|
1352
|
+
* // ["user.name", "count"]
|
|
1353
|
+
*/
|
|
1354
|
+
declare const extractPlaceholders: (template: string) => string[];
|
|
1355
|
+
/**
|
|
1356
|
+
* Interpolate {{path}} placeholders with values from state
|
|
1357
|
+
*
|
|
1358
|
+
* Replaces {{path.to.value}} with actual values from state.
|
|
1359
|
+
* Only replaces if value is a string, number, or boolean.
|
|
1360
|
+
* Missing/null/undefined/object values leave the original {{path}} for debugging.
|
|
1361
|
+
*
|
|
1362
|
+
* @param template The template string with {{path}} placeholders
|
|
1363
|
+
* @param state The state object to read values from
|
|
1364
|
+
* @returns The interpolated string
|
|
1365
|
+
*
|
|
1366
|
+
* @example
|
|
1367
|
+
* interpolateTemplate("Value is {{market.spot}}", state)
|
|
1368
|
+
* // "Value is 105"
|
|
1369
|
+
*
|
|
1370
|
+
* @example
|
|
1371
|
+
* interpolateTemplate("Hello {{user.name}}, missing: {{invalid.path}}", state)
|
|
1372
|
+
* // "Hello Alice, missing: {{invalid.path}}"
|
|
1373
|
+
*/
|
|
1374
|
+
declare const interpolateTemplate: <STATE extends object>(template: string, state: STATE) => string;
|
|
1375
|
+
|
|
1376
|
+
/**
|
|
1377
|
+
* Deep access utilities for safe nested object access
|
|
1378
|
+
*
|
|
1379
|
+
* Uses native Reflect for reads (~2x faster) and writes (~3x faster) vs lodash.
|
|
1380
|
+
* Maintains valtio reactivity when setting values.
|
|
1381
|
+
*/
|
|
1382
|
+
|
|
1383
|
+
/**
|
|
1384
|
+
* Unified namespace for dot notation path operations
|
|
1385
|
+
*
|
|
1386
|
+
* Provides type-safe and unsafe variants for working with nested objects
|
|
1387
|
+
* using dot notation paths (e.g., 'user.address.city')
|
|
1388
|
+
*/
|
|
1389
|
+
declare const dot: {
|
|
1390
|
+
get: <T extends object, P extends DeepKey<T>>(obj: T, path: P) => DeepValue<T, P>;
|
|
1391
|
+
get__unsafe: <P extends string>(obj: unknown, path: P) => unknown;
|
|
1392
|
+
set: <T extends object, P extends DeepKey<T>>(obj: T, path: P, value: DeepValue<T, P>) => void;
|
|
1393
|
+
set__unsafe: <T extends object, P extends string>(obj: T, path: P, value: unknown) => void;
|
|
1394
|
+
has: <T extends object, P extends DeepKey<T>>(obj: T, path: P) => boolean;
|
|
1395
|
+
same: <T extends object, P extends string>(obj: T, ...paths: P[]) => boolean;
|
|
1396
|
+
};
|
|
1397
|
+
|
|
1398
|
+
/**
|
|
1399
|
+
* Hash key utilities
|
|
1400
|
+
*
|
|
1401
|
+
* Utilities for working with hash key notation in Record-based paths.
|
|
1402
|
+
* Hash keys ([*]) are type-level markers for indexing into Records/HashMaps.
|
|
1403
|
+
*
|
|
1404
|
+
* @example
|
|
1405
|
+
* ```typescript
|
|
1406
|
+
* import { _, hashKey } from '@sladg/apex-state'
|
|
1407
|
+
*
|
|
1408
|
+
* // Use _ in template strings
|
|
1409
|
+
* const path = `users.${_('u1')}.posts.${_('p1')}.name`
|
|
1410
|
+
*
|
|
1411
|
+
* // Use hashKey namespace for validation
|
|
1412
|
+
* hashKey.rejectDynamic(path)
|
|
1413
|
+
* ```
|
|
1414
|
+
*/
|
|
1415
|
+
|
|
1416
|
+
/**
|
|
1417
|
+
* Converts a concrete ID to hash key notation for inline template string usage
|
|
1418
|
+
* Returns the concrete ID typed as HASH_KEY for Record/HashMap indexing
|
|
1419
|
+
*
|
|
1420
|
+
* @param id - The concrete ID (e.g., "u1", "p1", "c1")
|
|
1421
|
+
* @returns The concrete ID typed as HASH_KEY
|
|
1422
|
+
*
|
|
1423
|
+
* @example
|
|
1424
|
+
* ```typescript
|
|
1425
|
+
* const path = `users.${_('u1')}.posts.${_('p1')}.name`
|
|
1426
|
+
* // → "users.u1.posts.p1.name" (typed as containing HASH_KEY)
|
|
1427
|
+
* ```
|
|
1428
|
+
*/
|
|
1429
|
+
declare const _: (id: string) => HASH_KEY;
|
|
1430
|
+
/**
|
|
1431
|
+
* Hash key utilities namespace
|
|
1432
|
+
*
|
|
1433
|
+
* Provides utilities for working with hash keys in Record-based paths
|
|
1434
|
+
*/
|
|
1435
|
+
declare const hashKey: {
|
|
1436
|
+
/** Reject paths with dynamic hash keys */
|
|
1437
|
+
readonly rejectDynamic: <P extends string>(path: P) => void;
|
|
1438
|
+
/** Alias for _ function */
|
|
1439
|
+
readonly _: (id: string) => HASH_KEY;
|
|
1440
|
+
};
|
|
1441
|
+
|
|
1442
|
+
/**
|
|
1443
|
+
* Type checking utilities — similar to lodash type guards
|
|
1444
|
+
*
|
|
1445
|
+
* Provides type-safe predicates for common type checks with TypeScript support
|
|
1446
|
+
*/
|
|
1447
|
+
/**
|
|
1448
|
+
* Unified namespace for type checking
|
|
1449
|
+
*
|
|
1450
|
+
* @example
|
|
1451
|
+
* ```typescript
|
|
1452
|
+
* import { is } from './utils/is'
|
|
1453
|
+
*
|
|
1454
|
+
* if (is.object(value)) { ... }
|
|
1455
|
+
* if (is.array(value)) { ... }
|
|
1456
|
+
* if (is.nil(value)) { ... }
|
|
1457
|
+
*
|
|
1458
|
+
* // Negated versions
|
|
1459
|
+
* if (is.not.object(value)) { ... }
|
|
1460
|
+
* if (is.not.array(value)) { ... }
|
|
1461
|
+
* ```
|
|
1462
|
+
*/
|
|
1463
|
+
declare const is: {
|
|
1464
|
+
nil: (value: unknown) => value is null | undefined;
|
|
1465
|
+
undefined: (value: unknown) => value is undefined;
|
|
1466
|
+
null: (value: unknown) => value is null;
|
|
1467
|
+
object: (value: unknown) => value is Record<string, unknown>;
|
|
1468
|
+
array: (value: unknown) => value is unknown[];
|
|
1469
|
+
string: (value: unknown) => value is string;
|
|
1470
|
+
number: (value: unknown) => value is number;
|
|
1471
|
+
boolean: (value: unknown) => value is boolean;
|
|
1472
|
+
function: (value: unknown) => value is (...args: unknown[]) => unknown;
|
|
1473
|
+
symbol: (value: unknown) => value is symbol;
|
|
1474
|
+
date: (value: unknown) => value is Date;
|
|
1475
|
+
regexp: (value: unknown) => value is RegExp;
|
|
1476
|
+
primitive: (value: unknown) => value is string | number | boolean | symbol | bigint | null | undefined;
|
|
1477
|
+
empty: (value: unknown) => boolean;
|
|
1478
|
+
equal: (a: unknown, b: unknown) => boolean;
|
|
1479
|
+
not: {
|
|
1480
|
+
nil: <T>(value: T | null | undefined) => value is T;
|
|
1481
|
+
undefined: <T>(value: T | undefined) => value is T;
|
|
1482
|
+
null: <T>(value: T | null) => value is T;
|
|
1483
|
+
object: (value: unknown) => boolean;
|
|
1484
|
+
array: (value: unknown) => boolean;
|
|
1485
|
+
string: (value: unknown) => boolean;
|
|
1486
|
+
number: (value: unknown) => boolean;
|
|
1487
|
+
boolean: (value: unknown) => boolean;
|
|
1488
|
+
function: (value: unknown) => boolean;
|
|
1489
|
+
symbol: (value: unknown) => boolean;
|
|
1490
|
+
date: (value: unknown) => boolean;
|
|
1491
|
+
regexp: (value: unknown) => boolean;
|
|
1492
|
+
primitive: (value: unknown) => boolean;
|
|
1493
|
+
empty: (value: unknown) => boolean;
|
|
1494
|
+
equal: (a: unknown, b: unknown) => boolean;
|
|
1495
|
+
};
|
|
1496
|
+
};
|
|
1497
|
+
|
|
1498
|
+
/**
|
|
1499
|
+
* Apply Changes Utility
|
|
1500
|
+
*
|
|
1501
|
+
* Applies an array of changes to an object, returning a new object.
|
|
1502
|
+
*/
|
|
1503
|
+
|
|
1504
|
+
/**
|
|
1505
|
+
* Applies changes to an object, returning a new object with changes applied.
|
|
1506
|
+
* Does not mutate the original object.
|
|
1507
|
+
*
|
|
1508
|
+
* @param obj - Source object
|
|
1509
|
+
* @param changes - Array of [path, value, meta] tuples
|
|
1510
|
+
* @returns New object with changes applied
|
|
1511
|
+
*
|
|
1512
|
+
* @example
|
|
1513
|
+
* ```typescript
|
|
1514
|
+
* const state = { user: { name: 'Alice', age: 30 } }
|
|
1515
|
+
* const changes: ArrayOfChanges<typeof state> = [
|
|
1516
|
+
* ['user.name', 'Bob', {}],
|
|
1517
|
+
* ['user.age', 31, {}],
|
|
1518
|
+
* ]
|
|
1519
|
+
*
|
|
1520
|
+
* const newState = applyChangesToObject(state, changes)
|
|
1521
|
+
* // newState: { user: { name: 'Bob', age: 31 } }
|
|
1522
|
+
* // state is unchanged
|
|
1523
|
+
* ```
|
|
1524
|
+
*/
|
|
1525
|
+
declare const applyChangesToObject: <T extends object>(obj: T, changes: ArrayOfChanges<T>) => T;
|
|
1526
|
+
|
|
1527
|
+
export { type Aggregation, type AggregationPair, type ArrayOfChanges, type BaseConcernProps, type BoolLogic, type BufferedField, type ConcernRegistration, type ConcernRegistrationMap, type ConcernType, type DebugConfig, type DebugTrack, type DebugTrackEntry, type DeepKey, type DeepKeyFiltered, type DeepPartial, type DeepRequired, type DeepValue, type EvaluatedConcerns, type ExtractEvaluateReturn, type FieldInput$2 as FieldInput, type FlipPair, type GenericMeta, type KeyboardSelectConfig, type ListenerRegistration, type OnStateListener, type PathsWithSameValueAs, type ProviderProps, type SelectOption, type SideEffects, type StoreConfig, type StoreInstance, type SyncPair, type ThrottleConfig, type TransformConfig, type ValidationError, type ValidationStateResult, _, applyChangesToObject, createGenericStore, defaultConcerns, dot, evaluateBoolLogic, extractPlaceholders, findConcern, hashKey, interpolateTemplate, is, index as prebuilts, registerFlipPair, registerListenerLegacy, registerSideEffects, registerSyncPairsBatch, useBufferedField, useKeyboardSelect, useThrottledField, useTransformedField };
|