@systemfsoftware/xstate 0.0.0-stage → 6.0.0-beta.65

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.
@@ -0,0 +1,472 @@
1
+ import { C as createCustomEffect, E as createSendToEffect, H as systemLogicMetadata, T as createEmitEffect, at as parseDelayToMilliseconds, gt as runStep, j as finalizeTransitionResult, r as createActor, w as createDeadLetterEffect, wt as createInitEvent } from "./createActor-BGHd7i8Q.mjs";
2
+ import { S as XSTATE_STOP } from "./utils-CXBxxp3Z.mjs";
3
+ // src/validation.ts
4
+ function assertValid(validator, request) {
5
+ const error = validator.check(request);
6
+ if (error) throw error;
7
+ }
8
+ // src/actors/logic.ts
9
+ const effectStates = /* @__PURE__ */ new WeakMap();
10
+ function getEffectState(self) {
11
+ let state = effectStates.get(self);
12
+ if (!state) {
13
+ state = /* @__PURE__ */ new Map();
14
+ effectStates.set(self, state);
15
+ }
16
+ return state;
17
+ }
18
+ function executeLogicEffect(self, key, effect) {
19
+ const state = getEffectState(self);
20
+ if (key !== void 0 && state.has(key)) return;
21
+ const cleanup = effect();
22
+ state.set(key ?? Symbol(), { cleanup: typeof cleanup === "function" ? cleanup : void 0 });
23
+ }
24
+ function cleanupLogicEffects(self) {
25
+ const state = effectStates.get(self);
26
+ if (!state) return;
27
+ effectStates.delete(self);
28
+ let failure;
29
+ for (const { cleanup } of state.values()) try {
30
+ cleanup?.();
31
+ } catch (error) {
32
+ failure ??= { error };
33
+ }
34
+ if (failure) throw failure.error;
35
+ }
36
+ function resolveContext(context, input) {
37
+ return typeof context === "function" ? context({ input }) : context;
38
+ }
39
+ function createLogic(config) {
40
+ const calculateTransition = ((snapshot, event, actorScope) => {
41
+ if (snapshot.status !== "active") return [snapshot, []];
42
+ if (event.type === "@xstate.stop") return [{
43
+ ...snapshot,
44
+ status: "stopped",
45
+ input: void 0
46
+ }, [createCustomEffect("xstate.logic.cleanup", () => cleanupLogicEffects(actorScope.self))]];
47
+ if (event.type === "xstate.logic.effect.start") return [{
48
+ ...snapshot,
49
+ effects: {
50
+ ...snapshot.effects,
51
+ [event.key]: { status: "active" }
52
+ }
53
+ }, []];
54
+ if (event.type === "xstate.logic.effect.resolve") return [{
55
+ ...snapshot,
56
+ effects: {
57
+ ...snapshot.effects,
58
+ [event.key]: {
59
+ status: "done",
60
+ output: event.output
61
+ }
62
+ }
63
+ }, []];
64
+ if (event.type === "xstate.logic.effect.reject") return [{
65
+ ...snapshot,
66
+ effects: {
67
+ ...snapshot.effects,
68
+ [event.key]: {
69
+ status: "error",
70
+ error: event.error
71
+ }
72
+ }
73
+ }, []];
74
+ const effects = [];
75
+ const trackedEffects = Object.create(null);
76
+ const enqueueEffect = (key, exec) => {
77
+ const recorded = snapshot.effects && Object.prototype.hasOwnProperty.call(snapshot.effects, key) ? snapshot.effects[key] : void 0;
78
+ if (recorded && !(event.type === "@xstate.init" && recorded.status === "active")) return;
79
+ effects.push(createCustomEffect("xstate.logic.effect", (runtime = actorScope.self.system) => executeLogicEffect(actorScope.self, key, () => exec(runtime)), { key }));
80
+ trackedEffects[key] = { status: "active" };
81
+ };
82
+ const patch = config.run({
83
+ context: snapshot.context,
84
+ event,
85
+ input: snapshot.input,
86
+ system: actorScope.system,
87
+ self: actorScope.self,
88
+ emit: actorScope.emit
89
+ }, {
90
+ emit: (emitted) => {
91
+ effects.push(createEmitEffect(actorScope, emitted));
92
+ },
93
+ sendBack: (sentEvent) => {
94
+ const parent = actorScope.self._parent;
95
+ if (parent) effects.push(createSendToEffect(actorScope, parent, sentEvent));
96
+ },
97
+ raise: (raisedEvent) => {
98
+ effects.push(createSendToEffect(actorScope, actorScope.self, raisedEvent));
99
+ },
100
+ effect: ((keyOrExec, maybeExec) => {
101
+ if (typeof keyOrExec === "string") {
102
+ enqueueEffect(keyOrExec, maybeExec);
103
+ return;
104
+ }
105
+ const exec = keyOrExec;
106
+ effects.push(createCustomEffect("xstate.logic.effect", (runtime = actorScope.self.system) => executeLogicEffect(actorScope.self, void 0, () => exec(runtime))));
107
+ })
108
+ });
109
+ const nextSnapshot = {
110
+ ...snapshot,
111
+ ...patch || {}
112
+ };
113
+ if ("context" in snapshot || patch?.context !== void 0) nextSnapshot.context = patch?.context ?? snapshot.context;
114
+ if (patch?.effects || Object.keys(trackedEffects).length) nextSnapshot.effects = {
115
+ ...snapshot.effects,
116
+ ...patch?.effects,
117
+ ...trackedEffects
118
+ };
119
+ return finalizeTransitionResult(actorScope, snapshot, [nextSnapshot, effects]);
120
+ });
121
+ const transition = ((snapshot, event, actorScope) => {
122
+ if (config.validator) {
123
+ const sourceRef = actorScope.self._lastSourceRef;
124
+ const eventOrigin = sourceRef ? "actor" : "external";
125
+ const error = config.validator.check({
126
+ kind: "event",
127
+ logic,
128
+ event,
129
+ eventOrigin
130
+ });
131
+ if (error) return [snapshot, [createDeadLetterEffect(actorScope, sourceRef, event, "invalidEvent", {
132
+ issues: error.issues,
133
+ error
134
+ })]];
135
+ }
136
+ const result = calculateTransition(snapshot, event, actorScope);
137
+ if (config.validator) assertValid(config.validator, {
138
+ kind: "result",
139
+ logic,
140
+ snapshot: result[0],
141
+ effects: result[1]
142
+ });
143
+ return result;
144
+ });
145
+ const logic = {
146
+ id: config.id,
147
+ config,
148
+ validator: config.validator,
149
+ transition,
150
+ start: (snapshot, actorScope, options) => {
151
+ if (!options?.restored) return;
152
+ const [nextSnapshot, effects] = transition(snapshot, createInitEvent(snapshot.input), actorScope);
153
+ Object.assign(snapshot, nextSnapshot);
154
+ for (const effect of effects) actorScope.actionExecutor(effect);
155
+ },
156
+ initialTransition: (input, actorScope) => {
157
+ if (config.validator) assertValid(config.validator, {
158
+ kind: "input",
159
+ logic,
160
+ input
161
+ });
162
+ const context = resolveContext(config.context, input);
163
+ const snapshot = {
164
+ status: "active",
165
+ output: void 0,
166
+ error: void 0,
167
+ input
168
+ };
169
+ if (context !== void 0) snapshot.context = context;
170
+ return transition({ ...snapshot }, createInitEvent(input), actorScope);
171
+ },
172
+ getInitialSnapshot: (actorScope, input) => logic.initialTransition(input, actorScope)[0],
173
+ getPersistedSnapshot: (snapshot) => snapshot,
174
+ restoreSnapshot: (snapshot) => snapshot
175
+ };
176
+ return logic;
177
+ }
178
+ // src/actors/callback.ts
179
+ const instanceStates = /* #__PURE__ */ new WeakMap();
180
+ function createCallbackLogic(callbackOrConfig) {
181
+ const callback = typeof callbackOrConfig === "function" ? callbackOrConfig : callbackOrConfig.run;
182
+ const schemas = typeof callbackOrConfig === "function" ? void 0 : callbackOrConfig.schemas;
183
+ return createLogic({
184
+ validator: typeof callbackOrConfig === "function" ? void 0 : callbackOrConfig.validator,
185
+ schemas,
186
+ context: void 0,
187
+ run: (args, enq) => {
188
+ const { event, input, self, system } = args;
189
+ const emit = args.emit;
190
+ instanceStates.get(self)?.receivers?.forEach((receiver) => receiver(event));
191
+ enq.effect("callback", () => {
192
+ const callbackState = {
193
+ receivers: void 0,
194
+ dispose: void 0
195
+ };
196
+ instanceStates.set(self, callbackState);
197
+ callbackState.dispose = callback({
198
+ input,
199
+ system,
200
+ self,
201
+ sendBack: (event) => {
202
+ if (self.getSnapshot().status === "stopped") return;
203
+ const parent = self._parent;
204
+ if (parent) system._relay(self, parent, event);
205
+ },
206
+ receive: (listener) => {
207
+ callbackState.receivers ??= /* @__PURE__ */ new Set();
208
+ callbackState.receivers.add(listener);
209
+ },
210
+ emit
211
+ });
212
+ return () => {
213
+ instanceStates.delete(self);
214
+ callbackState.receivers?.clear();
215
+ callbackState.dispose?.();
216
+ };
217
+ });
218
+ }
219
+ });
220
+ }
221
+ // src/actors/observable.ts
222
+ const XSTATE_OBSERVABLE_NEXT = "xstate.observable.next";
223
+ const XSTATE_OBSERVABLE_ERROR = "xstate.observable.error";
224
+ const XSTATE_OBSERVABLE_COMPLETE = "xstate.observable.complete";
225
+ function createObservableLogic(observableCreatorOrConfig) {
226
+ const observableCreator = typeof observableCreatorOrConfig === "function" ? observableCreatorOrConfig : observableCreatorOrConfig.run;
227
+ const schemas = typeof observableCreatorOrConfig === "function" ? void 0 : observableCreatorOrConfig.schemas;
228
+ return createLogic({
229
+ validator: typeof observableCreatorOrConfig === "function" ? void 0 : observableCreatorOrConfig.validator,
230
+ schemas,
231
+ context: void 0,
232
+ run: (args, enq) => {
233
+ const { event, input, self, system } = args;
234
+ switch (event.type) {
235
+ case XSTATE_OBSERVABLE_NEXT: return { context: event["data"] };
236
+ case XSTATE_OBSERVABLE_ERROR: return {
237
+ status: "error",
238
+ error: event.data,
239
+ input: void 0,
240
+ effects: { observable: {
241
+ status: "error",
242
+ error: event.data
243
+ } }
244
+ };
245
+ case XSTATE_OBSERVABLE_COMPLETE: return {
246
+ status: "done",
247
+ input: void 0,
248
+ effects: { observable: { status: "done" } }
249
+ };
250
+ }
251
+ if (event.type !== "@xstate.init") return;
252
+ enq.effect((runtime = system) => {
253
+ const actorSelf = self;
254
+ const sendSelf = (event) => void runtime.sendEvent(actorSelf, actorSelf, event);
255
+ const subscription = observableCreator({
256
+ input,
257
+ system,
258
+ self,
259
+ emit: ((event) => void runtime.emitEvent(actorSelf, event))
260
+ }).subscribe({
261
+ next: (value) => {
262
+ sendSelf({
263
+ type: XSTATE_OBSERVABLE_NEXT,
264
+ data: value
265
+ });
266
+ },
267
+ error: (err) => {
268
+ sendSelf({
269
+ type: XSTATE_OBSERVABLE_ERROR,
270
+ data: err
271
+ });
272
+ },
273
+ complete: () => {
274
+ sendSelf({ type: XSTATE_OBSERVABLE_COMPLETE });
275
+ }
276
+ });
277
+ return () => subscription.unsubscribe();
278
+ });
279
+ }
280
+ });
281
+ }
282
+ function createEventObservableLogic(lazyObservableOrConfig) {
283
+ const lazyObservable = typeof lazyObservableOrConfig === "function" ? lazyObservableOrConfig : lazyObservableOrConfig.run;
284
+ const validator = typeof lazyObservableOrConfig === "function" ? void 0 : lazyObservableOrConfig.validator;
285
+ const logic = {
286
+ config: lazyObservableOrConfig,
287
+ validator,
288
+ transition: (state, event, actorScope) => {
289
+ if (state.status !== "active") return [state, []];
290
+ switch (event.type) {
291
+ case XSTATE_OBSERVABLE_ERROR: return finalizeTransitionResult(actorScope, state, [{
292
+ ...state,
293
+ status: "error",
294
+ error: event.data,
295
+ input: void 0,
296
+ _subscription: void 0
297
+ }, []]);
298
+ case XSTATE_OBSERVABLE_COMPLETE: return finalizeTransitionResult(actorScope, state, [{
299
+ ...state,
300
+ status: "done",
301
+ input: void 0,
302
+ _subscription: void 0
303
+ }, []]);
304
+ case XSTATE_STOP:
305
+ state._subscription.unsubscribe();
306
+ return [{
307
+ ...state,
308
+ status: "stopped",
309
+ input: void 0,
310
+ _subscription: void 0
311
+ }, []];
312
+ default: return [state, []];
313
+ }
314
+ },
315
+ initialTransition: (input, _) => {
316
+ if (validator) assertValid(validator, {
317
+ kind: "input",
318
+ logic,
319
+ input
320
+ });
321
+ return [{
322
+ status: "active",
323
+ output: void 0,
324
+ error: void 0,
325
+ context: void 0,
326
+ input,
327
+ _subscription: void 0
328
+ }, []];
329
+ },
330
+ getInitialSnapshot: (actorScope, input) => logic.initialTransition(input, actorScope)[0],
331
+ start: (state, { self, system, emit }) => {
332
+ if (state.status === "done") return;
333
+ state._subscription = lazyObservable({
334
+ input: state.input,
335
+ system,
336
+ self,
337
+ emit: (event) => {
338
+ emit(event);
339
+ }
340
+ }).subscribe({
341
+ next: (value) => {
342
+ if (self._parent) system._relay(self, self._parent, value);
343
+ },
344
+ error: (err) => {
345
+ system._relay(self, self, {
346
+ type: XSTATE_OBSERVABLE_ERROR,
347
+ data: err
348
+ });
349
+ },
350
+ complete: () => {
351
+ system._relay(self, self, { type: XSTATE_OBSERVABLE_COMPLETE });
352
+ }
353
+ });
354
+ },
355
+ getPersistedSnapshot: ({ _subscription, ...snapshot }) => snapshot,
356
+ restoreSnapshot: (snapshot) => ({
357
+ ...snapshot,
358
+ _subscription: void 0
359
+ })
360
+ };
361
+ return logic;
362
+ }
363
+ // src/actors/promise.ts
364
+ const XSTATE_ASYNC_RESOLVE = "xstate.async.resolve";
365
+ const XSTATE_ASYNC_REJECT = "xstate.async.reject";
366
+ /** @public */
367
+ var TimeoutError = class extends Error {
368
+ constructor(timeout) {
369
+ super(`Async logic timed out after ${timeout}.`);
370
+ this.name = "TimeoutError";
371
+ }
372
+ };
373
+ function createAsyncLogic(asyncLogic) {
374
+ const config = asyncLogic;
375
+ const logic = createLogic({
376
+ id: config.id,
377
+ validator: config.validator,
378
+ schemas: config.schemas,
379
+ context: void 0,
380
+ run: ({ event, input, self, system }, enq) => {
381
+ switch (event.type) {
382
+ case XSTATE_ASYNC_RESOLVE: {
383
+ const resolvedValue = event.data ?? event.output;
384
+ return {
385
+ status: "done",
386
+ output: resolvedValue,
387
+ input: void 0,
388
+ effects: { async: {
389
+ status: "done",
390
+ output: resolvedValue
391
+ } }
392
+ };
393
+ }
394
+ case XSTATE_ASYNC_REJECT: {
395
+ const error = event.data ?? event.error;
396
+ return {
397
+ status: "error",
398
+ error,
399
+ input: void 0,
400
+ effects: { async: {
401
+ status: "error",
402
+ error
403
+ } }
404
+ };
405
+ }
406
+ }
407
+ if (event.type !== "@xstate.init") return;
408
+ enq.effect((runtime = system) => {
409
+ const actorSelf = self;
410
+ const sendSelf = (event) => void runtime.sendEvent(actorSelf, actorSelf, event);
411
+ const controller = new AbortController();
412
+ const timeout = config.timeout;
413
+ const timeoutMs = parseDelayToMilliseconds(timeout);
414
+ const timeoutId = timeoutMs === void 0 ? void 0 : system._clock.setTimeout(() => {
415
+ if (self.getSnapshot().status !== "active") return;
416
+ controller.abort();
417
+ sendSelf({
418
+ type: XSTATE_ASYNC_REJECT,
419
+ data: new TimeoutError(timeout)
420
+ });
421
+ }, timeoutMs);
422
+ const clearTimeout = () => {
423
+ if (timeoutId !== void 0) system._clock.clearTimeout(timeoutId);
424
+ };
425
+ const runBody = () => Promise.resolve(config.run({
426
+ input,
427
+ system,
428
+ self,
429
+ signal: controller.signal
430
+ }, {
431
+ emit: (event) => void runtime.emitEvent(actorSelf, event),
432
+ step: (key, exec) => runtime.runStep ? Promise.resolve(runtime.runStep(actorSelf, key, exec)) : runStep(actorSelf, key, exec, sendSelf)
433
+ }));
434
+ (runtime.runLogic ? Promise.resolve(runtime.runLogic(actorSelf, runBody)) : runBody()).then((response) => {
435
+ clearTimeout();
436
+ if (self.getSnapshot().status !== "active") return;
437
+ sendSelf({
438
+ type: XSTATE_ASYNC_RESOLVE,
439
+ data: response
440
+ });
441
+ }, (errorData) => {
442
+ clearTimeout();
443
+ if (self.getSnapshot().status !== "active") return;
444
+ sendSelf({
445
+ type: XSTATE_ASYNC_REJECT,
446
+ data: errorData
447
+ });
448
+ });
449
+ return () => {
450
+ controller.abort();
451
+ clearTimeout();
452
+ };
453
+ });
454
+ return { effects: { async: { status: "active" } } };
455
+ }
456
+ });
457
+ Object.defineProperty(logic, systemLogicMetadata, { value: {
458
+ kind: "async",
459
+ timeout: config.timeout
460
+ } });
461
+ return logic;
462
+ }
463
+ // src/actors/index.ts
464
+ const emptyLogic = /* #__PURE__ */ createLogic({
465
+ context: void 0,
466
+ run: () => void 0
467
+ });
468
+ /** @public */
469
+ function createEmptyActor() {
470
+ return createActor(emptyLogic);
471
+ }
472
+ export { createObservableLogic as a, assertValid as c, createEventObservableLogic as i, TimeoutError as n, createCallbackLogic as o, createAsyncLogic as r, createLogic as s, createEmptyActor as t };
@@ -0,0 +1,3 @@
1
+ import { Cr as LogicArgs, Dr as TimeoutError, Er as LogicFunction, Or as createAsyncLogic, Sr as AsyncSnapshot, Tr as LogicEnqueue, br as AsyncActorLogic, wr as LogicConfig, xr as AsyncActorRef } from "./types-B2EAwOk-.js";
2
+ import { A as LogicActorLogic, B as createLogic, C as listenerLogic, D as CallbackLogicFunction, E as CallbackLogicConfig, F as LogicEffectState, I as LogicEnqueue$1, L as LogicFunction$1, M as LogicArgs$1, N as LogicConfig$1, O as CallbackSnapshot, P as LogicEffect, R as LogicPatch, S as createListenerLogic, T as CallbackActorRef, _ as createObservableLogic, a as SubscriptionMappers, b as ListenerInput, c as subscriptionLogic, d as ObservableActorLogic, f as ObservableActorRef, g as createEventObservableLogic, h as ObservableSnapshot, i as SubscriptionInput, j as LogicActorRef, k as createCallbackLogic, l as EventObservableLogicConfig, m as ObservableLogicFunction, n as SubscriptionActorLogic, o as SubscriptionSnapshot, p as ObservableLogicConfig, r as SubscriptionActorRef, s as createSubscriptionLogic, t as createEmptyActor, u as EventObservableLogicFunction, v as ListenerActorLogic, w as CallbackActorLogic, x as ListenerSnapshot, y as ListenerActorRef, z as LogicSnapshot } from "./index-DDWoKIF5.js";
3
+ export { type AsyncActorLogic, type AsyncActorRef, type LogicArgs as AsyncLogicArgs, type LogicConfig as AsyncLogicConfig, type LogicEnqueue as AsyncLogicEnqueue, type LogicFunction as AsyncLogicFunction, type AsyncSnapshot, type CallbackActorLogic, type CallbackActorRef, type CallbackLogicConfig, type CallbackLogicFunction, type CallbackSnapshot, type EventObservableLogicConfig, type EventObservableLogicFunction, type ListenerActorLogic, type ListenerActorRef, type ListenerInput, type ListenerSnapshot, type LogicActorLogic, type LogicActorRef, type LogicArgs$1 as LogicArgs, type LogicConfig$1 as LogicConfig, type LogicEffect, type LogicEffectState, type LogicEnqueue$1 as LogicEnqueue, type LogicFunction$1 as LogicFunction, type LogicPatch, type LogicSnapshot, type ObservableActorLogic, type ObservableActorRef, type ObservableLogicConfig, type ObservableLogicFunction, type ObservableSnapshot, type SubscriptionActorLogic, type SubscriptionActorRef, type SubscriptionInput, type SubscriptionMappers, type SubscriptionSnapshot, TimeoutError, createAsyncLogic, createCallbackLogic, createEmptyActor, createEventObservableLogic, createListenerLogic, createLogic, createObservableLogic, createSubscriptionLogic, listenerLogic, subscriptionLogic };
@@ -0,0 +1,3 @@
1
+ import { B as createListenerLogic, R as createSubscriptionLogic, V as listenerLogic, z as subscriptionLogic } from "./createActor-BGHd7i8Q.mjs";
2
+ import { a as createObservableLogic, i as createEventObservableLogic, n as TimeoutError, o as createCallbackLogic, r as createAsyncLogic, s as createLogic, t as createEmptyActor } from "./actors-LxMBvgxO.mjs";
3
+ export { TimeoutError, createAsyncLogic, createCallbackLogic, createEmptyActor, createEventObservableLogic, createListenerLogic, createLogic, createObservableLogic, createSubscriptionLogic, listenerLogic, subscriptionLogic };
@@ -0,0 +1,210 @@
1
+ /**
2
+ * The Standard Schema interface.
3
+ *
4
+ * @public
5
+ */
6
+ interface StandardSchemaV1<Input = unknown, Output = Input> {
7
+ /** The Standard Schema properties. */
8
+ readonly '~standard': StandardSchemaV1.Props<Input, Output>;
9
+ }
10
+ /**
11
+ * Schemas that can be declared for an individual state node.
12
+ *
13
+ * @public
14
+ */
15
+ type SetupStateSchemas = {
16
+ /** Refines the machine's root context schema while this state is active. */
17
+ context?: StandardSchemaV1 | undefined;
18
+ input?: StandardSchemaV1 | undefined;
19
+ /** The output emitted when this state node completes. */
20
+ output?: StandardSchemaV1 | undefined;
21
+ };
22
+ /**
23
+ * A type-only Standard Schema produced by {@link types}.
24
+ *
25
+ * @public
26
+ */
27
+ interface TypeSchema<T> extends StandardSchemaV1<T, T> {
28
+ readonly '~standard': StandardSchemaV1.Props<T, T> & {
29
+ readonly vendor: 'xstate.types';
30
+ };
31
+ }
32
+ /**
33
+ * Declares schema types for inference only — no runtime validation.
34
+ *
35
+ * Use in a machine's `schemas` when you want TypeScript types without pulling
36
+ * in a schema library like Zod. The returned value is a valid Standard Schema
37
+ * whose validation is the identity function, so it carries types but never
38
+ * rejects input.
39
+ *
40
+ * @example
41
+ *
42
+ * ```ts
43
+ * import { createMachine, types } from '@systemfsoftware/xstate';
44
+ *
45
+ * const machine = createMachine({
46
+ * schemas: {
47
+ * context: types<{ count: number }>(),
48
+ * events: {
49
+ * inc: types<{ by: number }>(),
50
+ * reset: types<void>()
51
+ * }
52
+ * },
53
+ * context: { count: 0 }
54
+ * // ...
55
+ * });
56
+ * ```
57
+ *
58
+ * Use `types<void>()` for events without a payload.
59
+ * @public
60
+ */
61
+ declare function types<T>(): TypeSchema<T>;
62
+ /**
63
+ * Returns true if the value is a type-only schema created by {@link types}.
64
+ *
65
+ * @public
66
+ */
67
+ declare function isTypeSchema(value: unknown): value is TypeSchema<unknown>;
68
+ declare namespace StandardSchemaV1 {
69
+ /** The Standard Schema properties interface. */
70
+ interface Props<Input = unknown, Output = Input> {
71
+ /** The version number of the standard. */
72
+ readonly version: 1;
73
+ /** The vendor name of the schema library. */
74
+ readonly vendor: string;
75
+ /** Validates unknown input values. */
76
+ readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
77
+ /** Inferred types associated with the schema. */
78
+ readonly types?: Types<Input, Output> | undefined;
79
+ }
80
+ /** The result interface of the validate function. */
81
+ type Result<Output> = SuccessResult<Output> | FailureResult;
82
+ /** The result interface if validation succeeds. */
83
+ interface SuccessResult<Output> {
84
+ /** The typed output value. */
85
+ readonly value: Output;
86
+ /** The non-existent issues. */
87
+ readonly issues?: undefined;
88
+ }
89
+ /** The result interface if validation fails. */
90
+ interface FailureResult {
91
+ /** The issues of failed validation. */
92
+ readonly issues: ReadonlyArray<Issue>;
93
+ }
94
+ /** The issue interface of the failure output. */
95
+ interface Issue {
96
+ /** The error message of the issue. */
97
+ readonly message: string;
98
+ /** The path of the issue, if any. */
99
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
100
+ }
101
+ /** The path segment interface of the issue. */
102
+ interface PathSegment {
103
+ /** The key representing a path segment. */
104
+ readonly key: PropertyKey;
105
+ }
106
+ /** The Standard Schema types interface. */
107
+ interface Types<Input = unknown, Output = Input> {
108
+ /** The input type of the schema. */
109
+ readonly input: Input;
110
+ /** The output type of the schema. */
111
+ readonly output: Output;
112
+ }
113
+ /** Infers the input type of a Standard Schema. */
114
+ type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['input'];
115
+ /** Infers the output type of a Standard Schema. */
116
+ type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output'];
117
+ }
118
+ declare global {
119
+ interface SymbolConstructor {
120
+ readonly observable: symbol;
121
+ }
122
+ }
123
+ /**
124
+ * The full definition of an event, with a string `type`.
125
+ *
126
+ * @public
127
+ */
128
+ type EventObject = {
129
+ /** The type of event that is sent. */
130
+ type: string;
131
+ };
132
+ /** @public */
133
+ type MachineContext = Record<string, any>;
134
+ /** @public */
135
+ type Values<T> = T[keyof T];
136
+ type ActionSchemas = Record<string, {
137
+ params: StandardSchemaV1;
138
+ }>;
139
+ type GuardSchemas = Record<string, {
140
+ params: StandardSchemaV1;
141
+ }>;
142
+ /**
143
+ * State node types that can be declared in a setup state contract.
144
+ *
145
+ * @public
146
+ */
147
+ type SetupStateType = 'atomic' | 'compound' | 'parallel' | 'final' | 'history' | 'choice';
148
+ /** @public */
149
+ type SetupSchemas = {
150
+ context?: StandardSchemaV1 | undefined;
151
+ events?: Record<string, StandardSchemaV1> | undefined;
152
+ internalEvents?: Record<string, StandardSchemaV1> | undefined;
153
+ actions?: ActionSchemas | undefined;
154
+ guards?: GuardSchemas | undefined;
155
+ emitted?: Record<string, StandardSchemaV1> | undefined;
156
+ input?: StandardSchemaV1 | undefined;
157
+ output?: StandardSchemaV1 | undefined;
158
+ meta?: StandardSchemaV1 | undefined;
159
+ transitionMeta?: StandardSchemaV1 | undefined;
160
+ tags?: StandardSchemaV1 | undefined;
161
+ children?: Record<string, StandardSchemaV1> | undefined;
162
+ };
163
+ /**
164
+ * State schema with optional input/output schemas, structural metadata, and
165
+ * nested states.
166
+ *
167
+ * Structural fields are contracts/defaults for `createMachine(...)`; machine
168
+ * behavior remains authored in the machine config.
169
+ *
170
+ * @public
171
+ */
172
+ interface SetupStateSchema {
173
+ type?: SetupStateType | undefined;
174
+ id?: string | undefined;
175
+ initial?: string | undefined;
176
+ history?: 'shallow' | 'deep' | true | undefined;
177
+ target?: string | readonly [string, ...string[]] | undefined;
178
+ route?: true | undefined;
179
+ schemas?: SetupStateSchemas | undefined;
180
+ states?: Record<string, SetupStateSchema> | undefined;
181
+ }
182
+ /**
183
+ * Event payloads from schemas (e.g. Zod) are often inferred as optional in
184
+ * output types. Wrapping in `Required<>` ensures properties defined in the schema
185
+ * are required on the event. Type-only schemas created with the `types()`
186
+ * helper are exempt: their declared type is authoritative, so optional
187
+ * properties stay optional.
188
+ *
189
+ * @public
190
+ */
191
+ type InferEvents<TEventSchemaMap extends Record<string, StandardSchemaV1>> = Values<{ [K in keyof TEventSchemaMap & string]: StandardSchemaV1.InferOutput<TEventSchemaMap[K]> extends (infer O) ? [O] extends [never] ? never : unknown extends O ? O & {
192
+ type: K;
193
+ } : [O] extends [void] ? {
194
+ type: K;
195
+ } : string extends keyof O ? [O[string]] extends [never] ? {
196
+ type: EventTypeFromSchemaKey<K>;
197
+ } : NormalizeEventPayload<TEventSchemaMap[K], O> & {
198
+ type: EventTypeFromSchemaKey<K>;
199
+ } : NormalizeEventPayload<TEventSchemaMap[K], O> & {
200
+ type: EventTypeFromSchemaKey<K>;
201
+ } : never; }>;
202
+ /** Infers internal events only from explicitly declared schema keys. */
203
+ type InferInternalEvents<TEventSchemaMap extends Record<string, StandardSchemaV1>> = string extends keyof TEventSchemaMap ? never : InferEvents<TEventSchemaMap>;
204
+ type EventTypeFromSchemaKey<TKey extends string> = TKey extends '*' ? string : TKey extends `${infer TLeading}.*` ? `${TLeading}.${string}` : TKey;
205
+ /**
206
+ * Keeps a type-only schema's payload verbatim; applies `Required<>` to payloads
207
+ * from validator libraries (see {@link InferEvents}).
208
+ */
209
+ type NormalizeEventPayload<TSchema extends StandardSchemaV1, O> = TSchema extends TypeSchema<any> ? O : Required<O>;
210
+ export { InferInternalEvents as a, SetupStateSchema as c, SetupStateSchemas as d, StandardSchemaV1 as f, types as h, InferEvents as i, SetupStateType as l, isTypeSchema as m, EventObject as n, MachineContext as o, TypeSchema as p, GuardSchemas as r, SetupSchemas as s, ActionSchemas as t, Values as u };