@@ -51,11 +51,13 @@ const InternalEvent = Schema.TaggedUnion({
5151const States = Machine .defineStates (State .cases )
5252```
5353
54- Construct event values with ` Event.cases.Save.make({}) ` . Construct new state
55- values through the target or initial builder's ` .from(...) ` method so schema
56- construction runs inside planning. Pass a state directly only when it is
57- already decoded. Use ` Schema.TaggedClass ` when a case needs class methods or
58- nominal class identity; ` .from(...) ` preserves that identity.
54+ After ` Machine.make ` , derive public constructors with ` Machine.events(machine) `
55+ and internal constructors with ` Machine.internalEvents(machine) ` . Construct new
56+ state values through the target or initial builder's ` .from(...) ` method. Both
57+ event constructors and state ` .from(...) ` defer schema construction until
58+ planning, so validation failures remain typed machine errors. Use
59+ ` Schema.TaggedClass ` when a case needs class methods or nominal class identity;
60+ the deferred constructors preserve that identity after decoding.
5961
6062## Hard invariants
6163
@@ -499,41 +501,51 @@ an event for the parent. Both operations validate their schemas.
499501union handled inside the statechart:
500502
501503``` ts
502- const machine = Machine .make ({
504+ const definition = Machine .make ({
503505 states: States .states ,
504- events: [Event . cases . Save ],
505- internalEvents: [InternalEvent . cases . Saved , InternalEvent . cases . SaveFailed ],
506+ events: [Event ],
507+ internalEvents: [InternalEvent ],
506508 initial : () => States .initial .Idle .from ()
507509})
510+
511+ const Events = Machine .events (definition )
512+ const InternalEvents = Machine .internalEvents (definition )
508513```
509514
510- When the same already-constructed event may be delivered repeatedly, construct
511- it once through its owning machine protocol:
515+ Use the protocol-bound constructors at every machine delivery boundary:
512516
513517``` ts
514- const save = Machine . event ( machine , Event . cases . Save )
515- yield * ref . send ( save )
518+ yield * ref . send ( Events . Save () )
519+ enqueue . raise ( InternalEvents . Saved ({ id: " entry-1 " }) )
516520```
517521
518- ` Machine.event ` runs the configured schema constructor once. That machine and
519- definitions derived from it with ` handle ` then recognize the decoded event as
520- trusted and do not decode it again. Tagged-union case schemas are recognized
521- when their union is configured. Treat the returned event as immutable. Raw
522- objects and values constructed for another machine continue through normal
523- runtime validation on every delivery.
522+ ` Machine.events ` exposes only public constructors;
523+ ` Machine.internalEvents ` exposes only machine-local constructors. Both flatten
524+ configured tagged unions and preserve tagged classes, finite discriminator
525+ unions, required inputs, and constructor defaults. A constructor returns an
526+ opaque instruction whose ` _tag ` is available for activity metadata. Its decoded
527+ fields are intentionally unavailable until the owning machine processes it.
528+
529+ Invalid constructor input fails ` Machine.plan ` or the running machine with
530+ ` MachineSchemaDecodeError ` ; creating the instruction itself never performs
531+ schema validation. ` Machine.event(machine, schema, fields?) ` remains available
532+ as an eager low-level constructor for callers that explicitly want an already
533+ decoded value and accept synchronous failure.
524534
525535Use the exported utility types when another API must preserve the boundary:
526536
527537``` ts
528- type PublicEvent = Machine .Machine .InputEvent <typeof machine >
529- type AnyHandledEvent = Machine .Machine .Event <typeof machine >
538+ type PublicEvent = Machine .Machine .InputEvent <typeof definition >
539+ type AnyHandledEvent = Machine .Machine .Event <typeof definition >
530540` ` `
531541
532- ` MachineRef .send ` , ` machineAtom .send ` , and ` Machine .plan ` use ` InputEvent ` at
533- their TypeScript boundary. Transition handlers, raised events, invoke results,
534- and mapped child events use the complete ` Event ` union. The local planner and
535- runtime intentionally share the complete decoder to support those internal
536- deliveries, so JavaScript or ` any ` can bypass the local public distinction.
542+ ` MachineRef .send ` , ` machineAtom .send ` , and ` Machine .plan ` accept decoded public
543+ events or constructions returned by ` Machine .events ` . Transition handlers
544+ receive only decoded events. Raised events, invoke results, and mapped child
545+ events additionally accept constructions from ` Machine .internalEvents ` . The
546+ local planner and runtime intentionally share the complete decoder to support
547+ those internal deliveries, so JavaScript or ` any ` can bypass the local public
548+ distinction.
537549Cluster RPC payloads are additionally decoded against the public ` events `
538550schemas at the transport boundary. Never repeat an ` _tag ` within a list or
539551across both configuration lists.
@@ -548,8 +560,8 @@ invoke: ({ state }) =>
548560 Machine .invokeEffect ({
549561 id: " save" ,
550562 effect: SaveService .save (state .draft ),
551- onSuccess: (entry ) => new Saved ({ entry }),
552- onFailure: (error ) => new SaveFailed ({ message: error .message })
563+ onSuccess: (entry ) => InternalEvents . Saved ({ entry }),
564+ onFailure: (error ) => InternalEvents . SaveFailed ({ message: error .message })
553565 })
554566` ` `
555567
@@ -566,7 +578,7 @@ recover only expected typed failures.
566578A cancellable timer uses ` Machine .after ` :
567579
568580` ` ` ts
569- invoke : Machine .after (" 3 seconds" , new ClearStatus ({} ), {
581+ invoke : Machine .after (" 3 seconds" , InternalEvents . ClearStatus (), {
570582 id: " clear-status"
571583})
572584` ` `
@@ -601,7 +613,7 @@ invoke: Machine.invokeMachine({
601613Use ` Editor ` for:
602614
603615``` ts
604- Machine .sendTo (Editor , new Reset ({} ))
616+ Machine .sendTo (Editor , EditorEvent . Reset ())
605617parentRef .child (Editor )
606618parentAtom .child (Editor )
607619```
0 commit comments