@@ -58,6 +58,12 @@ type MachineRequirements<InitialR, R, Events, Emits> = ExcludeCompatibleMachineR
5858 Emits
5959>
6060
61+ type MachineResumeRequirements < R , Events , Emits > = ExcludeCompatibleMachineRuntime <
62+ Machine . ExecutionServices < R > ,
63+ Events ,
64+ Emits
65+ >
66+
6167type MachineRuntimeError < E , R > =
6268 | E
6369 | Machine . ActionError < R >
@@ -75,6 +81,17 @@ type MachineStartError<InitialE, E, InitialR, R, RuntimeError = never> =
7581 | Machine . StoppedError
7682 | RuntimeError
7783
84+ const runMachineAtomEffect = < State , Event , Error , Output , StartError , Requirements > (
85+ get : Atom . AtomContext ,
86+ start : Effect . Effect < Machine . MachineRef < State , Event , Error , Output > , StartError , Requirements >
87+ ) : Effect . Effect < never , StartError , Requirements > =>
88+ Effect . scoped (
89+ Effect . acquireRelease ( start , ( ref ) => ref . stop ) . pipe (
90+ Effect . tap ( ( ref ) => Effect . sync ( ( ) => get . setSelf ( AsyncResult . success ( ref ) ) ) ) ,
91+ Effect . flatMap ( ( ) => Effect . never )
92+ )
93+ )
94+
7895const startMachineAtomEffect = <
7996 const States extends Machine . Machine . StateSchemas ,
8097 const Events extends ReadonlyArray < Machine . Machine . TaggedSchema > ,
@@ -118,16 +135,13 @@ const startMachineAtomEffect = <
118135 > ,
119136 MachineStartError < InitialE , E , InitialR , R > ,
120137 MachineRequirements < InitialR , R , Machine . Machine . EventOf < Events > , Machine . Machine . EmitOf < Emits > >
121- > =>
122- Effect . scoped (
123- Effect . acquireRelease (
124- Machine . start ( machine , ...args ) ,
125- ( ref ) => ref . stop
126- ) . pipe (
127- Effect . tap ( ( ref ) => Effect . sync ( ( ) => get . setSelf ( AsyncResult . success ( ref ) ) ) ) ,
128- Effect . flatMap ( ( ) => Effect . never )
129- )
130- )
138+ > => runMachineAtomEffect ( get , Machine . start ( machine , ...args ) )
139+
140+ const resumeMachineAtomEffect = (
141+ get : Atom . AtomContext ,
142+ machine : Machine . Machine . Any ,
143+ snapshot : Machine . Machine . Snapshot < any >
144+ ) => runMachineAtomEffect ( get , Machine . resume ( machine as any , snapshot as any ) )
131145
132146/**
133147 * Atoms backed by one running machine instance in an `AtomRegistry`.
@@ -781,12 +795,37 @@ type EnsureBoundRequirements<Services, M extends Machine.Machine.Any> = IsAny<Ma
781795 readonly [ BoundRequirementsTypeId ] : MissingBoundRequirements < Services , M >
782796 }
783797
798+ type MachineResumeRequirementsOf < M extends Machine . Machine . Any > = MachineResumeRequirements <
799+ Machine . Machine . Services < M > ,
800+ Machine . Machine . Event < M > ,
801+ Machine . Machine . Emit < M >
802+ >
803+
804+ type MissingBoundResumeRequirements < Services , M extends Machine . Machine . Any > = Exclude <
805+ ExternalRequirements < MachineResumeRequirementsOf < M > > ,
806+ Services
807+ >
808+
809+ type EnsureBoundResumeRequirements < Services , M extends Machine . Machine . Any > =
810+ IsAny < MachineResumeRequirementsOf < M > > extends true ? {
811+ readonly [ BoundRequirementsTypeId ] : MachineResumeRequirementsOf < M >
812+ }
813+ : [ MissingBoundResumeRequirements < Services , M > ] extends [ never ] ? unknown
814+ : {
815+ readonly [ BoundRequirementsTypeId ] : MissingBoundResumeRequirements < Services , M >
816+ }
817+
784818type EnsureMachineOutputImplementations < M extends Machine . Machine . Any > = IsAny < Machine . Machine . States < M > > extends true ?
785819 {
786820 readonly "~effect/reactivity/AtomMachine/ConcreteMachineRequired" : M
787821 }
788822 : Machine . Machine . EnsureOutputImplementations < Machine . Machine . States < M > , Machine . Machine . OutputStates < M > >
789823
824+ type EnsureMachineHistoryImplementations < M extends Machine . Machine . Any > = Machine . Machine . EnsureHistoryImplementations <
825+ Machine . Machine . States < M > ,
826+ Machine . Machine . UnhandledStates < M >
827+ >
828+
790829type MachineInputArgsOf < M extends Machine . Machine . Any > = [
791830 ...Machine . Machine . InputArgs < Machine . Machine . Input < M > >
792831]
@@ -805,6 +844,14 @@ type MachineAtomOf<M extends Machine.Machine.Any, RuntimeError> = MachineAtom<
805844 >
806845>
807846
847+ type ResumedMachineAtomOf < M extends Machine . Machine . Any , RuntimeError > = MachineAtom <
848+ Machine . Machine . Snapshot < Machine . Machine . States < M > > ,
849+ Machine . Machine . InputEvent < M > ,
850+ MachineRuntimeError < Machine . Machine . Error < M > , Machine . Machine . Services < M > > ,
851+ Machine . Machine . Output < M > ,
852+ Machine . MachineSchemaDecodeError | RuntimeError
853+ >
854+
808855/**
809856 * An `AtomMachine` factory with one owned Effect runtime.
810857 *
@@ -827,6 +874,16 @@ export interface Bound<Services, RuntimeError = never> {
827874 & EnsureMachineOutputImplementations < NoInfer < M > > ,
828875 ...args : MachineInputArgsOf < M >
829876 ) => MachineAtomOf < M , RuntimeError >
877+
878+ /** Creates a lazy bridge from a decoded logical snapshot. */
879+ readonly resume : < M extends Machine . Machine . Any > (
880+ machine :
881+ & M
882+ & EnsureBoundResumeRequirements < Services , NoInfer < M > >
883+ & EnsureMachineOutputImplementations < NoInfer < M > >
884+ & EnsureMachineHistoryImplementations < NoInfer < M > > ,
885+ snapshot : Machine . Machine . Snapshot < Machine . Machine . States < M > >
886+ ) => ResumedMachineAtomOf < M , RuntimeError >
830887}
831888
832889/**
@@ -892,6 +949,30 @@ export const make: {
892949 return makeFromRefAtom ( ref as any )
893950} ) as any
894951
952+ /**
953+ * Creates a lazy atom bridge from a decoded logical snapshot.
954+ *
955+ * The bridge owns one freshly resumed runtime per `AtomRegistry`, with the same
956+ * lazy start and disposal semantics as {@link make}. The machine initial
957+ * function and its input, errors, and services are not involved.
958+ *
959+ * @category constructors
960+ * @since 4.0.0
961+ */
962+ export const resume : {
963+ < M extends Machine . Machine . Any > (
964+ machine :
965+ & M
966+ & EnsureNoExternalRequirements < MachineResumeRequirementsOf < NoInfer < M > > >
967+ & EnsureMachineOutputImplementations < NoInfer < M > >
968+ & EnsureMachineHistoryImplementations < NoInfer < M > > ,
969+ snapshot : Machine . Machine . Snapshot < Machine . Machine . States < M > >
970+ ) : ResumedMachineAtomOf < M , never >
971+ } = ( ( machine : Machine . Machine . Any , snapshot : Machine . Machine . Snapshot < any > ) => {
972+ const ref = Atom . make ( ( get ) => resumeMachineAtomEffect ( get , machine , snapshot ) )
973+ return makeFromRefAtom ( ref as any )
974+ } ) as any
975+
895976const makeWithRuntime = (
896977 runtime : Atom . AtomRuntime < any , any > ,
897978 machine : Machine . Machine . Any ,
@@ -901,6 +982,15 @@ const makeWithRuntime = (
901982 return makeFromRefAtom ( ref as any )
902983}
903984
985+ const resumeWithRuntime = (
986+ runtime : Atom . AtomRuntime < any , any > ,
987+ machine : Machine . Machine . Any ,
988+ snapshot : Machine . Machine . Snapshot < any >
989+ ) : MachineAtom < any , any , any , any , any > => {
990+ const ref = runtime . atom ( ( get ) => resumeMachineAtomEffect ( get , machine , snapshot ) )
991+ return makeFromRefAtom ( ref as any )
992+ }
993+
904994/**
905995 * Creates an `AtomMachine` factory that owns a shared Effect runtime.
906996 *
@@ -919,5 +1009,8 @@ export const bind = <Services, RuntimeError>(
9191009 makeWithRuntime ( runtime , machine , args ) ) as Bound <
9201010 Services ,
9211011 RuntimeError
922- > [ "make" ]
1012+ > [ "make" ] ,
1013+ resume :
1014+ ( ( machine : Machine . Machine . Any , snapshot : Machine . Machine . Snapshot < any > ) =>
1015+ resumeWithRuntime ( runtime , machine , snapshot ) ) as Bound < Services , RuntimeError > [ "resume" ]
9231016} )
0 commit comments