@typeonce/effect-machine 0.19.0 → 0.20.0

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 (32) hide show
  1. package/README.md +50 -15
  2. package/dist/Machine.d.ts +93 -2
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +11 -0
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/atom.d.ts.map +1 -1
  7. package/dist/internal/machine/atom.js +18 -4
  8. package/dist/internal/machine/atom.js.map +1 -1
  9. package/dist/internal/machine/invocation.d.ts.map +1 -1
  10. package/dist/internal/machine/invocation.js +7 -0
  11. package/dist/internal/machine/invocation.js.map +1 -1
  12. package/dist/internal/machine/machine.d.ts +1 -0
  13. package/dist/internal/machine/machine.d.ts.map +1 -1
  14. package/dist/internal/machine/machine.js +11 -4
  15. package/dist/internal/machine/machine.js.map +1 -1
  16. package/dist/internal/machine/runtime.d.ts +4 -3
  17. package/dist/internal/machine/runtime.d.ts.map +1 -1
  18. package/dist/internal/machine/runtime.js +12 -1
  19. package/dist/internal/machine/runtime.js.map +1 -1
  20. package/dist/unstable/reactivity/AtomMachine.d.ts +10 -8
  21. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  22. package/dist/unstable/reactivity/AtomMachine.js +2 -2
  23. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  24. package/docs/agent-guide.md +302 -1449
  25. package/docs/effect-atom-react.md +230 -0
  26. package/package.json +4 -4
  27. package/src/Machine.ts +141 -2
  28. package/src/internal/machine/atom.ts +26 -18
  29. package/src/internal/machine/invocation.ts +13 -1
  30. package/src/internal/machine/machine.ts +18 -6
  31. package/src/internal/machine/runtime.ts +48 -19
  32. package/src/unstable/reactivity/AtomMachine.ts +10 -8
@@ -18,9 +18,10 @@ import * as Scope from "effect/Scope"
18
18
  import * as Stream from "effect/Stream"
19
19
  import * as SynchronizedRef from "effect/SynchronizedRef"
20
20
  import type * as Take from "effect/Take"
21
- import type { Inspection, Machine as MachineDefinition, MachineTarget } from "../../Machine.js"
21
+ import type { ChildMachine, Inspection, Machine as MachineDefinition, MachineTarget } from "../../Machine.js"
22
22
  import { ChildAlreadyExistsError, StoppedError } from "./errors.js"
23
23
  import * as InspectionRuntime from "./inspectionRuntime.js"
24
+ import { ChildMachineLogicTypeId } from "./symbols.js"
24
25
 
25
26
  type ChildDescriptor = {
26
27
  readonly id: string
@@ -376,7 +377,7 @@ const sendMachineTarget = (
376
377
  export interface ProcessScope<Event> {
377
378
  readonly self: ProcessAddress<Event>
378
379
  readonly parent: ProcessAddress<unknown> | undefined
379
- readonly spawn: ProcessSpawn
380
+ readonly spawn: ProcessSpawn<Event>
380
381
  readonly sendParent: (event: unknown) => Effect.Effect<void, StoppedError>
381
382
  readonly emit: (event: unknown) => Effect.Effect<void>
382
383
  readonly sendTo: {
@@ -552,7 +553,15 @@ export interface ProcessLogic<
552
553
  run(context: ProcessContext<State, Event>): Effect.Effect<Output, Error, Requirements>
553
554
  }
554
555
 
555
- export interface ProcessSpawn {
556
+ export interface ProcessSpawn<OwnerEvent = unknown> {
557
+ <const Child extends ChildMachine.Any>(
558
+ child: Child & ChildMachine.Executable<Child> & ChildMachine.ParentCompatibility<Child, OwnerEvent>,
559
+ ...options: ChildMachine.SpawnArgs<Child>
560
+ ): Effect.Effect<
561
+ ChildMachine.Ref<Child>,
562
+ ChildAlreadyExistsError | ChildMachine.StartError<Child>,
563
+ ChildMachine.StartRequirements<Child>
564
+ >
556
565
  <ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError = never>(
557
566
  logic: ProcessLogic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>
558
567
  ): Effect.Effect<
@@ -1207,6 +1216,14 @@ const makeChildRuntimeSync = (
1207
1216
  })
1208
1217
  }
1209
1218
 
1219
+ function spawn<const Child extends ChildMachine.Any>(
1220
+ child: Child & ChildMachine.Executable<Child> & ChildMachine.ParentCompatibility<Child, unknown>,
1221
+ ...options: ChildMachine.SpawnArgs<Child>
1222
+ ): Effect.Effect<
1223
+ ChildMachine.Ref<Child>,
1224
+ ChildAlreadyExistsError | ChildMachine.StartError<Child>,
1225
+ ChildMachine.StartRequirements<Child>
1226
+ >
1210
1227
  function spawn<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError = never>(
1211
1228
  logic: ProcessLogic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>
1212
1229
  ): Effect.Effect<
@@ -1232,32 +1249,44 @@ const makeChildRuntimeSync = (
1232
1249
  ChildAlreadyExistsError | ChildInitialError,
1233
1250
  Exclude<ChildRequirements, Scope.Scope>
1234
1251
  >
1235
- function spawn<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError = never>(
1236
- logic: ProcessLogic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>,
1237
- spawnOptions?: {
1252
+ function spawn(
1253
+ logicOrChild: ProcessLogic<any, any, any, any, any, any> | ChildMachine.Any,
1254
+ options?: {
1238
1255
  readonly id: string
1239
1256
  readonly descriptor?: ChildDescriptor
1240
1257
  readonly onOutcome?: (
1241
- outcome: RuntimeOutcome<ChildState, ChildError, ChildOutput>
1258
+ outcome: RuntimeOutcome<any, any, any>
1242
1259
  ) => Effect.Effect<void>
1243
1260
  readonly [activeSnapshotObserver]?: (
1244
- snapshot: Extract<RuntimeSnapshot<ChildState, ChildError, ChildOutput>, { readonly status: "active" }>
1261
+ snapshot: Extract<RuntimeSnapshot<any, any, any>, { readonly status: "active" }>
1245
1262
  ) => Effect.Effect<void>
1246
1263
  readonly [sendParentOverride]?: (event: unknown) => Effect.Effect<void, StoppedError>
1247
- }
1248
- ): Effect.Effect<
1249
- MachineRef<ChildState, ChildEvent, ChildError, ChildOutput>,
1250
- ChildAlreadyExistsError | ChildInitialError,
1251
- Exclude<ChildRequirements, Scope.Scope>
1252
- > {
1264
+ } | { readonly input?: unknown }
1265
+ ): Effect.Effect<MachineRef<any, any, any, any>, any, any> {
1266
+ const descriptor = typeof logicOrChild === "object" && logicOrChild !== null &&
1267
+ ChildMachineLogicTypeId in logicOrChild
1268
+ ? logicOrChild as ChildMachine.Any
1269
+ : undefined
1270
+ const logic = descriptor === undefined
1271
+ ? logicOrChild as ProcessLogic<any, any, any, any, any, any>
1272
+ : descriptor[ChildMachineLogicTypeId](
1273
+ (options as { readonly input?: unknown } | undefined)?.input
1274
+ ) as unknown as ProcessLogic<any, any, any, any, any, any>
1275
+ const spawnOptions = descriptor === undefined
1276
+ ? options as {
1277
+ readonly id: string
1278
+ readonly descriptor?: ChildDescriptor
1279
+ readonly onOutcome?: (outcome: RuntimeOutcome<any, any, any>) => Effect.Effect<void>
1280
+ readonly [activeSnapshotObserver]?: (
1281
+ snapshot: Extract<RuntimeSnapshot<any, any, any>, { readonly status: "active" }>
1282
+ ) => Effect.Effect<void>
1283
+ readonly [sendParentOverride]?: (event: unknown) => Effect.Effect<void, StoppedError>
1284
+ } | undefined
1285
+ : { id: descriptor.id, descriptor }
1253
1286
  const token = Symbol()
1254
1287
  const key = spawnOptions?.id ?? token
1255
1288
  let startedChild: MachineRef<any, any, any, any> | undefined
1256
- return Effect.suspend((): Effect.Effect<
1257
- MachineRef<ChildState, ChildEvent, ChildError, ChildOutput>,
1258
- ChildAlreadyExistsError | ChildInitialError,
1259
- Exclude<ChildRequirements, Scope.Scope>
1260
- > => {
1289
+ return Effect.suspend(() => {
1261
1290
  if (registry.closed) {
1262
1291
  return Effect.interrupt
1263
1292
  }
@@ -148,9 +148,9 @@ export interface MachineAtom<State, Event, Error = never, Output = never, StartE
148
148
  readonly stop: Atom.Writable<AsyncResult.AsyncResult<void, StartError | NotReadyError>, void>
149
149
 
150
150
  /**
151
- * Creates a reactive bridge for a directly invoked child machine.
152
- * Reusing the same descriptor returns the same live bridge while it remains
153
- * referenced.
151
+ * Creates a reactive bridge for a directly owned child machine.
152
+ * Descriptors with the same id and machine definition return the same live
153
+ * bridge while it remains referenced.
154
154
  *
155
155
  * @since 0.4.0
156
156
  */
@@ -206,7 +206,8 @@ export const childEmissions: <Child extends Machine.ChildMachine.Any, StartError
206
206
  > = internal.childEmissions
207
207
 
208
208
  /**
209
- * Reactive access to one invoked child machine selected by its descriptor.
209
+ * Reactive access to one directly owned child machine selected by its
210
+ * descriptor.
210
211
  *
211
212
  * **Details**
212
213
  *
@@ -295,8 +296,9 @@ export interface ChildMachineAtom<Child extends Machine.ChildMachine.Any, StartE
295
296
  void
296
297
  >
297
298
  /**
298
- * Creates a reactive bridge for a directly owned nested child. Reusing the
299
- * same descriptor returns the same live bridge while it remains referenced.
299
+ * Creates a reactive bridge for a directly owned nested child. Descriptors
300
+ * with the same id and machine definition return the same live bridge while
301
+ * it remains referenced.
300
302
  *
301
303
  * @since 0.4.0
302
304
  */
@@ -418,7 +420,7 @@ export const selectSnapshot: <
418
420
  > = internal.selectSnapshot
419
421
 
420
422
  /**
421
- * Selects the typed value for an active state path in an invoked child.
423
+ * Selects the typed value for an active state path in a directly owned child.
422
424
  *
423
425
  * Valid paths and their selected value types are inferred from the child
424
426
  * bridge. An inactive child produces `Option.none()`. Keep the returned atom
@@ -513,7 +515,7 @@ export const matches: <
513
515
  ) => Atom.Atom<AsyncResult.AsyncResult<boolean, StartError | Error>> = internal.matches
514
516
 
515
517
  /**
516
- * Returns whether a state path is active in an invoked child.
518
+ * Returns whether a state path is active in a directly owned child.
517
519
  *
518
520
  * Valid paths are inferred from the child bridge snapshot.
519
521
  * An inactive child produces `false`. Keep the returned atom stable when