@typeonce/effect-machine 0.6.0 → 0.6.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.
Files changed (38) hide show
  1. package/package.json +5 -5
  2. package/src/Machine.ts +6873 -0
  3. package/src/index.ts +1 -0
  4. package/src/internal/machine/activities.ts +108 -0
  5. package/src/internal/machine/atom.ts +636 -0
  6. package/src/internal/machine/cluster.ts +394 -0
  7. package/src/internal/machine/command.ts +58 -0
  8. package/src/internal/machine/commandRuntime.ts +43 -0
  9. package/src/internal/machine/configuration.ts +1331 -0
  10. package/src/internal/machine/errors.ts +87 -0
  11. package/src/internal/machine/executionPlan.ts +996 -0
  12. package/src/internal/machine/invocation.ts +119 -0
  13. package/src/internal/machine/machine.ts +1747 -0
  14. package/src/internal/machine/planner.ts +1933 -0
  15. package/src/internal/machine/process.ts +906 -0
  16. package/src/internal/machine/protocol.ts +322 -0
  17. package/src/internal/machine/readiness.ts +10 -0
  18. package/src/internal/machine/runtime.ts +2512 -0
  19. package/src/internal/machine/serialization.ts +498 -0
  20. package/src/internal/machine/stateDefinition.ts +270 -0
  21. package/src/internal/machine/symbols.ts +2 -0
  22. package/src/internal/machine/topology.ts +479 -0
  23. package/src/internal/testing/machine/arbitrary.ts +102 -0
  24. package/src/internal/testing/machine/exploration.ts +331 -0
  25. package/src/internal/testing/machine/finiteModel.ts +1498 -0
  26. package/src/internal/testing/machine/invariant.ts +372 -0
  27. package/src/internal/testing/machine/probe.ts +79 -0
  28. package/src/internal/testing/machine/referenceModel.ts +1505 -0
  29. package/src/internal/testing/machine/runtime.ts +1710 -0
  30. package/src/internal/testing/machine/runtimeInvariant.ts +486 -0
  31. package/src/internal/testing/machine/trace.ts +150 -0
  32. package/src/internal/testing/machine/verification.ts +1890 -0
  33. package/src/testing/MachineTest.ts +2067 -0
  34. package/src/testing/index.ts +7 -0
  35. package/src/unstable/cluster/ClusterMachine.ts +390 -0
  36. package/src/unstable/cluster/index.ts +1 -0
  37. package/src/unstable/reactivity/AtomMachine.ts +649 -0
  38. package/src/unstable/reactivity/index.ts +1 -0
@@ -0,0 +1,1747 @@
1
+ import * as Duration from "effect/Duration"
2
+ import * as Effect from "effect/Effect"
3
+ import * as Inspectable from "effect/Inspectable"
4
+ import * as Option from "effect/Option"
5
+ import { Prototype as PipeablePrototype } from "effect/Pipeable"
6
+ import { hasProperty } from "effect/Predicate"
7
+ import type * as Schema from "effect/Schema"
8
+ import type * as Scope from "effect/Scope"
9
+ import type * as Stream from "effect/Stream"
10
+ import type {
11
+ ActionError,
12
+ ChildAddress,
13
+ ChildMachine,
14
+ Command,
15
+ ExecutionServices,
16
+ InitialEvent as InitialEventModel,
17
+ Logic,
18
+ Machine,
19
+ MachineRef,
20
+ MachineSchemaDecodeError,
21
+ MachineSchemaEncodeError,
22
+ Runtime,
23
+ RuntimeOutcome,
24
+ SpawnOptions,
25
+ StoppedError
26
+ } from "../../Machine.js"
27
+ import * as Activities from "./activities.js"
28
+ import * as Configuration from "./configuration.js"
29
+ import type { ChildAlreadyExistsError, InfiniteTransitionError, StartupError } from "./errors.js"
30
+ import * as internalPlanner from "./planner.js"
31
+ import * as internalProcess from "./process.js"
32
+ import * as Protocol from "./protocol.js"
33
+ import type { EnsureExecutable } from "./readiness.js"
34
+ import * as internalRuntime from "./runtime.js"
35
+ import * as Serialization from "./serialization.js"
36
+ import * as StateDefinition from "./stateDefinition.js"
37
+ import * as Topology from "./topology.js"
38
+
39
+ export {
40
+ ChildAlreadyExistsError,
41
+ InfiniteTransitionError,
42
+ MachineSchemaDecodeError,
43
+ MachineSchemaEncodeError,
44
+ ProcessLocalError,
45
+ StartupError,
46
+ StoppedError
47
+ } from "./errors.js"
48
+ export { InitialEventTypeId } from "./symbols.js"
49
+
50
+ const TypeId = "~effect/Machine"
51
+ export const SnapshotBuilderStateTypeId: unique symbol = Symbol("effect/Machine/SnapshotBuilderState")
52
+ export const InvokeTypeId: unique symbol = Symbol.for("effect/Machine/Invoke")
53
+ const ChildMachineTypeId = "~effect/Machine/ChildMachine"
54
+ type InvokeLifecycleId = string
55
+ type IsAny<A> = 0 extends 1 & A ? true : false
56
+ type MachineRuntimeRequirement = internalRuntime.MachineRuntime
57
+ type ExcludeCompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<
58
+ infer RequiredEvents,
59
+ infer RequiredEmits
60
+ > ? IsAny<Requirements> extends true ? Requirements
61
+ : [RequiredEvents] extends [Events] ? [RequiredEmits] extends [Emits] ? never : Requirements
62
+ : Requirements
63
+ : Requirements
64
+ type SpawnRequirements<Requirements> = Exclude<Requirements, Scope.Scope>
65
+ type SpawnIdError<Options extends SpawnOptions> = "id" extends keyof Options ? Options extends {
66
+ readonly id?: infer Id
67
+ } ? [Id] extends [undefined] ? never : ChildAlreadyExistsError
68
+ : ChildAlreadyExistsError
69
+ : never
70
+ type SpawnError<Options extends SpawnOptions> = SpawnIdError<Options>
71
+ type SpawnResult<State, Event, Error, Requirements, Output, SpawnError, InitialError = never> = Effect.Effect<
72
+ MachineRef<State, Event, Error, Output>,
73
+ SpawnError | InitialError,
74
+ MachineRuntimeRequirement | SpawnRequirements<Requirements>
75
+ >
76
+ type DefineStateTreeInput<States extends Machine.StateSchemas> = States
77
+ type ValidateDefinedStates<States extends Machine.StateSchemas> = [States] extends
78
+ [Machine.ValidateStateSchemas<States>] ? []
79
+ : [validation: Machine.ValidateStateSchemas<States>]
80
+ type InvalidDefinedStateTreeInput<States extends Machine.StateSchemas> = [States] extends
81
+ [Machine.ValidateStateSchemas<States>] ? never
82
+ : States & Machine.ValidateStateSchemas<States>
83
+ interface DefineStates {
84
+ <const States extends Machine.StateSchemas>(
85
+ states: States,
86
+ ..._validation: ValidateDefinedStates<NoInfer<States>>
87
+ ): Machine.DefinedStates<States>
88
+ <const States extends Machine.StateSchemas>(states: InvalidDefinedStateTreeInput<States>): never
89
+ }
90
+ type ValidateInputEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>> = InputEvents
91
+ type ValidateInternalEventProtocol<
92
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
93
+ InternalEvents extends ReadonlyArray<Machine.TaggedSchema>
94
+ > = InputEvents | InternalEvents extends ReadonlyArray<Machine.TaggedSchema> ? unknown : never
95
+
96
+ const Proto = {
97
+ ...Inspectable.BaseProto,
98
+ ...PipeablePrototype,
99
+ [TypeId]: TypeId,
100
+ toJSON() {
101
+ return {
102
+ _id: "Machine"
103
+ }
104
+ }
105
+ }
106
+
107
+ const cloneWithHandlers = (
108
+ self: Machine.Any,
109
+ handlers: Machine.StateConfigs<any, any, any, any, any, any, any>
110
+ ): Machine.Any => {
111
+ const machine = Object.create(Proto)
112
+ machine.states = self.states
113
+ machine.events = self.events
114
+ machine.internalEvents = self.internalEvents
115
+ machine.emits = self.emits
116
+ machine.input = self.input
117
+ machine.id = self.id
118
+ machine.initial = self.initial
119
+ machine.stateNodes = self.stateNodes
120
+ machine.makeTargetBuilder = self.makeTargetBuilder
121
+ machine.handlers = handlers
122
+ machine.handle = makeHandle(machine)
123
+ Protocol.copyProtocol(self, machine)
124
+ return machine
125
+ }
126
+
127
+ const validateTransitionTargets = (
128
+ stateNodes: Machine.StateNodes,
129
+ path: string,
130
+ trigger: PropertyKey,
131
+ transition: unknown
132
+ ): void => {
133
+ if (typeof transition !== "object" || transition === null || !hasProperty(transition, "targets")) {
134
+ return
135
+ }
136
+ if (!Array.isArray(transition.targets)) {
137
+ throw new Error(
138
+ `Machine expected transition targets for state "${path}" on "${String(trigger)}" to be an array`
139
+ )
140
+ }
141
+ for (const target of transition.targets) {
142
+ if (typeof target !== "string" || !stateNodes.byPath.has(target)) {
143
+ throw new Error(
144
+ `Machine transition for state "${path}" on "${String(trigger)}" declares unknown target "${String(target)}"`
145
+ )
146
+ }
147
+ }
148
+ }
149
+
150
+ const captureTransition = (transition: unknown): unknown => {
151
+ if (typeof transition !== "object" || transition === null) {
152
+ return transition
153
+ }
154
+ const captured = { ...(transition as Record<PropertyKey, unknown>) }
155
+ if (Array.isArray(captured.targets)) {
156
+ captured.targets = captured.targets.slice()
157
+ }
158
+ return captured
159
+ }
160
+
161
+ const captureEventHandlers = (on: object): Record<PropertyKey, unknown> => {
162
+ // The machine owns its dispatch table. Compiled plans may snapshot these
163
+ // definitions, so retaining caller-owned containers would let strategies
164
+ // observe different handlers after an unsafe external mutation.
165
+ const captured: Record<PropertyKey, unknown> = Object.create(null)
166
+ for (const event of Reflect.ownKeys(on)) {
167
+ captured[event] = captureTransition((on as Record<PropertyKey, unknown>)[event])
168
+ }
169
+ return captured
170
+ }
171
+
172
+ const flattenHandlers = (
173
+ handlers: Record<PropertyKey, Machine.AnyStateConfig>,
174
+ stateNodes: Machine.StateNodes,
175
+ states: Machine.StateTree,
176
+ prefix: string,
177
+ config: Record<string, unknown>
178
+ ): void => {
179
+ for (const key of Object.keys(config)) {
180
+ const path = prefix === "" ? key : `${prefix}.${key}`
181
+ if (!hasProperty(states, key)) {
182
+ throw new Error(`Machine received handler for unknown state "${path}"`)
183
+ }
184
+ const nodeConfig = config[key]
185
+ if (typeof nodeConfig !== "object" || nodeConfig === null) {
186
+ throw new Error(`Machine expected state "${path}" handler to be an object`)
187
+ }
188
+ const { states: childConfig, ...stateConfig } = nodeConfig as Record<string, unknown>
189
+ const on = stateConfig.on
190
+ if (typeof on === "object" && on !== null) {
191
+ const capturedOn = captureEventHandlers(on)
192
+ stateConfig.on = capturedOn
193
+ for (const event of Reflect.ownKeys(capturedOn)) {
194
+ validateTransitionTargets(stateNodes, path, event, capturedOn[event])
195
+ }
196
+ }
197
+ validateTransitionTargets(stateNodes, path, "always", stateConfig.always)
198
+ validateTransitionTargets(stateNodes, path, "done", stateConfig.onDone)
199
+ validateTransitionTargets(stateNodes, path, "choice", stateConfig.choice)
200
+ const node = stateNodes.byPath.get(path)
201
+ if (node?.type === "choice") {
202
+ if (
203
+ typeof stateConfig.choice !== "object" || stateConfig.choice === null ||
204
+ !hasProperty(stateConfig.choice, "transition") || typeof stateConfig.choice.transition !== "function" ||
205
+ !hasProperty(stateConfig.choice, "targets") || !Array.isArray(stateConfig.choice.targets) ||
206
+ stateConfig.choice.targets.length === 0
207
+ ) {
208
+ throw new Error(`Machine choice state "${path}" requires a transition and at least one declared target`)
209
+ }
210
+ }
211
+ handlers[path] = stateConfig as Machine.AnyStateConfig
212
+ if (childConfig !== undefined) {
213
+ const node = Topology.getStateNodeDefinition(path, states[key]!)
214
+ if (node.states === undefined) {
215
+ throw new Error(`Machine expected state "${path}" to declare child states`)
216
+ }
217
+ if (typeof childConfig !== "object" || childConfig === null) {
218
+ throw new Error(`Machine expected state "${path}" child handlers to be an object`)
219
+ }
220
+ flattenHandlers(handlers, stateNodes, node.states, path, childConfig as Record<string, unknown>)
221
+ }
222
+ }
223
+ }
224
+
225
+ const makeHandle = (self: Machine.Any): Machine.Any["handle"] =>
226
+ ((config: Record<string, unknown>) => {
227
+ const handlers: Record<PropertyKey, Machine.AnyStateConfig> = Object.assign(
228
+ Object.create(null),
229
+ self.handlers
230
+ )
231
+ flattenHandlers(handlers, self.stateNodes, self.states, "", config)
232
+ return cloneWithHandlers(self, handlers)
233
+ }) as Machine.Any["handle"]
234
+
235
+ export const isMachine = (
236
+ u: unknown
237
+ ): u is Machine.Any => hasProperty(u, TypeId) && u[TypeId] === TypeId
238
+
239
+ export const isFinal = <
240
+ const States extends Machine.StateSchemas,
241
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
242
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
243
+ const Input extends Schema.Top = typeof Schema.Void,
244
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
245
+ E = never,
246
+ R = never,
247
+ InitialE = never,
248
+ InitialR = never,
249
+ FinalStates extends Machine.StateIdentifier<States> = never,
250
+ Output = never,
251
+ OutputStates extends Machine.StateIdentifier<States> = never,
252
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
253
+ >(
254
+ machine: Machine<
255
+ States,
256
+ Events,
257
+ Input,
258
+ UnhandledStates,
259
+ E,
260
+ R,
261
+ InitialE,
262
+ InitialR,
263
+ FinalStates,
264
+ Output,
265
+ Emits,
266
+ OutputStates,
267
+ InputEvents
268
+ >,
269
+ state: Machine.Snapshot<States>
270
+ ): state is Machine.SnapshotContainingFinal<States, FinalStates> => internalPlanner.isFinal(machine as any, state)
271
+
272
+ type SnapshotBuilderOptions = {
273
+ readonly mode: "initial" | "full"
274
+ readonly prefix: string
275
+ }
276
+
277
+ type FromMethodKind = "leaf" | "nested"
278
+
279
+ const withFrom = <Method extends (value: unknown, ...args: ReadonlyArray<any>) => unknown>(
280
+ method: Method,
281
+ kind: FromMethodKind
282
+ ): Method & { readonly from: (...args: ReadonlyArray<any>) => unknown } => {
283
+ Object.defineProperty(method, "from", {
284
+ value: (...args: ReadonlyArray<any>) => {
285
+ const omitted = args.length === 0 || (kind === "nested" && args.length === 1 && typeof args[0] === "function")
286
+ const input = omitted ? {} : args[0]
287
+ const rest = omitted ? args : args.slice(1)
288
+ return method(Topology.makeStateInput(input), ...rest)
289
+ },
290
+ enumerable: false
291
+ })
292
+ return method as Method & { readonly from: (...args: ReadonlyArray<any>) => unknown }
293
+ }
294
+
295
+ const makeSnapshotBuilder = (
296
+ states: Machine.StateTree,
297
+ options: SnapshotBuilderOptions
298
+ ): unknown => {
299
+ const builder: Record<string, unknown> = {}
300
+ for (const key of Object.keys(states)) {
301
+ const definition = states[key]!
302
+ const pseudoType = (definition as { readonly type?: unknown }).type
303
+ if (pseudoType === "history") {
304
+ continue
305
+ }
306
+ const path = options.prefix === "" ? key : `${options.prefix}.${key}`
307
+ if (pseudoType === "choice") {
308
+ builder[key] = () => Topology.makeChoiceTarget(path, getParentPathRuntime(path))
309
+ continue
310
+ }
311
+ const node = Topology.getStateNodeDefinition(path, definition)
312
+ builder[key] = withFrom(
313
+ (value: unknown, selector?: (builder: unknown) => unknown) =>
314
+ makeSnapshotForNode(definition, key, value, selector, options),
315
+ node.states === undefined ? "leaf" : "nested"
316
+ )
317
+ }
318
+ return builder
319
+ }
320
+
321
+ const makeParallelSnapshotBuilder = (
322
+ states: Machine.StateTree,
323
+ options: SnapshotBuilderOptions,
324
+ regions: Readonly<Record<string, unknown>>
325
+ ): unknown => {
326
+ const builder: Record<string, unknown> = {}
327
+ Object.defineProperty(builder, SnapshotBuilderStateTypeId, {
328
+ value: regions,
329
+ enumerable: false
330
+ })
331
+ for (const key of Object.keys(states)) {
332
+ const definition = states[key]!
333
+ const pseudoType = (definition as { readonly type?: unknown }).type
334
+ if (pseudoType === "history" || pseudoType === "choice") {
335
+ continue
336
+ }
337
+ if (hasProperty(regions, key)) {
338
+ continue
339
+ }
340
+ const path = options.prefix === "" ? key : `${options.prefix}.${key}`
341
+ const node = Topology.getStateNodeDefinition(path, definition)
342
+ builder[key] = withFrom((value: unknown, selector?: (builder: unknown) => unknown) => {
343
+ const nextRegions: Record<string, unknown> = {}
344
+ for (const regionKey of Object.keys(regions)) {
345
+ nextRegions[regionKey] = regions[regionKey]
346
+ }
347
+ nextRegions[key] = makeSnapshotForNode(definition, key, value, selector, options)
348
+ return makeParallelSnapshotBuilder(states, options, nextRegions)
349
+ }, node.states === undefined ? "leaf" : "nested")
350
+ }
351
+ return builder
352
+ }
353
+
354
+ const getParallelSnapshotBuilderRegions = (
355
+ path: string,
356
+ states: Machine.StateTree,
357
+ builder: unknown
358
+ ): Readonly<Record<string, unknown>> => {
359
+ if (typeof builder !== "object" || builder === null || !hasProperty(builder, SnapshotBuilderStateTypeId)) {
360
+ throw new Error(`Machine expected parallel state "${path}" builder callback to return a builder`)
361
+ }
362
+ const regions = (builder as { readonly [SnapshotBuilderStateTypeId]: Readonly<Record<string, unknown>> })[
363
+ SnapshotBuilderStateTypeId
364
+ ]
365
+ for (const key of Object.keys(states)) {
366
+ const pseudoType = (states[key] as { readonly type?: unknown }).type
367
+ if (pseudoType === "history" || pseudoType === "choice") {
368
+ continue
369
+ }
370
+ if (!hasProperty(regions, key)) {
371
+ throw new Error(`Machine expected parallel state "${path}" builder callback to provide region "${key}"`)
372
+ }
373
+ }
374
+ return regions
375
+ }
376
+
377
+ const makeSnapshotForNode = (
378
+ definition: Machine.TaggedSchema | Machine.StateNodeConfig,
379
+ key: string,
380
+ value: unknown,
381
+ selector: ((builder: unknown) => unknown) | undefined,
382
+ options: SnapshotBuilderOptions
383
+ ): Record<string, unknown> => {
384
+ const path = options.prefix === "" ? key : `${options.prefix}.${key}`
385
+ const node = Topology.getStateNodeDefinition(path, definition)
386
+ const snapshot: Record<string, unknown> = {
387
+ path,
388
+ value
389
+ }
390
+ if (node.states === undefined) {
391
+ return snapshot
392
+ }
393
+ if (selector === undefined) {
394
+ throw new Error(`Machine expected state "${path}" builder to provide active child states`)
395
+ }
396
+ if (node.type === "parallel") {
397
+ const builder = makeParallelSnapshotBuilder(node.states, { ...options, prefix: path }, {})
398
+ const selected = selector(builder)
399
+ snapshot.states = getParallelSnapshotBuilderRegions(path, node.states, selected)
400
+ return snapshot
401
+ }
402
+ const childStates = options.mode === "initial" && node.initial !== undefined
403
+ ? { [node.initial]: node.states[node.initial]! }
404
+ : node.states
405
+ const selected = selector(makeSnapshotBuilder(childStates, { ...options, prefix: path }))
406
+ snapshot.state = selected
407
+ return snapshot
408
+ }
409
+
410
+ const getTargetBuilderNode = (
411
+ stateNodes: Machine.StateNodes,
412
+ path: string
413
+ ): Machine.StateNode => {
414
+ const node = stateNodes.byPath.get(path)
415
+ if (node === undefined) {
416
+ throw new Error(`Machine expected state path "${path}" to exist`)
417
+ }
418
+ return node
419
+ }
420
+
421
+ const getLocalTargetScope = (
422
+ stateNodes: Machine.StateNodes,
423
+ source: string
424
+ ): string | undefined => {
425
+ let current: string | undefined = source
426
+ while (current !== undefined) {
427
+ const node = stateNodes.byPath.get(current)
428
+ if (node === undefined) {
429
+ return undefined
430
+ }
431
+ if (node.type === "compound") {
432
+ return node.path
433
+ }
434
+ current = node.parent
435
+ }
436
+ return undefined
437
+ }
438
+
439
+ const hasTargetValues = (
440
+ values: Readonly<Record<string, unknown>> | undefined
441
+ ): values is Readonly<Record<string, unknown>> => values !== undefined && Object.keys(values).length > 0
442
+
443
+ const makeTargetWithValues = (
444
+ path: string,
445
+ value: unknown,
446
+ values: Readonly<Record<string, unknown>> | undefined
447
+ ): Machine.Target<any, any> =>
448
+ hasTargetValues(values)
449
+ ? Topology.makeTarget(path as any, value as any, { values: values as any })
450
+ : Topology.makeTarget(path as any, value as any)
451
+
452
+ const getTargetBuilderDefinition = (
453
+ states: Machine.StateTree,
454
+ targetPath: string
455
+ ): Machine.TaggedSchema | Machine.StateNodeConfig => {
456
+ let children = states
457
+ let path = ""
458
+ let definition: Machine.TaggedSchema | Machine.StateNodeConfig | undefined
459
+ for (const key of targetPath.split(".")) {
460
+ if (!hasProperty(children, key)) {
461
+ throw new Error(`Machine expected state path "${targetPath}" to exist`)
462
+ }
463
+ definition = children[key]!
464
+ path = path === "" ? key : `${path}.${key}`
465
+ const node = Topology.getStateNodeDefinition(path, definition)
466
+ children = node.states ?? {}
467
+ }
468
+ return definition!
469
+ }
470
+
471
+ const makeParallelTarget = (
472
+ states: Machine.StateTree,
473
+ node: Machine.StateNode,
474
+ value: unknown,
475
+ selector: ((builder: unknown) => unknown) | undefined,
476
+ values: Readonly<Record<string, unknown>> | undefined
477
+ ): Machine.Target<any, any> => {
478
+ if (selector === undefined) {
479
+ throw new Error(`Machine expected parallel target "${node.path}" builder to provide every active region`)
480
+ }
481
+ const snapshot = makeSnapshotForNode(
482
+ getTargetBuilderDefinition(states, node.path),
483
+ node.key,
484
+ value,
485
+ selector,
486
+ { mode: "full", prefix: node.parent ?? "" }
487
+ )
488
+ return Topology.makeTarget(node.path as any, value as any, {
489
+ snapshot: snapshot as any,
490
+ values: values as any
491
+ })
492
+ }
493
+
494
+ const extendTargetValues = (
495
+ values: Readonly<Record<string, unknown>> | undefined,
496
+ path: string,
497
+ value: unknown
498
+ ): Readonly<Record<string, unknown>> => {
499
+ const next: Record<string, unknown> = {}
500
+ if (values !== undefined) {
501
+ for (const key of Object.keys(values)) {
502
+ next[key] = values[key]
503
+ }
504
+ }
505
+ next[path] = value
506
+ return next
507
+ }
508
+
509
+ const makeLocalTargetChildBuilder = (
510
+ states: Machine.StateTree,
511
+ stateNodes: Machine.StateNodes,
512
+ parentPath: string,
513
+ values: Readonly<Record<string, unknown>> | undefined,
514
+ source: string
515
+ ): unknown => {
516
+ const parent = getTargetBuilderNode(stateNodes, parentPath)
517
+ const builder: Record<string, unknown> = {}
518
+ for (
519
+ const childPath of Array.from(stateNodes.byPath.values())
520
+ .filter((node) => node.parent === parent.path && node.type !== "history")
521
+ .map((node) => node.path)
522
+ ) {
523
+ const child = getTargetBuilderNode(stateNodes, childPath)
524
+ if (child.type === "choice") {
525
+ builder[child.key] = () => Topology.makeChoiceTarget(child.path, parent.path, values)
526
+ continue
527
+ }
528
+ builder[child.key] = withFrom((value: unknown, selector?: (builder: unknown) => unknown) => {
529
+ if (child.type === "atomic" || child.type === "final") {
530
+ return makeTargetWithValues(child.path, value, values)
531
+ }
532
+ if (child.type === "parallel") {
533
+ if (source !== child.path && !source.startsWith(`${child.path}.`)) {
534
+ return makeParallelTarget(states, child, value, selector, values)
535
+ }
536
+ if (selector === undefined) {
537
+ throw new Error(`Machine expected target "${child.path}" builder to provide an active child state`)
538
+ }
539
+ return selector(makeLocalTargetChildBuilder(
540
+ states,
541
+ stateNodes,
542
+ child.path,
543
+ extendTargetValues(values, child.path, value),
544
+ source
545
+ ))
546
+ }
547
+ if (selector === undefined) {
548
+ throw new Error(`Machine expected target "${child.path}" builder to provide an active child state`)
549
+ }
550
+ return selector(makeLocalTargetChildBuilder(
551
+ states,
552
+ stateNodes,
553
+ child.path,
554
+ extendTargetValues(values, child.path, value),
555
+ source
556
+ ))
557
+ }, child.type === "atomic" || child.type === "final" ? "leaf" : "nested")
558
+ }
559
+ return builder
560
+ }
561
+
562
+ const makeLocalTargetBuilder = (
563
+ states: Machine.StateTree,
564
+ stateNodes: Machine.StateNodes,
565
+ source: string
566
+ ): unknown => {
567
+ const scope = getLocalTargetScope(stateNodes, source)
568
+ if (scope === undefined) {
569
+ return {}
570
+ }
571
+ const builder = makeLocalTargetChildBuilder(states, stateNodes, scope, undefined, source) as Record<string, unknown>
572
+ builder.with = withFrom((value: unknown, selector?: (builder: unknown) => unknown) => {
573
+ if (selector === undefined) {
574
+ throw new Error(`Machine expected target "${scope}" builder to provide an active child state`)
575
+ }
576
+ return selector(makeLocalTargetChildBuilder(states, stateNodes, scope, { [scope]: value }, source))
577
+ }, "nested")
578
+ return builder
579
+ }
580
+
581
+ const addBranchTargetChildren = (
582
+ builder: Record<string, unknown>,
583
+ states: Machine.StateTree,
584
+ stateNodes: Machine.StateNodes,
585
+ parentPath: string,
586
+ values: Readonly<Record<string, unknown>> | undefined,
587
+ source: string
588
+ ): void => {
589
+ const parent = getTargetBuilderNode(stateNodes, parentPath)
590
+ for (
591
+ const childPath of Array.from(stateNodes.byPath.values())
592
+ .filter((node) => node.parent === parent.path && node.type !== "history")
593
+ .map((node) => node.path)
594
+ ) {
595
+ const child = getTargetBuilderNode(stateNodes, childPath)
596
+ if (child.type === "choice") {
597
+ builder[child.key] = () => Topology.makeChoiceTarget(child.path, parent.path, values)
598
+ continue
599
+ }
600
+ builder[child.key] = makeBranchTargetNodeBuilder(states, stateNodes, child.path, values, source)
601
+ }
602
+ }
603
+
604
+ const makeBranchTargetNodeBuilder = (
605
+ states: Machine.StateTree,
606
+ stateNodes: Machine.StateNodes,
607
+ path: string,
608
+ values: Readonly<Record<string, unknown>> | undefined,
609
+ source: string
610
+ ): unknown => {
611
+ const node = getTargetBuilderNode(stateNodes, path)
612
+ if (node.type === "atomic" || node.type === "final") {
613
+ return withFrom((value: unknown) => makeTargetWithValues(node.path, value, values), "leaf")
614
+ }
615
+ const builder = withFrom((value: unknown, selector?: (builder: unknown) => unknown) => {
616
+ if (node.type === "parallel") {
617
+ if (source !== node.path && !source.startsWith(`${node.path}.`)) {
618
+ return makeParallelTarget(states, node, value, selector, values)
619
+ }
620
+ if (selector === undefined) {
621
+ throw new Error(`Machine expected target "${node.path}" builder to provide an active child state`)
622
+ }
623
+ const nextBuilder: Record<string, unknown> = {}
624
+ addBranchTargetChildren(
625
+ nextBuilder,
626
+ states,
627
+ stateNodes,
628
+ node.path,
629
+ extendTargetValues(values, node.path, value),
630
+ source
631
+ )
632
+ return selector(nextBuilder)
633
+ }
634
+ if (selector === undefined) {
635
+ throw new Error(`Machine expected target "${node.path}" builder to provide an active child state`)
636
+ }
637
+ const nextBuilder: Record<string, unknown> = {}
638
+ addBranchTargetChildren(
639
+ nextBuilder,
640
+ states,
641
+ stateNodes,
642
+ node.path,
643
+ extendTargetValues(values, node.path, value),
644
+ source
645
+ )
646
+ return selector(nextBuilder)
647
+ }, "nested") as unknown as Record<string, unknown>
648
+ if (node.type !== "parallel" || source === node.path || source.startsWith(`${node.path}.`)) {
649
+ addBranchTargetChildren(builder, states, stateNodes, node.path, values, source)
650
+ }
651
+ return builder
652
+ }
653
+
654
+ const makeBranchTargetBuilder = (
655
+ states: Machine.StateTree,
656
+ stateNodes: Machine.StateNodes,
657
+ source: string
658
+ ): unknown => {
659
+ const rootPath = source.split(".")[0]!
660
+ const root = getTargetBuilderNode(stateNodes, rootPath)
661
+ return {
662
+ [root.key]: makeBranchTargetNodeBuilder(states, stateNodes, root.path, undefined, source)
663
+ }
664
+ }
665
+
666
+ const makeHistoryTargetBuilder = (
667
+ states: Machine.StateTree,
668
+ prefix: string
669
+ ): unknown => {
670
+ const builder: Record<string, unknown> = {}
671
+ for (const key of Object.keys(states)) {
672
+ const path = prefix === "" ? key : `${prefix}.${key}`
673
+ const definition = Topology.getStateNodeDefinition(path, states[key]!)
674
+ if (definition.type === "history") {
675
+ const parent = getParentPathRuntime(path)
676
+ builder[key] = () => Topology.makeHistoryTarget(path, parent)
677
+ continue
678
+ }
679
+ if (definition.states !== undefined) {
680
+ builder[key] = makeHistoryTargetBuilder(definition.states, path)
681
+ }
682
+ }
683
+ return builder
684
+ }
685
+
686
+ const getParentPathRuntime = (path: string): string => {
687
+ const separator = path.lastIndexOf(".")
688
+ if (separator < 0) {
689
+ throw new Error(`Machine expected history state "${path}" to have an active parent`)
690
+ }
691
+ return path.slice(0, separator)
692
+ }
693
+
694
+ const makeTargetBuilder = <const States extends Machine.StateSchemas>(
695
+ states: States,
696
+ stateNodes: Machine.StateNodes
697
+ ) => {
698
+ const full = makeSnapshotBuilder(states, { mode: "full", prefix: "" }) as Machine.FullTargetBuilder<States>
699
+ const history = makeHistoryTargetBuilder(states, "") as Machine.HistoryTargetBuilder<States>
700
+ return <Source extends Machine.StateNodeIdentifier<States>>(source: Source): Machine.TargetBuilder<States, Source> =>
701
+ ({
702
+ local: makeLocalTargetBuilder(states, stateNodes, source),
703
+ branch: makeBranchTargetBuilder(states, stateNodes, source),
704
+ full,
705
+ history
706
+ }) as Machine.TargetBuilder<States, Source>
707
+ }
708
+
709
+ export const defineStates: DefineStates = (<const States extends Machine.StateSchemas>(
710
+ states: States
711
+ ): Machine.DefinedStates<States> => {
712
+ StateDefinition.validateStateDefinitions(states, "Machine.defineStates")
713
+ return {
714
+ states,
715
+ initial: makeSnapshotBuilder(states, { mode: "initial", prefix: "" }) as Machine.InitialBuilder<States>,
716
+ get: ((snapshot, path) =>
717
+ Topology.getSnapshotByPath(snapshot, path).pipe(
718
+ Option.map((snapshot) => snapshot.value)
719
+ )) as Machine.DefinedStates<States>["get"],
720
+ getWithParents: ((snapshot, path) => {
721
+ const parents: Record<string, unknown> = {}
722
+ return Topology.getSnapshotByPath(snapshot, path, parents).pipe(
723
+ Option.map((snapshot) => ({ value: snapshot.value, parents }))
724
+ )
725
+ }) as Machine.DefinedStates<States>["getWithParents"],
726
+ getSnapshot: Topology.getSnapshotByPath as unknown as Machine.DefinedStates<States>["getSnapshot"],
727
+ matches: (snapshot, path) => Option.isSome(Topology.getSnapshotByPath(snapshot, path))
728
+ }
729
+ }) as DefineStates
730
+
731
+ type MakeConfig<
732
+ States extends Machine.StateSchemas,
733
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
734
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
735
+ Input extends Schema.Top,
736
+ InitialE,
737
+ InitialR,
738
+ InternalEvents extends ReadonlyArray<Machine.TaggedSchema>
739
+ > = {
740
+ readonly id?: string
741
+ readonly states: States & DefineStateTreeInput<NoInfer<States>>
742
+ readonly events: InputEvents & ValidateInputEventProtocol<NoInfer<InputEvents>>
743
+ readonly internalEvents?:
744
+ & InternalEvents
745
+ & ValidateInternalEventProtocol<
746
+ NoInfer<InputEvents>,
747
+ NoInfer<InternalEvents>
748
+ >
749
+ readonly emits?: Emits
750
+ readonly input?: Input
751
+ readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
752
+ }
753
+
754
+ type MakeResult<
755
+ States extends Machine.StateSchemas,
756
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
757
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
758
+ Input extends Schema.Top,
759
+ InitialE,
760
+ InitialR,
761
+ InternalEvents extends ReadonlyArray<Machine.TaggedSchema>
762
+ > = Machine<
763
+ States,
764
+ readonly [...InputEvents, ...InternalEvents],
765
+ Input,
766
+ Machine.StateIdentifier<States>,
767
+ never,
768
+ never,
769
+ InitialE,
770
+ InitialR,
771
+ Machine.FinalStateFromDefinition<States>,
772
+ Machine.TerminalOutput<States>,
773
+ Emits,
774
+ never,
775
+ InputEvents
776
+ >
777
+
778
+ interface Make {
779
+ <
780
+ const States extends Machine.StateSchemas,
781
+ const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
782
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
783
+ const Input extends Schema.Top = typeof Schema.Void,
784
+ InitialE = never,
785
+ InitialR = never,
786
+ const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
787
+ >(
788
+ config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>,
789
+ ..._validation: ValidateDefinedStates<NoInfer<States>>
790
+ ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>
791
+ <
792
+ const States extends Machine.StateSchemas,
793
+ const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
794
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
795
+ const Input extends Schema.Top = typeof Schema.Void,
796
+ InitialE = never,
797
+ InitialR = never,
798
+ const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
799
+ >(
800
+ config:
801
+ & Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>, "states">
802
+ & { readonly states: InvalidDefinedStateTreeInput<States> }
803
+ ): never
804
+ }
805
+
806
+ export const make: Make = (<
807
+ const States extends Machine.StateSchemas,
808
+ const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
809
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
810
+ const Input extends Schema.Top = typeof Schema.Void,
811
+ InitialE = never,
812
+ InitialR = never,
813
+ const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
814
+ >(
815
+ config: {
816
+ readonly id?: string
817
+ readonly states: States
818
+ readonly events: InputEvents
819
+ readonly internalEvents?: InternalEvents
820
+ readonly emits?: Emits
821
+ readonly input?: Input
822
+ readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
823
+ }
824
+ ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents> => {
825
+ StateDefinition.validateStateDefinitions(config.states, "Machine.make")
826
+ const self = Object.create(Proto)
827
+ self.states = config.states
828
+ self.events = config.events
829
+ self.internalEvents = config.internalEvents ?? []
830
+ self.emits = config.emits ?? []
831
+ self.input = config.input
832
+ self.id = config.id
833
+ self.initial = config.initial
834
+ self.stateNodes = Topology.compileStateNodes(config.states)
835
+ self.makeTargetBuilder = makeTargetBuilder(config.states, self.stateNodes)
836
+ self.handlers = Object.create(null)
837
+ self.handle = makeHandle(self)
838
+ Protocol.setProtocol(self)
839
+ return self
840
+ }) as Make
841
+
842
+ type EventConstructorArgs<EventSchema extends Machine.TaggedSchema> = {} extends EventSchema["~type.make.in"] ?
843
+ [input?: EventSchema["~type.make.in"]]
844
+ : [input: EventSchema["~type.make.in"]]
845
+
846
+ export const event = <
847
+ const M extends Machine.Any,
848
+ const EventSchema extends Machine.TaggedSchema
849
+ >(
850
+ machine: M,
851
+ schema: EventSchema & ([EventSchema["Type"]] extends [Machine.Event<M>] ? unknown : never),
852
+ ...args: EventConstructorArgs<EventSchema>
853
+ ): EventSchema["Type"] => Protocol.makeEvent(machine, schema, args.length === 0 ? {} : args[0])
854
+
855
+ export const encodeSnapshot: <
856
+ const States extends Machine.StateSchemas,
857
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
858
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
859
+ const Input extends Schema.Top = typeof Schema.Void,
860
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
861
+ E = never,
862
+ R = never,
863
+ InitialE = never,
864
+ InitialR = never,
865
+ FinalStates extends Machine.StateIdentifier<States> = never,
866
+ Output = never,
867
+ OutputStates extends Machine.StateIdentifier<States> = never,
868
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
869
+ >(
870
+ machine: Machine<
871
+ States,
872
+ Events,
873
+ Input,
874
+ UnhandledStates,
875
+ E,
876
+ R,
877
+ InitialE,
878
+ InitialR,
879
+ FinalStates,
880
+ Output,
881
+ Emits,
882
+ OutputStates,
883
+ InputEvents
884
+ >,
885
+ snapshot: Machine.Snapshot<States>
886
+ ) => Effect.Effect<
887
+ Machine.EncodedSnapshot,
888
+ MachineSchemaEncodeError,
889
+ Machine.SnapshotEncodingServices<States>
890
+ > = Serialization.encodeSnapshot as any
891
+
892
+ export const decodeSnapshot: <
893
+ const States extends Machine.StateSchemas,
894
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
895
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
896
+ const Input extends Schema.Top = typeof Schema.Void,
897
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
898
+ E = never,
899
+ R = never,
900
+ InitialE = never,
901
+ InitialR = never,
902
+ FinalStates extends Machine.StateIdentifier<States> = never,
903
+ Output = never,
904
+ OutputStates extends Machine.StateIdentifier<States> = never,
905
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
906
+ >(
907
+ machine: Machine<
908
+ States,
909
+ Events,
910
+ Input,
911
+ UnhandledStates,
912
+ E,
913
+ R,
914
+ InitialE,
915
+ InitialR,
916
+ FinalStates,
917
+ Output,
918
+ Emits,
919
+ OutputStates,
920
+ InputEvents
921
+ >,
922
+ encoded: unknown
923
+ ) => Effect.Effect<
924
+ Machine.Snapshot<States>,
925
+ MachineSchemaDecodeError,
926
+ Machine.SnapshotDecodingServices<States>
927
+ > = Serialization.decodeSnapshot as any
928
+
929
+ export const invoke = <
930
+ ChildState,
931
+ ChildEvent,
932
+ ChildError = never,
933
+ ChildRequirements = never,
934
+ ChildOutput = never,
935
+ ChildInitialError = never,
936
+ Event = never,
937
+ Address extends ChildAddress<never> | undefined = undefined
938
+ >(
939
+ config:
940
+ & {
941
+ readonly id: InvokeLifecycleId
942
+ readonly src: () => Logic<
943
+ ChildState,
944
+ ChildEvent,
945
+ ChildError,
946
+ ChildRequirements,
947
+ ChildOutput,
948
+ ChildInitialError
949
+ >
950
+ readonly snapshot?: (
951
+ context: Machine.InvokeSnapshotContext<ChildState, ChildError, ChildOutput>
952
+ ) => Event | undefined
953
+ }
954
+ & ([Address] extends [undefined] ? {
955
+ readonly address?: never
956
+ }
957
+ : {
958
+ readonly address: Exclude<Address, undefined>
959
+ } & ChildAddress.Compatibility<Exclude<Address, undefined>, NoInfer<ChildEvent>>)
960
+ ): Machine.InvokeConfig<
961
+ any,
962
+ any,
963
+ any,
964
+ any,
965
+ Event,
966
+ ChildState,
967
+ ChildEvent,
968
+ ChildError,
969
+ ChildRequirements,
970
+ ChildOutput,
971
+ ChildInitialError
972
+ > => ({
973
+ ...config,
974
+ [InvokeTypeId]: undefined as any,
975
+ [Activities.ActivityMetadataTypeId]: { type: "process" }
976
+ })
977
+
978
+ type InvokeEffectResult<Requirements, Event> = Machine.InvokeConfig<
979
+ any,
980
+ any,
981
+ any,
982
+ any,
983
+ never,
984
+ void,
985
+ never,
986
+ never,
987
+ Requirements,
988
+ Event | void,
989
+ never
990
+ >
991
+
992
+ type InvokeEffectIsInfallible<Fx extends Effect.Effect<any, any, any>> = IsAny<Effect.Error<Fx>> extends true ? false
993
+ : [Effect.Error<Fx>] extends [never] ? true
994
+ : false
995
+
996
+ type InvokeEffectConfig<
997
+ Fx extends Effect.Effect<any, any, any>,
998
+ SuccessEvent,
999
+ FailureEvent
1000
+ > =
1001
+ & {
1002
+ readonly id: InvokeLifecycleId
1003
+ readonly effect: Fx
1004
+ readonly onSuccess: (value: NoInfer<Effect.Success<Fx>>) => SuccessEvent | void
1005
+ }
1006
+ & (
1007
+ InvokeEffectIsInfallible<Fx> extends true ? {
1008
+ readonly onFailure?: never
1009
+ }
1010
+ : {
1011
+ readonly onFailure: (error: NoInfer<Effect.Error<Fx>>) => FailureEvent | void
1012
+ }
1013
+ )
1014
+
1015
+ export const invokeEffect = <
1016
+ const Fx extends Effect.Effect<any, any, any>,
1017
+ SuccessEvent,
1018
+ FailureEvent = never
1019
+ >(
1020
+ config: InvokeEffectConfig<Fx, SuccessEvent, FailureEvent>
1021
+ ): InvokeEffectResult<
1022
+ Effect.Services<Fx>,
1023
+ SuccessEvent | (InvokeEffectIsInfallible<Fx> extends true ? never : FailureEvent)
1024
+ > =>
1025
+ ((config: {
1026
+ readonly id: string
1027
+ readonly effect: Effect.Effect<unknown, unknown, unknown>
1028
+ readonly onSuccess: (value: unknown) => unknown
1029
+ readonly onFailure?: (error: unknown) => unknown
1030
+ }) => ({
1031
+ ...invoke({
1032
+ id: config.id,
1033
+ src: () =>
1034
+ effect(
1035
+ config.onFailure === undefined
1036
+ ? Effect.map(config.effect, config.onSuccess)
1037
+ : Effect.matchEffect(config.effect, {
1038
+ onFailure: (error) => Effect.succeed(config.onFailure!(error)),
1039
+ onSuccess: (value) => Effect.succeed(config.onSuccess(value))
1040
+ })
1041
+ )
1042
+ }),
1043
+ [Activities.ActivityMetadataTypeId]: {
1044
+ type: "effect",
1045
+ outcomes: {
1046
+ success: "dynamic",
1047
+ failure: config.onFailure === undefined ? "none" : "dynamic"
1048
+ }
1049
+ }
1050
+ }))(config as any) as any
1051
+
1052
+ export const after = <Event extends { readonly _tag: PropertyKey }>(
1053
+ duration: Duration.Input,
1054
+ event: Event,
1055
+ options?: { readonly id?: InvokeLifecycleId }
1056
+ ): InvokeEffectResult<never, Event> => ({
1057
+ ...invoke({
1058
+ id: options?.id ?? `Machine.after:${String(event._tag)}`,
1059
+ src: () => effect(Effect.as(Effect.sleep(duration), event))
1060
+ }),
1061
+ [Activities.ActivityMetadataTypeId]: {
1062
+ type: "timer",
1063
+ duration: Duration.format(Duration.fromInputUnsafe(duration)),
1064
+ event: String(event._tag)
1065
+ }
1066
+ })
1067
+
1068
+ export const retag = (
1069
+ target: Machine.TaggedSchema,
1070
+ source: { readonly _tag: PropertyKey },
1071
+ patch?: unknown
1072
+ ): any => {
1073
+ const { _tag: _, ...fields } = source
1074
+ return target.make({ ...fields, ...((patch ?? {}) as object) } as never)
1075
+ }
1076
+
1077
+ type InvokeMachineInput<Input extends Schema.Top> = Input extends typeof Schema.Void ? {
1078
+ readonly input?: never
1079
+ }
1080
+ : {
1081
+ readonly input: Input["Type"]
1082
+ }
1083
+
1084
+ export const invokeMachine: {
1085
+ <
1086
+ const States extends Machine.StateSchemas,
1087
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
1088
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
1089
+ const Input extends Schema.Top = typeof Schema.Void,
1090
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
1091
+ E = never,
1092
+ R = never,
1093
+ InitialE = never,
1094
+ InitialR = never,
1095
+ FinalStates extends Machine.StateIdentifier<States> = never,
1096
+ Output = never,
1097
+ SnapshotEvent = never,
1098
+ DoneEvent = never,
1099
+ Id extends string = string,
1100
+ OutputStates extends Machine.StateIdentifier<States> = never,
1101
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
1102
+ >(
1103
+ config:
1104
+ & {
1105
+ readonly child: ChildMachine<
1106
+ Id,
1107
+ & Machine<
1108
+ States,
1109
+ Events,
1110
+ Input,
1111
+ UnhandledStates,
1112
+ E,
1113
+ R,
1114
+ InitialE,
1115
+ InitialR,
1116
+ FinalStates,
1117
+ Output,
1118
+ Emits,
1119
+ OutputStates,
1120
+ InputEvents
1121
+ >
1122
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
1123
+ >
1124
+ readonly snapshot?: (
1125
+ context: Machine.InvokeSnapshotContext<
1126
+ Machine.Snapshot<States>,
1127
+ | E
1128
+ | ActionError<R>
1129
+ | InfiniteTransitionError
1130
+ | MachineSchemaDecodeError
1131
+ | StoppedError,
1132
+ Output
1133
+ >
1134
+ ) => SnapshotEvent | undefined
1135
+ readonly onDone: (context: Machine.InvokeDoneContext<Output>) => DoneEvent | undefined
1136
+ }
1137
+ & InvokeMachineInput<Input>
1138
+ ): Machine.InvokeConfig<
1139
+ any,
1140
+ any,
1141
+ any,
1142
+ any,
1143
+ SnapshotEvent,
1144
+ Machine.Snapshot<States>,
1145
+ Machine.EventOf<InputEvents>,
1146
+ E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
1147
+ ExcludeCompatibleRuntime<
1148
+ Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>,
1149
+ Machine.EventOf<Events>,
1150
+ Machine.EmitOf<Emits>
1151
+ >,
1152
+ Output,
1153
+ | InitialE
1154
+ | E
1155
+ | ActionError<InitialR | R>
1156
+ | InfiniteTransitionError
1157
+ | MachineSchemaDecodeError
1158
+ | StartupError
1159
+ | StoppedError,
1160
+ Machine.EmitOf<Emits>,
1161
+ DoneEvent
1162
+ >
1163
+ <
1164
+ const States extends Machine.StateSchemas,
1165
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
1166
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
1167
+ const Input extends Schema.Top = typeof Schema.Void,
1168
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
1169
+ E = never,
1170
+ R = never,
1171
+ InitialE = never,
1172
+ InitialR = never,
1173
+ FinalStates extends Machine.StateIdentifier<States> = never,
1174
+ Output = never,
1175
+ SnapshotEvent = never,
1176
+ Id extends string = string,
1177
+ OutputStates extends Machine.StateIdentifier<States> = never,
1178
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
1179
+ >(
1180
+ config:
1181
+ & {
1182
+ readonly child: ChildMachine<
1183
+ Id,
1184
+ & Machine<
1185
+ States,
1186
+ Events,
1187
+ Input,
1188
+ UnhandledStates,
1189
+ E,
1190
+ R,
1191
+ InitialE,
1192
+ InitialR,
1193
+ FinalStates,
1194
+ Output,
1195
+ Emits,
1196
+ OutputStates,
1197
+ InputEvents
1198
+ >
1199
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
1200
+ >
1201
+ readonly snapshot?: (
1202
+ context: Machine.InvokeSnapshotContext<
1203
+ Machine.Snapshot<States>,
1204
+ | E
1205
+ | ActionError<R>
1206
+ | InfiniteTransitionError
1207
+ | MachineSchemaDecodeError
1208
+ | StoppedError,
1209
+ Output
1210
+ >
1211
+ ) => SnapshotEvent | undefined
1212
+ readonly onDone?: never
1213
+ }
1214
+ & InvokeMachineInput<Input>
1215
+ ): Machine.InvokeConfig<
1216
+ any,
1217
+ any,
1218
+ any,
1219
+ any,
1220
+ SnapshotEvent,
1221
+ Machine.Snapshot<States>,
1222
+ Machine.EventOf<InputEvents>,
1223
+ E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
1224
+ ExcludeCompatibleRuntime<
1225
+ Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>,
1226
+ Machine.EventOf<Events>,
1227
+ Machine.EmitOf<Emits>
1228
+ >,
1229
+ Output,
1230
+ | InitialE
1231
+ | E
1232
+ | ActionError<InitialR | R>
1233
+ | InfiniteTransitionError
1234
+ | MachineSchemaDecodeError
1235
+ | StartupError
1236
+ | StoppedError,
1237
+ Machine.EmitOf<Emits>
1238
+ >
1239
+ } = ((config: {
1240
+ readonly child: ChildMachine.Any
1241
+ readonly input?: unknown
1242
+ readonly snapshot?: (context: Machine.InvokeSnapshotContext<any, any, any>) => unknown
1243
+ readonly onDone?: (context: Machine.InvokeDoneContext<any>) => unknown
1244
+ }) => {
1245
+ const machine = config.child.machine
1246
+ // An invoke descriptor fixes both its machine and input. Compile its process
1247
+ // logic once; all mutable execution state belongs to the process instance.
1248
+ const logic = machine.input === undefined
1249
+ ? (internalProcess.toProcessLogic as any)(machine)
1250
+ : (internalProcess.toProcessLogic as any)(machine, config.input)
1251
+ return {
1252
+ id: config.child.id,
1253
+ address: config.child.id,
1254
+ descriptor: config.child,
1255
+ src: () => logic,
1256
+ snapshot: config.snapshot,
1257
+ onDone: config.onDone,
1258
+ [Activities.ActivityMetadataTypeId]: {
1259
+ type: "machine",
1260
+ child: {
1261
+ id: config.child.id,
1262
+ machineId: machine.id ?? null
1263
+ }
1264
+ },
1265
+ [InvokeTypeId]: undefined as any
1266
+ }
1267
+ }) as any
1268
+
1269
+ export const planInitial: <
1270
+ const States extends Machine.StateSchemas,
1271
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
1272
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
1273
+ const Input extends Schema.Top = typeof Schema.Void,
1274
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
1275
+ E = never,
1276
+ R = never,
1277
+ InitialE = never,
1278
+ InitialR = never,
1279
+ FinalStates extends Machine.StateIdentifier<States> = never,
1280
+ Output = never,
1281
+ OutputStates extends Machine.StateIdentifier<States> = never,
1282
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
1283
+ >(
1284
+ machine:
1285
+ & Machine<
1286
+ States,
1287
+ Events,
1288
+ Input,
1289
+ UnhandledStates,
1290
+ E,
1291
+ R,
1292
+ InitialE,
1293
+ InitialR,
1294
+ FinalStates,
1295
+ Output,
1296
+ Emits,
1297
+ OutputStates,
1298
+ InputEvents
1299
+ >
1300
+ & EnsureExecutable<States, UnhandledStates, OutputStates>,
1301
+ ...args: [...Machine.InputArgs<Input>]
1302
+ ) => Effect.Effect<
1303
+ & {
1304
+ readonly startingState: Machine.Snapshot<States>
1305
+ readonly initialEntryPaths: ReadonlyArray<Machine.StateIdentifier<States>>
1306
+ readonly state: Machine.Snapshot<States>
1307
+ readonly commands: ReadonlyArray<Command>
1308
+ readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
1309
+ readonly microsteps: ReadonlyArray<{
1310
+ readonly next: Machine.Snapshot<States>
1311
+ readonly event: Machine.EventOf<Events> | InitialEventModel
1312
+ readonly transitions: ReadonlyArray<
1313
+ Machine.RetainedTransition<
1314
+ Machine.StateNodeIdentifier<States>,
1315
+ Machine.TagOf<Events[number]>,
1316
+ Machine.StateNodeIdentifier<States>
1317
+ >
1318
+ >
1319
+ readonly commands: ReadonlyArray<Command>
1320
+ readonly raisedEvents: ReadonlyArray<Machine.EventOf<Events>>
1321
+ readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
1322
+ readonly exitPaths: ReadonlyArray<string>
1323
+ readonly entryPaths: ReadonlyArray<string>
1324
+ readonly changed: boolean
1325
+ }>
1326
+ }
1327
+ & (
1328
+ | {
1329
+ readonly done: true
1330
+ readonly output: Output
1331
+ }
1332
+ | {
1333
+ readonly done: false
1334
+ readonly output: undefined
1335
+ }
1336
+ ),
1337
+ InitialE | E | InfiniteTransitionError | MachineSchemaDecodeError | StartupError,
1338
+ never
1339
+ > = internalPlanner.planInitial as any
1340
+
1341
+ export const stateNodes = <M extends Machine.Any>(
1342
+ machine: M
1343
+ ): ReadonlyArray<
1344
+ Machine.StateNode<
1345
+ Machine.StateIdentifier<Machine.States<M>>,
1346
+ Machine.HistoryIdentifier<Machine.States<M>>,
1347
+ Machine.ChoiceIdentifier<Machine.States<M>>
1348
+ >
1349
+ > =>
1350
+ Array.from(machine.stateNodes.byPath.values()) as unknown as ReadonlyArray<
1351
+ Machine.StateNode<
1352
+ Machine.StateIdentifier<Machine.States<M>>,
1353
+ Machine.HistoryIdentifier<Machine.States<M>>,
1354
+ Machine.ChoiceIdentifier<Machine.States<M>>
1355
+ >
1356
+ >
1357
+
1358
+ export const transitionDefinitions = <M extends Machine.Any>(
1359
+ machine: M
1360
+ ): ReadonlyArray<
1361
+ Machine.TransitionDefinition<
1362
+ Machine.StateNodeIdentifier<Machine.States<M>>,
1363
+ Machine.TagOf<Machine.Events<M>[number]>,
1364
+ Machine.StateNodeIdentifier<Machine.States<M>>
1365
+ >
1366
+ > =>
1367
+ Topology.transitionDefinitions(machine) as ReadonlyArray<
1368
+ Machine.TransitionDefinition<
1369
+ Machine.StateNodeIdentifier<Machine.States<M>>,
1370
+ Machine.TagOf<Machine.Events<M>[number]>,
1371
+ Machine.StateNodeIdentifier<Machine.States<M>>
1372
+ >
1373
+ >
1374
+
1375
+ export const activityDefinitions = <M extends Machine.Any>(
1376
+ machine: M
1377
+ ): ReadonlyArray<Machine.ActivityDefinition<Machine.StateIdentifier<Machine.States<M>>>> =>
1378
+ Activities.activityDefinitions(machine) as ReadonlyArray<
1379
+ Machine.ActivityDefinition<Machine.StateIdentifier<Machine.States<M>>>
1380
+ >
1381
+
1382
+ export const configuration = <M extends Machine.Any>(
1383
+ machine: M,
1384
+ state: Machine.Snapshot<Machine.States<M>>
1385
+ ): ReadonlyArray<
1386
+ Machine.ActiveStateNode<
1387
+ Machine.StateIdentifier<Machine.States<M>>,
1388
+ Machine.ChoiceIdentifier<Machine.States<M>>
1389
+ >
1390
+ > => {
1391
+ const active = Configuration.normalizeConfiguration(machine, state).active
1392
+ return stateNodes(machine).filter(
1393
+ (node): node is Machine.ActiveStateNode<
1394
+ Machine.StateIdentifier<Machine.States<M>>,
1395
+ Machine.ChoiceIdentifier<Machine.States<M>>
1396
+ > => node.type !== "history" && node.type !== "choice" && active.has(node.path)
1397
+ )
1398
+ }
1399
+
1400
+ export const enabled = <
1401
+ const States extends Machine.StateSchemas,
1402
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
1403
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
1404
+ const Input extends Schema.Top = typeof Schema.Void,
1405
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
1406
+ E = never,
1407
+ R = never,
1408
+ InitialE = never,
1409
+ InitialR = never,
1410
+ FinalStates extends Machine.StateIdentifier<States> = never,
1411
+ Output = never,
1412
+ OutputStates extends Machine.StateIdentifier<States> = never,
1413
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
1414
+ >(
1415
+ machine: Machine<
1416
+ States,
1417
+ Events,
1418
+ Input,
1419
+ UnhandledStates,
1420
+ E,
1421
+ R,
1422
+ InitialE,
1423
+ InitialR,
1424
+ FinalStates,
1425
+ Output,
1426
+ Emits,
1427
+ OutputStates,
1428
+ InputEvents
1429
+ >,
1430
+ state: Machine.Snapshot<States>
1431
+ ): ReadonlyArray<Machine.TagOf<Events[number]>> => internalPlanner.enabled(machine as any, state)
1432
+
1433
+ export const plan: <
1434
+ const States extends Machine.StateSchemas,
1435
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
1436
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
1437
+ const Input extends Schema.Top = typeof Schema.Void,
1438
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
1439
+ E = never,
1440
+ R = never,
1441
+ InitialE = never,
1442
+ InitialR = never,
1443
+ FinalStates extends Machine.StateIdentifier<States> = never,
1444
+ Output = never,
1445
+ OutputStates extends Machine.StateIdentifier<States> = never,
1446
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
1447
+ >(
1448
+ machine:
1449
+ & Machine<
1450
+ States,
1451
+ Events,
1452
+ Input,
1453
+ UnhandledStates,
1454
+ E,
1455
+ R,
1456
+ InitialE,
1457
+ InitialR,
1458
+ FinalStates,
1459
+ Output,
1460
+ Emits,
1461
+ OutputStates,
1462
+ InputEvents
1463
+ >
1464
+ & EnsureExecutable<States, UnhandledStates, OutputStates>,
1465
+ state: Machine.Snapshot<States>,
1466
+ event: Machine.EventOf<InputEvents>
1467
+ ) => Effect.Effect<
1468
+ & {
1469
+ readonly next: Machine.Snapshot<States>
1470
+ readonly commands: ReadonlyArray<Command>
1471
+ readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
1472
+ readonly microsteps: ReadonlyArray<{
1473
+ readonly next: Machine.Snapshot<States>
1474
+ readonly event: Machine.EventOf<Events> | InitialEventModel
1475
+ readonly transitions: ReadonlyArray<
1476
+ Machine.RetainedTransition<
1477
+ Machine.StateNodeIdentifier<States>,
1478
+ Machine.TagOf<Events[number]>,
1479
+ Machine.StateNodeIdentifier<States>
1480
+ >
1481
+ >
1482
+ readonly commands: ReadonlyArray<Command>
1483
+ readonly raisedEvents: ReadonlyArray<Machine.EventOf<Events>>
1484
+ readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
1485
+ readonly exitPaths: ReadonlyArray<string>
1486
+ readonly entryPaths: ReadonlyArray<string>
1487
+ readonly changed: boolean
1488
+ }>
1489
+ }
1490
+ & (
1491
+ | {
1492
+ readonly done: true
1493
+ readonly output: Output
1494
+ }
1495
+ | {
1496
+ readonly done: false
1497
+ readonly output: undefined
1498
+ }
1499
+ ),
1500
+ E | InfiniteTransitionError | MachineSchemaDecodeError,
1501
+ never
1502
+ > = internalPlanner.plan as any
1503
+
1504
+ export const effect = <Output, Error = never, Requirements = never>(
1505
+ effect: Effect.Effect<Output, Error, Requirements>
1506
+ ): Logic<void, never, Error, Requirements, Output> => ({
1507
+ initial: () => Effect.void,
1508
+ run: () => effect
1509
+ })
1510
+
1511
+ export const logic = <
1512
+ State,
1513
+ Event = never,
1514
+ Output = void,
1515
+ Error = never,
1516
+ Requirements = never,
1517
+ InitialError = never,
1518
+ InitialRequirements = never
1519
+ >(
1520
+ options: {
1521
+ readonly initial:
1522
+ | State
1523
+ | ((
1524
+ scope: Logic.Scope<Event>
1525
+ ) => Effect.Effect<State, InitialError, InitialRequirements>)
1526
+ readonly run: (
1527
+ context: Logic.Context<State, Event>
1528
+ ) => Effect.Effect<Output, Error, Requirements>
1529
+ }
1530
+ ): Logic<State, Event, Error, Requirements | InitialRequirements, Output, InitialError> => ({
1531
+ initial: (scope) =>
1532
+ typeof options.initial === "function"
1533
+ ? (options.initial as (
1534
+ scope: Logic.Scope<Event>
1535
+ ) => Effect.Effect<State, InitialError, InitialRequirements>)(scope)
1536
+ : Effect.succeed(options.initial),
1537
+ run: options.run
1538
+ })
1539
+
1540
+ export const transition = <State, Event, Error = never, Requirements = never>(
1541
+ initial: State,
1542
+ transition: (state: State, event: Event) => Effect.Effect<State, Error, Requirements>
1543
+ ): Logic<State, Event, Error, Requirements, never> =>
1544
+ logic<State, Event, never, Error, Requirements>({
1545
+ initial,
1546
+ run: ({ receive, updateState }) =>
1547
+ receive.pipe(
1548
+ Effect.flatMap((event) => updateState((state) => transition(state, event))),
1549
+ Effect.forever
1550
+ )
1551
+ })
1552
+
1553
+ export const child = <const Id extends string, M extends Machine.Any>(
1554
+ id: Id,
1555
+ machine: M
1556
+ ): ChildMachine<Id, M> => ({
1557
+ [ChildMachineTypeId]: ChildMachineTypeId,
1558
+ id,
1559
+ machine
1560
+ })
1561
+
1562
+ export const childAddress = <Event = never>(id: string): ChildAddress<Event> => id as ChildAddress<Event>
1563
+
1564
+ export const spawn: {
1565
+ <ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError = never>(
1566
+ logic: Logic<
1567
+ ChildState,
1568
+ ChildEvent,
1569
+ ChildError,
1570
+ ChildRequirements,
1571
+ ChildOutput,
1572
+ ChildInitialError
1573
+ >
1574
+ ): SpawnResult<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, never, ChildInitialError>
1575
+ <
1576
+ ChildState,
1577
+ ChildEvent,
1578
+ ChildError,
1579
+ ChildRequirements,
1580
+ ChildOutput,
1581
+ Options extends SpawnOptions,
1582
+ ChildInitialError = never
1583
+ >(
1584
+ logic: Logic<
1585
+ ChildState,
1586
+ ChildEvent,
1587
+ ChildError,
1588
+ ChildRequirements,
1589
+ ChildOutput,
1590
+ ChildInitialError
1591
+ >,
1592
+ options: Options & ChildAddress.OptionsCompatibility<Options, ChildEvent>
1593
+ ): SpawnResult<
1594
+ ChildState,
1595
+ ChildEvent,
1596
+ ChildError,
1597
+ ChildRequirements,
1598
+ ChildOutput,
1599
+ SpawnError<Options>,
1600
+ ChildInitialError
1601
+ >
1602
+ } = ((
1603
+ logic: Logic<any, any, any, any, any, any>,
1604
+ options?: SpawnOptions
1605
+ ) =>
1606
+ Effect.flatMap(
1607
+ internalRuntime.MachineRuntime,
1608
+ (runtime) => options === undefined ? runtime.spawn(logic) : (runtime.spawn as any)(logic, options)
1609
+ )) as any
1610
+
1611
+ export const sendTo: {
1612
+ <Child extends ChildMachine.Any>(
1613
+ child: Child,
1614
+ event: ChildMachine.Event<Child>
1615
+ ): Effect.Effect<void, StoppedError, MachineRuntimeRequirement>
1616
+ <Address extends ChildAddress<never>>(
1617
+ id: Address,
1618
+ event: ChildAddress.Event<Address>
1619
+ ): Effect.Effect<void, StoppedError, MachineRuntimeRequirement>
1620
+ } = ((child: string | ChildMachine.Any, event: unknown) =>
1621
+ Effect.flatMap(
1622
+ internalRuntime.MachineRuntime,
1623
+ (runtime) => runtime.sendTo(child, event)
1624
+ )) as any
1625
+
1626
+ export const stopChild: {
1627
+ <Event>(child: ChildAddress<Event>): Effect.Effect<void, never, MachineRuntimeRequirement>
1628
+ <Child extends ChildMachine.Any>(child: Child): Effect.Effect<void, never, MachineRuntimeRequirement>
1629
+ } = ((child: string | ChildMachine.Any) =>
1630
+ Effect.flatMap(
1631
+ internalRuntime.MachineRuntime,
1632
+ (runtime) => runtime.stopChild(child)
1633
+ )) as any
1634
+
1635
+ export const watch = <State, Event, Error = never, Output = never>(
1636
+ ref: MachineRef<State, Event, Error, Output>
1637
+ ): Stream.Stream<RuntimeOutcome<State, Error, Output>> => internalRuntime.watch(ref)
1638
+
1639
+ export const start: <
1640
+ const States extends Machine.StateSchemas,
1641
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
1642
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
1643
+ const Input extends Schema.Top = typeof Schema.Void,
1644
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
1645
+ E = never,
1646
+ R = never,
1647
+ InitialE = never,
1648
+ InitialR = never,
1649
+ FinalStates extends Machine.StateIdentifier<States> = never,
1650
+ Output = never,
1651
+ OutputStates extends Machine.StateIdentifier<States> = never,
1652
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
1653
+ >(
1654
+ machine:
1655
+ & Machine<
1656
+ States,
1657
+ Events,
1658
+ Input,
1659
+ UnhandledStates,
1660
+ E,
1661
+ R,
1662
+ InitialE,
1663
+ InitialR,
1664
+ FinalStates,
1665
+ Output,
1666
+ Emits,
1667
+ OutputStates,
1668
+ InputEvents
1669
+ >
1670
+ & EnsureExecutable<States, UnhandledStates, OutputStates>,
1671
+ ...args: [...Machine.InputArgs<Input>]
1672
+ ) => Effect.Effect<
1673
+ MachineRef<
1674
+ Machine.Snapshot<States>,
1675
+ Machine.EventOf<InputEvents>,
1676
+ | E
1677
+ | ActionError<R>
1678
+ | InfiniteTransitionError
1679
+ | MachineSchemaDecodeError
1680
+ | StoppedError,
1681
+ Output
1682
+ >,
1683
+ | InitialE
1684
+ | E
1685
+ | ActionError<InitialR | R>
1686
+ | InfiniteTransitionError
1687
+ | MachineSchemaDecodeError
1688
+ | StartupError
1689
+ | StoppedError,
1690
+ ExcludeCompatibleRuntime<
1691
+ ExecutionServices<InitialR | R>,
1692
+ Machine.EventOf<Events>,
1693
+ Machine.EmitOf<Emits>
1694
+ >
1695
+ > = internalProcess.start as any
1696
+
1697
+ export const resume: <
1698
+ const States extends Machine.StateSchemas,
1699
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
1700
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
1701
+ const Input extends Schema.Top = typeof Schema.Void,
1702
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
1703
+ E = never,
1704
+ R = never,
1705
+ InitialE = never,
1706
+ InitialR = never,
1707
+ FinalStates extends Machine.StateIdentifier<States> = never,
1708
+ Output = never,
1709
+ OutputStates extends Machine.StateIdentifier<States> = never,
1710
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
1711
+ >(
1712
+ machine:
1713
+ & Machine<
1714
+ States,
1715
+ Events,
1716
+ Input,
1717
+ UnhandledStates,
1718
+ E,
1719
+ R,
1720
+ InitialE,
1721
+ InitialR,
1722
+ FinalStates,
1723
+ Output,
1724
+ Emits,
1725
+ OutputStates,
1726
+ InputEvents
1727
+ >
1728
+ & EnsureExecutable<States, UnhandledStates, OutputStates>,
1729
+ snapshot: Machine.Snapshot<States>
1730
+ ) => Effect.Effect<
1731
+ MachineRef<
1732
+ Machine.Snapshot<States>,
1733
+ Machine.EventOf<InputEvents>,
1734
+ | E
1735
+ | ActionError<R>
1736
+ | InfiniteTransitionError
1737
+ | MachineSchemaDecodeError
1738
+ | StoppedError,
1739
+ Output
1740
+ >,
1741
+ MachineSchemaDecodeError,
1742
+ ExcludeCompatibleRuntime<
1743
+ ExecutionServices<R>,
1744
+ Machine.EventOf<Events>,
1745
+ Machine.EmitOf<Emits>
1746
+ >
1747
+ > = internalProcess.resume as any