@@ -35,15 +35,13 @@ currently coupled to the exact Effect peer version listed in its `package.json`.
3535Use this order so inference has all schemas available when handlers are
3636declared:
3737
38- 1 . Domain schemas used by state and event fields.
39- 2 . Tagged schemas for states that own data.
40- 3 . Tagged public-event, internal-event, parent-event, and emitted-event schemas.
41- 4 . ` Machine.defineStates ` .
42- 5 . ` Machine.make ` , including input, ` events ` , ` internalEvents ` , ` parentEvents ` ,
43- ` emittedEvents ` , and the initial function.
44- 6 . One or more ` .handle(...) ` calls.
45- 7 . Child descriptors.
46- 8 . Runtime, Atom, or Cluster adapters.
38+ 1 . Domain schemas used by state, and by event fields when they are shared.
39+ 2 . ` Machine.defineStates ` , using a tagged state union and ` .cases ` when state
40+ schemas need to be reused.
41+ 3 . ` Machine.events ` , ` Machine.internalEvents ` , ` Machine.emittedEvents ` , and
42+ ` parentEvents ` ; pass ` Schema.TaggedUnion({...}) ` or tagged classes directly.
43+ 4 . ` Machine.make({...}).handle({...}) ` .
44+ 5 . Child descriptors, then runtime, Atom, or Cluster adapters.
4745
4846` Schema.TaggedUnion ` avoids one class declaration per case:
4947
@@ -54,22 +52,23 @@ const State = Schema.TaggedUnion({
5452 Failed: { message: Schema .String }
5553})
5654
57- const Event = Schema .TaggedUnion ({
58- Save: {}
59- })
60-
61- const InternalEvent = Schema .TaggedUnion ({
62- Saved: { id: Schema .String },
63- SaveFailed: { message: Schema .String }
64- })
65-
6655const States = Machine .defineStates (State .cases )
67- const Events = Machine .events (Event )
68- const InternalEvents = Machine .internalEvents (InternalEvent )
56+ export const Event = Machine .events (
57+ Schema .TaggedUnion ({
58+ Save: {}
59+ })
60+ )
61+ export const Internal = Machine .internalEvents (
62+ Schema .TaggedUnion ({
63+ Saved: { id: Schema .String },
64+ SaveFailed: { message: Schema .String }
65+ })
66+ )
6967```
7068
71- Pass these descriptors to ` Machine.make ` and export ` Events ` instead of the raw
72- event schema. Construct new state values through the target or initial
69+ Pass these descriptors to ` Machine.make ` ; the event descriptor is the public
70+ handle, so do not introduce a tagged-union binding used only by an event helper.
71+ Construct new state values through the target or initial
7372builder's ` .from(...) ` method. Both event constructors and state ` .from(...) `
7473defer schema construction until planning, so validation failures remain typed
7574machine errors. Use
@@ -121,9 +120,9 @@ its extra control is required:
121120- Use one invocation object: ` effect ` for one-shot work, ` after ` for a timer,
122121 ` logic ` for reusable process logic, and ` child ` for a complete child
123122 statechart. ` Machine.invoke({...}) ` preserves owner state and source channels
124- across sibling lifecycle handlers. Use ` definition.invoke({ ...}) ` when a
125- callback uses ` self ` or ` parent ` ; the bound constructor preserves the
126- definition's exact public input and ` parentEvents ` protocols .
123+ across sibling lifecycle handlers. Inside ` .handle( ...) ` , ` self ` and ` parent `
124+ use the owning definition's exact public input and ` parentEvents ` protocols.
125+ The bound ` definition.invoke({...}) ` form is equivalent, not required .
127126- Use ` Machine.child(id, machine) ` for a complete statechart descriptor and
128127 ` Machine.childAddress<Event>(id) ` for a low-level process address. A logic
129128 invocation is addressable only when ` Machine.invoke ` receives that
@@ -764,8 +763,17 @@ an event for the parent. Both operations validate their schemas.
764763union handled inside the statechart:
765764
766765``` ts
767- const Events = Machine .events (Event )
768- const InternalEvents = Machine .internalEvents (InternalEvent )
766+ const Events = Machine .events (
767+ Schema .TaggedUnion ({
768+ Save: {}
769+ })
770+ )
771+ const InternalEvents = Machine .internalEvents (
772+ Schema .TaggedUnion ({
773+ Saved: { id: Schema .String },
774+ SaveFailed: { message: Schema .String }
775+ })
776+ )
769777
770778const definition = Machine .make ({
771779 states: States .states ,
@@ -871,29 +879,30 @@ invoke: Machine.invoke({
871879})
872880` ` `
873881
874- The standalone constructor cannot know the owning machine's input protocols,
875- so its ` self ` and ` parent ` references are non-sendable. When a source sends
876- through either reference, use the owning definition's bound constructor :
882+ Inside ` . handle (...) ` , the constructor receives the owning machine's public
883+ input and ` parentEvents ` protocols contextually. Sources and lifecycle handlers
884+ can send through ` self ` and ` parent ` without naming the definition :
877885
878886` ` ` ts
879- const definition = Machine .make ({
887+ const machine = Machine .make ({
880888 events: Commands ,
881889 internalEvents: InternalEvents ,
882890 parentEvents: ParentEvents ,
883891 // ...
884- })
885-
886- const machine = definition .handle ({
892+ }).handle ({
887893 Saving: {
888- invoke: definition .invoke ({
894+ invoke: Machine .invoke ({
889895 id: " notify-parent" ,
890- effect : ({ parent }) =>
891- parent === undefined
892- ? Effect .void
893- : parent .send (ParentEvents .SaveStarted ()),
896+ effect : () => saveDocument ,
894897 onDone: Machine .transition ({
895898 target : (to ) => to .none (),
896- resolve : () => undefined
899+ resolve : ({ parent , self }, enqueue ) => {
900+ enqueue .sendTo (self , Commands .Save ())
901+ if (parent !== undefined ) {
902+ enqueue .sendTo (parent , ParentEvents .ChildFinished ({ id: " job-1" }))
903+ }
904+ return undefined
905+ }
897906 }),
898907 onFailure: Machine .transition ({
899908 target : (to ) => to .none (),
@@ -904,8 +913,9 @@ const machine = definition.handle({
904913})
905914```
906915
907- A direct ` invoke: { ... } ` object remains available when lifecycle handlers do
908- not need source-derived context.
916+ The machine-bound ` definition.invoke(...) ` form remains equivalent when the
917+ definition is already named. A direct ` invoke: { ... } ` object remains available
918+ when lifecycle handlers do not need source-derived context.
909919
910920A cancellable timer uses the same object:
911921
0 commit comments