@doeixd/machine 0.0.18 → 0.0.19
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/README.md +164 -3
- package/dist/cjs/development/core.js.map +2 -2
- package/dist/cjs/development/extract.js +500 -0
- package/dist/cjs/development/extract.js.map +7 -0
- package/dist/cjs/development/index.js +135 -477
- package/dist/cjs/development/index.js.map +4 -4
- package/dist/cjs/production/extract.js +5 -0
- package/dist/cjs/production/index.js +4 -5
- package/dist/esm/development/core.js.map +2 -2
- package/dist/esm/development/extract.js +486 -0
- package/dist/esm/development/extract.js.map +7 -0
- package/dist/esm/development/index.js +135 -484
- package/dist/esm/development/index.js.map +4 -4
- package/dist/esm/production/extract.js +5 -0
- package/dist/esm/production/index.js +4 -5
- package/dist/types/index.d.ts +45 -8
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/matcher.d.ts +318 -0
- package/dist/types/matcher.d.ts.map +1 -0
- package/package.json +13 -1
- package/src/index.ts +73 -9
- package/src/matcher.ts +544 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Advanced Pattern Matching for State Machines
|
|
3
|
+
* @description
|
|
4
|
+
* Provides type-safe pattern matching utilities for discriminating between machine types.
|
|
5
|
+
* Supports three complementary APIs: type guards, exhaustive pattern matching, and simple matching.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Define a matcher for class-based machines
|
|
10
|
+
* const match = createMatcher(
|
|
11
|
+
* classCase('idle', IdleMachine),
|
|
12
|
+
* classCase('loading', LoadingMachine),
|
|
13
|
+
* classCase('success', SuccessMachine)
|
|
14
|
+
* );
|
|
15
|
+
*
|
|
16
|
+
* // API 1: Type Guards
|
|
17
|
+
* if (match.is.loading(machine)) {
|
|
18
|
+
* // machine is narrowed to LoadingMachine
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* // API 2: Exhaustive Pattern Matching
|
|
22
|
+
* const result = match.when(machine).is<string>(
|
|
23
|
+
* match.case.idle(() => 'idle'),
|
|
24
|
+
* match.case.loading(() => 'loading'),
|
|
25
|
+
* match.case.success(m => m.context.data),
|
|
26
|
+
* match.exhaustive
|
|
27
|
+
* );
|
|
28
|
+
*
|
|
29
|
+
* // API 3: Simple Match
|
|
30
|
+
* const name = match(machine); // 'idle' | 'loading' | 'success' | null
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
import type { Machine, Context } from './index';
|
|
34
|
+
/**
|
|
35
|
+
* A matcher case tuple that defines a state pattern.
|
|
36
|
+
*
|
|
37
|
+
* @template Name - The unique name for this case (used for type guards and pattern matching)
|
|
38
|
+
* @template M - The machine type this case matches
|
|
39
|
+
* @template Pred - The predicate function that determines if a machine matches this case
|
|
40
|
+
*/
|
|
41
|
+
export type MatcherCase<Name extends string, M, Pred extends (m: any) => m is M> = readonly [
|
|
42
|
+
name: Name,
|
|
43
|
+
machineType: M,
|
|
44
|
+
predicate: Pred
|
|
45
|
+
];
|
|
46
|
+
/**
|
|
47
|
+
* Extracts the machine type from a MatcherCase.
|
|
48
|
+
*/
|
|
49
|
+
type CaseToMachine<C> = C extends MatcherCase<any, infer M, any> ? M : never;
|
|
50
|
+
/**
|
|
51
|
+
* Extracts the case name from a MatcherCase.
|
|
52
|
+
*/
|
|
53
|
+
type CaseToName<C> = C extends MatcherCase<infer Name, any, any> ? Name : never;
|
|
54
|
+
/**
|
|
55
|
+
* Builds a mapping from case names to their machine types.
|
|
56
|
+
*/
|
|
57
|
+
export type CasesToMapping<Cases extends readonly MatcherCase<any, any, any>[]> = {
|
|
58
|
+
[C in Cases[number] as CaseToName<C>]: CaseToMachine<C>;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Creates a union of all possible machine types from the cases.
|
|
62
|
+
*/
|
|
63
|
+
export type MatcherUnion<Cases extends readonly MatcherCase<any, any, any>[]> = Cases[number] extends MatcherCase<any, infer M, any> ? M : never;
|
|
64
|
+
/**
|
|
65
|
+
* Extracts the union of all case names.
|
|
66
|
+
*/
|
|
67
|
+
export type CaseNames<Cases extends readonly MatcherCase<any, any, any>[]> = CaseToName<Cases[number]>;
|
|
68
|
+
/**
|
|
69
|
+
* A branded type representing a case handler in pattern matching.
|
|
70
|
+
* This is used internally to track which cases have been handled.
|
|
71
|
+
*/
|
|
72
|
+
export type CaseHandler<Name extends string, M, R> = {
|
|
73
|
+
readonly __brand: 'CaseHandler';
|
|
74
|
+
readonly __name: Name;
|
|
75
|
+
readonly __machine: M;
|
|
76
|
+
readonly __return: R;
|
|
77
|
+
readonly handler: (machine: M) => R;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Exhaustiveness marker - signals that all cases must be handled.
|
|
81
|
+
*/
|
|
82
|
+
export type ExhaustivenessMarker = {
|
|
83
|
+
readonly __exhaustive: true;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Extracts machine types from an array of case handlers.
|
|
87
|
+
*/
|
|
88
|
+
type HandledMachines<Handlers extends readonly any[]> = Handlers extends readonly [infer H, ...infer Rest] ? (H extends CaseHandler<any, infer M, any> ? M : never) | HandledMachines<Rest> : never;
|
|
89
|
+
/**
|
|
90
|
+
* Checks if all machine types in Union have been handled.
|
|
91
|
+
* Returns true if exhaustive, otherwise returns an error type with missing cases.
|
|
92
|
+
*/
|
|
93
|
+
export type IsExhaustive<Union, Handled> = Exclude<Union, Handled> extends never ? true : {
|
|
94
|
+
readonly __error: 'Non-exhaustive match - missing cases';
|
|
95
|
+
readonly __missing: Exclude<Union, Handled>;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Pattern matching builder returned by matcher.when().
|
|
99
|
+
*/
|
|
100
|
+
export interface WhenBuilder<_Cases extends readonly MatcherCase<any, any, any>[], M> {
|
|
101
|
+
/**
|
|
102
|
+
* Execute pattern matching with exhaustiveness checking.
|
|
103
|
+
*
|
|
104
|
+
* @template R - The return type of all handlers
|
|
105
|
+
* @param handlers - Array of case handlers followed by exhaustiveness marker
|
|
106
|
+
* @returns The result of the matched handler, or compile error if not exhaustive
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* match.when(machine).is<string>(
|
|
111
|
+
* match.case.idle(() => 'idle'),
|
|
112
|
+
* match.case.loading(() => 'loading'),
|
|
113
|
+
* match.exhaustive
|
|
114
|
+
* );
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
is<R>(...handlers: [...any[], ExhaustivenessMarker]): IsExhaustive<M, HandledMachines<typeof handlers>> extends true ? R : IsExhaustive<M, HandledMachines<typeof handlers>>;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The main Matcher interface with three APIs.
|
|
121
|
+
*/
|
|
122
|
+
export interface Matcher<Cases extends readonly MatcherCase<any, any, any>[]> {
|
|
123
|
+
/**
|
|
124
|
+
* API 1: Type guard access via dynamic properties.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* if (match.is.loading(machine)) {
|
|
129
|
+
* // machine is narrowed to LoadingMachine
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
readonly is: {
|
|
134
|
+
[Name in CaseNames<Cases>]: <M>(machine: M) => machine is Extract<M, CasesToMapping<Cases>[Name]>;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* API 2a: Pattern matching builder.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* match.when(machine).is<string>(
|
|
142
|
+
* match.case.idle(() => 'idle'),
|
|
143
|
+
* match.case.loading(() => 'loading'),
|
|
144
|
+
* match.exhaustive
|
|
145
|
+
* );
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
when: <M>(machine: M) => WhenBuilder<Cases, M>;
|
|
149
|
+
/**
|
|
150
|
+
* API 2b: Case handler creator for pattern matching.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* match.case.loading((m) => `Loading: ${m.context.startTime}`)
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
readonly case: {
|
|
158
|
+
[Name in CaseNames<Cases>]: <R>(handler: (machine: CasesToMapping<Cases>[Name]) => R) => CaseHandler<Name, CasesToMapping<Cases>[Name], R>;
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* API 2c: Exhaustiveness marker for pattern matching.
|
|
162
|
+
*/
|
|
163
|
+
readonly exhaustive: ExhaustivenessMarker;
|
|
164
|
+
/**
|
|
165
|
+
* API 3: Simple match - returns the name of the matched case or null.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* const name = match(machine); // 'idle' | 'loading' | 'success' | null
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
<M>(machine: M): M extends MatcherUnion<Cases> ? CaseNames<Cases> | null : null;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Creates a type-safe matcher for discriminating between machine types.
|
|
176
|
+
*
|
|
177
|
+
* @template Cases - Tuple of [name, MachineType, predicate] configurations
|
|
178
|
+
* @param cases - Array of matcher case definitions
|
|
179
|
+
* @returns A matcher object with three APIs: is (type guards), when (pattern matching), and direct call (simple match)
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* // Class-based matching
|
|
184
|
+
* const match = createMatcher(
|
|
185
|
+
* ['idle', IdleMachine, (m): m is IdleMachine => m instanceof IdleMachine],
|
|
186
|
+
* ['loading', LoadingMachine, (m): m is LoadingMachine => m instanceof LoadingMachine]
|
|
187
|
+
* );
|
|
188
|
+
*
|
|
189
|
+
* // Or use helper functions
|
|
190
|
+
* const match = createMatcher(
|
|
191
|
+
* classCase('idle', IdleMachine),
|
|
192
|
+
* classCase('loading', LoadingMachine)
|
|
193
|
+
* );
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export declare function createMatcher<const Cases extends readonly MatcherCase<string, any, (m: any) => m is any>[]>(...cases: Cases): Matcher<Cases>;
|
|
197
|
+
/**
|
|
198
|
+
* Creates a class-based matcher case using instanceof checking.
|
|
199
|
+
* This is the most common pattern for Type-State machines.
|
|
200
|
+
*
|
|
201
|
+
* @template Name - The unique name for this case
|
|
202
|
+
* @template T - The class constructor
|
|
203
|
+
* @param name - The name to use for this case
|
|
204
|
+
* @param machineClass - The class to check with instanceof
|
|
205
|
+
* @returns A matcher case tuple
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* const match = createMatcher(
|
|
210
|
+
* classCase('idle', IdleMachine),
|
|
211
|
+
* classCase('loading', LoadingMachine),
|
|
212
|
+
* classCase('success', SuccessMachine)
|
|
213
|
+
* );
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
export declare function classCase<Name extends string, T extends abstract new (...args: any[]) => any>(name: Name, machineClass: T): MatcherCase<Name, InstanceType<T>, (m: any) => m is InstanceType<T>>;
|
|
217
|
+
/**
|
|
218
|
+
* Creates a discriminated union matcher case based on a context property.
|
|
219
|
+
* This integrates with the existing hasState utility for context-based discrimination.
|
|
220
|
+
*
|
|
221
|
+
* @template Name - The unique name for this case
|
|
222
|
+
* @template M - The machine type (use Machine<YourContextUnion> for proper narrowing)
|
|
223
|
+
* @template K - The context key to check
|
|
224
|
+
* @template V - The value to match
|
|
225
|
+
* @param name - The name to use for this case
|
|
226
|
+
* @param key - The context property to check
|
|
227
|
+
* @param value - The value the property should equal
|
|
228
|
+
* @returns A matcher case tuple
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* type FetchContext =
|
|
233
|
+
* | { status: 'idle' }
|
|
234
|
+
* | { status: 'loading' }
|
|
235
|
+
* | { status: 'success'; data: string };
|
|
236
|
+
*
|
|
237
|
+
* const match = createMatcher(
|
|
238
|
+
* discriminantCase<'idle', Machine<FetchContext>, 'status', 'idle'>('idle', 'status', 'idle'),
|
|
239
|
+
* discriminantCase<'loading', Machine<FetchContext>, 'status', 'loading'>('loading', 'status', 'loading'),
|
|
240
|
+
* discriminantCase<'success', Machine<FetchContext>, 'status', 'success'>('success', 'status', 'success')
|
|
241
|
+
* );
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
export declare function discriminantCase<Name extends string, M extends Machine<any> = Machine<any>, K extends keyof Context<M> = any, V extends Context<M>[K] = any>(name: Name, key: K, value: V): MatcherCase<Name, M & {
|
|
245
|
+
context: Extract<Context<M>, {
|
|
246
|
+
[P in K]: V;
|
|
247
|
+
}>;
|
|
248
|
+
}, (m: M) => m is M & {
|
|
249
|
+
context: Extract<Context<M>, {
|
|
250
|
+
[P in K]: V;
|
|
251
|
+
}>;
|
|
252
|
+
}>;
|
|
253
|
+
/**
|
|
254
|
+
* Creates a custom matcher case with a user-defined predicate.
|
|
255
|
+
* For advanced matching logic beyond instanceof or discriminants.
|
|
256
|
+
*
|
|
257
|
+
* @template Name - The unique name for this case
|
|
258
|
+
* @template M - The machine type this case matches (inferred from predicate)
|
|
259
|
+
* @param name - The name to use for this case
|
|
260
|
+
* @param predicate - A type guard function that determines if a machine matches
|
|
261
|
+
* @returns A matcher case tuple
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* const match = createMatcher(
|
|
266
|
+
* customCase('complex', (m): m is ComplexMachine => {
|
|
267
|
+
* return m.context.value > 10 && m.context.status === 'active';
|
|
268
|
+
* })
|
|
269
|
+
* );
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
export declare function customCase<const Name extends string, M>(name: Name, predicate: (m: any) => m is M): MatcherCase<Name, M, (m: any) => m is M>;
|
|
273
|
+
/**
|
|
274
|
+
* Creates a discriminated matcher builder for a specific context union type.
|
|
275
|
+
* Provides better type inference by capturing the context type upfront.
|
|
276
|
+
*
|
|
277
|
+
* @template C - The discriminated union context type
|
|
278
|
+
* @returns A builder object with a `case` method for defining cases with less boilerplate
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* type FetchContext =
|
|
283
|
+
* | { status: 'idle' }
|
|
284
|
+
* | { status: 'loading'; startTime: number }
|
|
285
|
+
* | { status: 'success'; data: string };
|
|
286
|
+
*
|
|
287
|
+
* const builder = forContext<FetchContext>();
|
|
288
|
+
*
|
|
289
|
+
* const match = createMatcher(
|
|
290
|
+
* builder.case('idle', 'status', 'idle'),
|
|
291
|
+
* builder.case('loading', 'status', 'loading'),
|
|
292
|
+
* builder.case('success', 'status', 'success')
|
|
293
|
+
* );
|
|
294
|
+
*
|
|
295
|
+
* // Full type inference and narrowing works!
|
|
296
|
+
* if (match.is.success(machine)) {
|
|
297
|
+
* console.log(machine.context.data); // ✓ TypeScript knows data exists
|
|
298
|
+
* }
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
export declare function forContext<C extends object>(): {
|
|
302
|
+
/**
|
|
303
|
+
* Creates a discriminated union case with full type inference.
|
|
304
|
+
*/
|
|
305
|
+
case<Name extends string, K extends keyof C, V extends C[K]>(name: Name, key: K, value: V): MatcherCase<Name, {
|
|
306
|
+
readonly context: C;
|
|
307
|
+
} & {
|
|
308
|
+
context: Extract<C, { [P in K]: V; }>;
|
|
309
|
+
}, (m: {
|
|
310
|
+
readonly context: C;
|
|
311
|
+
}) => m is {
|
|
312
|
+
readonly context: C;
|
|
313
|
+
} & {
|
|
314
|
+
context: Extract<C, { [P in K]: V; }>;
|
|
315
|
+
}>;
|
|
316
|
+
};
|
|
317
|
+
export {};
|
|
318
|
+
//# sourceMappingURL=matcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matcher.d.ts","sourceRoot":"","sources":["../../src/matcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;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AAE;SAAG,CAAC,IAAI,CAAC,GAAG,CAAC;KAAE,CAAC,CAAA;CAAE,CACrE,CAMA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CACxB,KAAK,CAAC,IAAI,SAAS,MAAM,EACzB,CAAC,EAED,IAAI,EAAE,IAAI,EACV,SAAS,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,GAC5B,WAAW,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAE1C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM;IAIvC;;OAEG;SAED,IAAI,SAAS,MAAM,EACnB,CAAC,SAAS,MAAM,CAAC,EACjB,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,QAER,IAAI,OACL,CAAC,SACC,CAAC,GACP,WAAW,CACZ,IAAI,EACJ;0BAhB0C,CAAC;QAgBtB;QAAE,OAAO,EAAE,OAAO,CAAC,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAE,CAAC,CAAA;KAAE,EAC7D,CAAC,CAAC;0BAjBwC,CAAC;KAiBrB,KAAK,CAAC,IAAI;0BAjBU,CAAC;QAiBU;QAAE,OAAO,EAAE,OAAO,CAAC,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAE,CAAC,CAAA;KAAE,CAC9F;EAQJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doeixd/machine",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"src"
|
|
@@ -115,12 +115,24 @@
|
|
|
115
115
|
},
|
|
116
116
|
"require": "./dist/cjs/production/core.js",
|
|
117
117
|
"import": "./dist/esm/production/core.js"
|
|
118
|
+
},
|
|
119
|
+
"./extract": {
|
|
120
|
+
"types": "./dist/types/extract.d.ts",
|
|
121
|
+
"development": {
|
|
122
|
+
"require": "./dist/cjs/development/extract.js",
|
|
123
|
+
"import": "./dist/esm/development/extract.js"
|
|
124
|
+
},
|
|
125
|
+
"require": "./dist/cjs/production/extract.js",
|
|
126
|
+
"import": "./dist/esm/production/extract.js"
|
|
118
127
|
}
|
|
119
128
|
},
|
|
120
129
|
"typesVersions": {
|
|
121
130
|
"*": {
|
|
122
131
|
"core": [
|
|
123
132
|
"./dist/types/core.d.ts"
|
|
133
|
+
],
|
|
134
|
+
"extract": [
|
|
135
|
+
"./dist/types/extract.d.ts"
|
|
124
136
|
]
|
|
125
137
|
}
|
|
126
138
|
},
|
package/src/index.ts
CHANGED
|
@@ -345,7 +345,7 @@ export type BindTransitions<T> = {
|
|
|
345
345
|
* @param factory - A function that receives a `transition` helper and returns the transitions object.
|
|
346
346
|
* @returns A new machine instance.
|
|
347
347
|
*/
|
|
348
|
-
export function createMachine<C extends object, T extends Record<string, (this: C, ...args: any[]) => any>>(
|
|
348
|
+
export function createMachine<C extends object, T extends Record<string, (this: C, ...args: any[]) => any> = Record<string, (this: C, ...args: any[]) => any>>(
|
|
349
349
|
context: C,
|
|
350
350
|
factory: (transition: (newContext: C) => Machine<C, any>) => T
|
|
351
351
|
): Machine<C, BindTransitions<T>>;
|
|
@@ -584,6 +584,41 @@ export function setContext<M extends Machine<any>>(
|
|
|
584
584
|
return createMachine(newContext, transitions as any) as M;
|
|
585
585
|
}
|
|
586
586
|
|
|
587
|
+
/**
|
|
588
|
+
* Creates a minimal machine-like object with just a context property.
|
|
589
|
+
* Useful for creating test fixtures and working with pattern matching utilities.
|
|
590
|
+
*
|
|
591
|
+
* @template C - The context type
|
|
592
|
+
* @param context - The context object
|
|
593
|
+
* @returns An object with a readonly context property
|
|
594
|
+
*
|
|
595
|
+
* @example
|
|
596
|
+
* ```typescript
|
|
597
|
+
* // For testing with discriminated unions
|
|
598
|
+
* type FetchContext =
|
|
599
|
+
* | { status: 'idle' }
|
|
600
|
+
* | { status: 'success'; data: string };
|
|
601
|
+
*
|
|
602
|
+
* const idleMachine = createContext<FetchContext>({ status: 'idle' });
|
|
603
|
+
* const successMachine = createContext<FetchContext>({ status: 'success', data: 'result' });
|
|
604
|
+
*
|
|
605
|
+
* // Works with pattern matching
|
|
606
|
+
* const match = createMatcher(
|
|
607
|
+
* discriminantCase('idle', 'status', 'idle'),
|
|
608
|
+
* discriminantCase('success', 'status', 'success')
|
|
609
|
+
* );
|
|
610
|
+
*
|
|
611
|
+
* if (match.is.success(successMachine)) {
|
|
612
|
+
* console.log(successMachine.context.data); // TypeScript knows data exists
|
|
613
|
+
* }
|
|
614
|
+
* ```
|
|
615
|
+
*/
|
|
616
|
+
export function createContext<C extends object>(
|
|
617
|
+
context: C
|
|
618
|
+
): { readonly context: C } {
|
|
619
|
+
return { context };
|
|
620
|
+
}
|
|
621
|
+
|
|
587
622
|
/**
|
|
588
623
|
* Creates a new machine by overriding or adding transition functions to an existing machine.
|
|
589
624
|
* Ideal for mocking in tests or decorating functionality. The original machine is unchanged.
|
|
@@ -764,7 +799,8 @@ export function matchMachine<
|
|
|
764
799
|
|
|
765
800
|
/**
|
|
766
801
|
* Type-safe helper to assert that a machine's context has a specific discriminant value.
|
|
767
|
-
* This narrows the type of the context based on the discriminant
|
|
802
|
+
* This narrows the type of the context based on the discriminant, properly handling
|
|
803
|
+
* discriminated unions.
|
|
768
804
|
*
|
|
769
805
|
* @template M - The machine type.
|
|
770
806
|
* @template K - The discriminant key.
|
|
@@ -775,8 +811,12 @@ export function matchMachine<
|
|
|
775
811
|
* @returns True if the discriminant matches, with type narrowing.
|
|
776
812
|
*
|
|
777
813
|
* @example
|
|
778
|
-
*
|
|
779
|
-
*
|
|
814
|
+
* type Context = { status: 'idle' } | { status: 'loading' } | { status: 'success'; data: string };
|
|
815
|
+
* const machine = createMachine<Context>({ status: 'success', data: 'test' }, {});
|
|
816
|
+
*
|
|
817
|
+
* if (hasState(machine, 'status', 'success')) {
|
|
818
|
+
* // machine.context is narrowed to { status: 'success'; data: string }
|
|
819
|
+
* console.log(machine.context.data); // ✓ TypeScript knows about 'data'
|
|
780
820
|
* }
|
|
781
821
|
*/
|
|
782
822
|
export function hasState<
|
|
@@ -787,7 +827,7 @@ export function hasState<
|
|
|
787
827
|
machine: M,
|
|
788
828
|
key: K,
|
|
789
829
|
value: V
|
|
790
|
-
): machine is M & { context: Context<M
|
|
830
|
+
): machine is M & { context: Extract<Context<M>, { [P in K]: V }> } {
|
|
791
831
|
return machine.context[key] === value;
|
|
792
832
|
}
|
|
793
833
|
|
|
@@ -1043,16 +1083,19 @@ export {
|
|
|
1043
1083
|
|
|
1044
1084
|
export type {
|
|
1045
1085
|
MachineConfig,
|
|
1046
|
-
ExtractionConfig
|
|
1086
|
+
ExtractionConfig,
|
|
1087
|
+
ParallelRegionConfig,
|
|
1088
|
+
ChildStatesConfig
|
|
1047
1089
|
} from './extract';
|
|
1048
1090
|
|
|
1091
|
+
// Note: Extraction functions (extractMachine, extractMachines, generateChart) are NOT exported
|
|
1092
|
+
// to keep them out of the runtime bundle. Use the CLI tool or import directly from the source
|
|
1093
|
+
// file for build-time statechart generation.
|
|
1049
1094
|
|
|
1050
1095
|
export * from './multi'
|
|
1051
1096
|
|
|
1052
1097
|
export * from './higher-order'
|
|
1053
1098
|
|
|
1054
|
-
export * from './extract'
|
|
1055
|
-
|
|
1056
1099
|
// =============================================================================
|
|
1057
1100
|
// SECTION: MIDDLEWARE & INTERCEPTION
|
|
1058
1101
|
// =============================================================================
|
|
@@ -1084,4 +1127,25 @@ export {
|
|
|
1084
1127
|
createTransitionExtender,
|
|
1085
1128
|
createFunctionalMachine,
|
|
1086
1129
|
state
|
|
1087
|
-
} from './functional-combinators';
|
|
1130
|
+
} from './functional-combinators';
|
|
1131
|
+
|
|
1132
|
+
// =============================================================================
|
|
1133
|
+
// SECTION: PATTERN MATCHING
|
|
1134
|
+
// =============================================================================
|
|
1135
|
+
|
|
1136
|
+
export {
|
|
1137
|
+
createMatcher,
|
|
1138
|
+
classCase,
|
|
1139
|
+
discriminantCase,
|
|
1140
|
+
customCase,
|
|
1141
|
+
forContext,
|
|
1142
|
+
type MatcherCase,
|
|
1143
|
+
type CasesToMapping,
|
|
1144
|
+
type MatcherUnion,
|
|
1145
|
+
type CaseNames,
|
|
1146
|
+
type CaseHandler,
|
|
1147
|
+
type ExhaustivenessMarker,
|
|
1148
|
+
type IsExhaustive,
|
|
1149
|
+
type WhenBuilder,
|
|
1150
|
+
type Matcher
|
|
1151
|
+
} from './matcher';
|