@typeonce/effect-machine 0.41.1 → 0.42.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.
- package/README.md +31 -4
- package/dist/Machine.d.ts +159 -27
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +20 -6
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/atom.d.ts +2 -7
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +132 -87
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/childRegistry.d.ts +7 -0
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/childRegistry.js +7 -0
- package/dist/internal/machine/childRegistry.js.map +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +13 -2
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts +7 -0
- package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
- package/dist/internal/machine/invocationDefinition.js +16 -0
- package/dist/internal/machine/invocationDefinition.js.map +1 -1
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +69 -6
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/plannedSnapshot.d.ts +30 -0
- package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -0
- package/dist/internal/machine/plannedSnapshot.js +103 -0
- package/dist/internal/machine/plannedSnapshot.js.map +1 -0
- package/dist/internal/machine/planner.d.ts +5 -0
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +1 -1
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +5 -4
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +60 -15
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.js +2 -2
- package/dist/internal/machine/runtimeCompiled.js.map +1 -1
- package/dist/internal/machine/runtimeGeneric.js +1 -1
- package/dist/internal/machine/runtimeGeneric.js.map +1 -1
- package/dist/internal/machine/runtimeProtocol.d.ts +2 -1
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
- package/dist/internal/machine/runtimeProtocol.js +2 -2
- package/dist/internal/machine/runtimeProtocol.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +2 -2
- package/dist/testing/MachineTest.js +2 -2
- package/dist/unstable/reactivity/AtomMachine.d.ts +82 -19
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +0 -7
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/docs/agent-guide.md +28 -5
- package/docs/effect-atom-react.md +207 -11
- package/docs/machine-review.md +18 -4
- package/package.json +1 -1
- package/src/Machine.ts +229 -32
- package/src/internal/machine/atom.ts +263 -113
- package/src/internal/machine/childRegistry.ts +9 -0
- package/src/internal/machine/invocation.ts +17 -2
- package/src/internal/machine/invocationDefinition.ts +19 -0
- package/src/internal/machine/machine.ts +65 -2
- package/src/internal/machine/plannedSnapshot.ts +113 -0
- package/src/internal/machine/planner.ts +1 -1
- package/src/internal/machine/runtime.ts +101 -28
- package/src/internal/machine/runtimeCompiled.ts +2 -2
- package/src/internal/machine/runtimeGeneric.ts +1 -1
- package/src/internal/machine/runtimeProtocol.ts +4 -2
- package/src/testing/MachineTest.ts +2 -2
- package/src/unstable/reactivity/AtomMachine.ts +90 -20
|
@@ -17,12 +17,11 @@ import * as Stream from "effect/Stream"
|
|
|
17
17
|
import type * as Machine from "../../Machine.js"
|
|
18
18
|
import type { Bound, ChildMachineAtom, MachineAtom } from "../../unstable/reactivity/AtomMachine.js"
|
|
19
19
|
import * as internalMachine from "./machine.js"
|
|
20
|
+
import * as PlannedSnapshot from "./plannedSnapshot.js"
|
|
20
21
|
import type { EnsureExecutable } from "./readiness.js"
|
|
21
22
|
import type { ExcludeCompatibleRuntime as ExcludeCompatibleMachineRuntime, IsAny } from "./requirements.js"
|
|
22
23
|
import * as Topology from "./topology.js"
|
|
23
24
|
|
|
24
|
-
export class NotReadyError extends Data.TaggedError("NotReadyError") {}
|
|
25
|
-
|
|
26
25
|
export class ChildNotActiveError extends Data.TaggedError("ChildNotActiveError")<{
|
|
27
26
|
readonly id: string
|
|
28
27
|
}> {}
|
|
@@ -142,6 +141,47 @@ const resumeMachineAtomEffect = (
|
|
|
142
141
|
snapshot: Machine.Machine.Snapshot<any>
|
|
143
142
|
) => runMachineAtomEffect(get, internalMachine.resume(machine, snapshot))
|
|
144
143
|
|
|
144
|
+
// Server reads. A server read renders the planned startup snapshot and never
|
|
145
|
+
// evaluates a bridge ref, because evaluating it starts the machine. Effect
|
|
146
|
+
// passes plain registry reads to a server value, so every exported atom carries
|
|
147
|
+
// its own server value derived from the bridge plan.
|
|
148
|
+
|
|
149
|
+
type ServerRead = <A>(atom: Atom.Atom<A>) => A
|
|
150
|
+
|
|
151
|
+
interface ServerValued<A> {
|
|
152
|
+
readonly [Atom.ServerValueTypeId]: (get: ServerRead) => A
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Effect stores the server read on the atom without exposing it in `Atom`'s
|
|
156
|
+
// type. This is the only erased boundary of the server protocol.
|
|
157
|
+
const readServerValue = <A>(atom: Atom.Atom<A>, get: ServerRead): A =>
|
|
158
|
+
Atom.ServerValueTypeId in atom ? (atom as Atom.Atom<A> & ServerValued<A>)[Atom.ServerValueTypeId](get) : get(atom)
|
|
159
|
+
|
|
160
|
+
/** A bridge plan: `undefined` when no snapshot can be planned synchronously. */
|
|
161
|
+
type ServerPlan = () => PlannedSnapshot.PlannedSnapshot | undefined
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* A child bridge plan: `Option.none()` when the planned parent does not run
|
|
165
|
+
* the child, `undefined` when the parent or child cannot be planned.
|
|
166
|
+
*/
|
|
167
|
+
type ChildServerPlan = () => Option.Option<PlannedSnapshot.PlannedSnapshot> | undefined
|
|
168
|
+
|
|
169
|
+
// Plans are computed on the first server read, at most once per bridge, so
|
|
170
|
+
// client starts never pay for them.
|
|
171
|
+
const lazyPlan = <A>(plan: () => A): () => A => {
|
|
172
|
+
let planned: { readonly value: A } | undefined
|
|
173
|
+
return () => {
|
|
174
|
+
if (planned === undefined) planned = { value: plan() }
|
|
175
|
+
return planned.value
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Planned snapshots are erased to the machine's snapshot schema; the bridge
|
|
180
|
+
// that owns a plan types its state, error and output.
|
|
181
|
+
const plannedSnapshot = <State, Error, Output>(
|
|
182
|
+
planned: PlannedSnapshot.PlannedSnapshot
|
|
183
|
+
): Machine.RuntimeSnapshot<State, Error, Output> => planned.snapshot as Machine.RuntimeSnapshot<State, Error, Output>
|
|
184
|
+
|
|
145
185
|
type RefState<Ref> = Ref extends Machine.MachineRef<infer State, any, any, any, any> ? State : never
|
|
146
186
|
type RefError<Ref> = Ref extends Machine.MachineRef<any, any, infer Error, any, any> ? Error : never
|
|
147
187
|
type RefOutput<Ref> = Ref extends Machine.MachineRef<any, any, any, infer Output, any> ? Output : never
|
|
@@ -213,6 +253,100 @@ export const childEmissions = <Child extends Machine.ChildMachine.Any, StartErro
|
|
|
213
253
|
)
|
|
214
254
|
)
|
|
215
255
|
|
|
256
|
+
// A write context exposes no registry, but a read through it does. Commands
|
|
257
|
+
// written before a machine is ready wait on that registry for the ref.
|
|
258
|
+
const registryAtom: Atom.Atom<AtomRegistry.AtomRegistry> = Atom.readable((get) => get.registry).pipe(
|
|
259
|
+
Atom.keepAlive
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Ordered delivery of held commands for one bridge in one registry.
|
|
264
|
+
*
|
|
265
|
+
* Commands written while the machine ref is still `Initial` are held here and
|
|
266
|
+
* delivered in write order once it settles. While held commands remain, later
|
|
267
|
+
* commands join the same queue so they cannot overtake earlier writes.
|
|
268
|
+
*/
|
|
269
|
+
interface CommandQueue {
|
|
270
|
+
readonly pending: Array<Effect.Effect<void>>
|
|
271
|
+
draining: boolean
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
type CommandQueues = WeakMap<AtomRegistry.AtomRegistry, CommandQueue>
|
|
275
|
+
|
|
276
|
+
const drainCommands = (queue: CommandQueue): Effect.Effect<void> =>
|
|
277
|
+
Effect.suspend(() => {
|
|
278
|
+
const next = queue.pending.shift()
|
|
279
|
+
if (next === undefined) {
|
|
280
|
+
queue.draining = false
|
|
281
|
+
return Effect.void
|
|
282
|
+
}
|
|
283
|
+
return Effect.andThen(next, drainCommands(queue))
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
const getCommandQueue = (queues: CommandQueues, registry: AtomRegistry.AtomRegistry): CommandQueue => {
|
|
287
|
+
let queue = queues.get(registry)
|
|
288
|
+
if (queue === undefined) {
|
|
289
|
+
queue = { pending: [], draining: false }
|
|
290
|
+
queues.set(registry, queue)
|
|
291
|
+
}
|
|
292
|
+
return queue
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Builds a command atom over a bridge ref.
|
|
297
|
+
*
|
|
298
|
+
* A write runs the command at once when the ref is ready and no earlier
|
|
299
|
+
* command is held. Otherwise the command is held until the ref settles, the
|
|
300
|
+
* write result stays waiting, and it settles with the command exit or with the
|
|
301
|
+
* startup failure.
|
|
302
|
+
*/
|
|
303
|
+
const makeCommandAtom = <Ref, StartError, E, W>(
|
|
304
|
+
ref: Atom.Atom<AsyncResult.AsyncResult<Ref, StartError>>,
|
|
305
|
+
queues: CommandQueues,
|
|
306
|
+
command: (ref: Ref, value: W) => Effect.Effect<void, E>
|
|
307
|
+
): Atom.Writable<AsyncResult.AsyncResult<void, StartError | E>, W> =>
|
|
308
|
+
Atom.writable<AsyncResult.AsyncResult<void, StartError | E>, W>(
|
|
309
|
+
(get): AsyncResult.AsyncResult<void, StartError | E> => {
|
|
310
|
+
const current = AsyncResult.map(get(ref), () => undefined)
|
|
311
|
+
const queue = queues.get(get.registry)
|
|
312
|
+
if (queue === undefined || !queue.draining) {
|
|
313
|
+
return current
|
|
314
|
+
}
|
|
315
|
+
return Option.getOrElse(
|
|
316
|
+
Option.map(get.self<AsyncResult.AsyncResult<void, StartError | E>>(), AsyncResult.waiting),
|
|
317
|
+
() => AsyncResult.initial(true)
|
|
318
|
+
)
|
|
319
|
+
},
|
|
320
|
+
(ctx, value) => {
|
|
321
|
+
const current = ctx.get(ref)
|
|
322
|
+
const registry = ctx.get(registryAtom)
|
|
323
|
+
const queue = getCommandQueue(queues, registry)
|
|
324
|
+
if (AsyncResult.isInitial(current) || queue.draining) {
|
|
325
|
+
ctx.setSelf(AsyncResult.initial(true))
|
|
326
|
+
queue.pending.push(
|
|
327
|
+
AtomRegistry.getResult(registry, ref).pipe(
|
|
328
|
+
Effect.flatMap((ready) => command(ready, value)),
|
|
329
|
+
Effect.exit,
|
|
330
|
+
Effect.map((exit) => {
|
|
331
|
+
const result = AsyncResult.fromExit(exit)
|
|
332
|
+
ctx.setSelf(queue.pending.length === 0 ? result : AsyncResult.waiting(result))
|
|
333
|
+
})
|
|
334
|
+
)
|
|
335
|
+
)
|
|
336
|
+
if (!queue.draining) {
|
|
337
|
+
queue.draining = true
|
|
338
|
+
Effect.runFork(drainCommands(queue))
|
|
339
|
+
}
|
|
340
|
+
} else if (AsyncResult.isFailure(current)) {
|
|
341
|
+
ctx.setSelf(AsyncResult.map(current, () => undefined))
|
|
342
|
+
} else {
|
|
343
|
+
Effect.runCallback(command(current.value, value), {
|
|
344
|
+
onExit: (exit) => ctx.setSelf(AsyncResult.fromExit(exit))
|
|
345
|
+
})
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
).pipe(Atom.withServerValueInitial)
|
|
349
|
+
|
|
216
350
|
const makeRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
217
351
|
snapshot: Atom.Atom<AsyncResult.AsyncResult<Machine.RuntimeSnapshot<State, Error, Output>, StartError>>
|
|
218
352
|
): Atom.Atom<AsyncResult.AsyncResult<State, StartError | Error>> =>
|
|
@@ -234,7 +368,11 @@ const makeRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
|
234
368
|
return AsyncResult.success(current.value.state, {
|
|
235
369
|
waiting: current.waiting
|
|
236
370
|
})
|
|
237
|
-
}).pipe(
|
|
371
|
+
}).pipe(
|
|
372
|
+
Atom.withEquality(Equal.equals),
|
|
373
|
+
// A planned snapshot is never an error, so its state is the server result.
|
|
374
|
+
Atom.withServerValue((get) => AsyncResult.map(readServerValue(snapshot, get), (current) => current.state))
|
|
375
|
+
)
|
|
238
376
|
|
|
239
377
|
const makeChildRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
240
378
|
snapshot: Atom.Atom<
|
|
@@ -267,7 +405,12 @@ const makeChildRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
|
267
405
|
return AsyncResult.success(Option.some(current.value.value.state), {
|
|
268
406
|
waiting: current.waiting
|
|
269
407
|
})
|
|
270
|
-
}).pipe(
|
|
408
|
+
}).pipe(
|
|
409
|
+
Atom.withEquality(Equal.equals),
|
|
410
|
+
Atom.withServerValue((get) =>
|
|
411
|
+
AsyncResult.map(readServerValue(snapshot, get), Option.map((current) => current.state))
|
|
412
|
+
)
|
|
413
|
+
)
|
|
271
414
|
|
|
272
415
|
const makeChildRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
273
416
|
parentRef: Atom.Atom<
|
|
@@ -275,7 +418,7 @@ const makeChildRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
|
275
418
|
>,
|
|
276
419
|
child: Child
|
|
277
420
|
): Atom.Atom<AsyncResult.AsyncResult<Option.Option<Machine.ChildMachine.Ref<Child>>, StartError>> =>
|
|
278
|
-
Atom.readable((get) => {
|
|
421
|
+
Atom.readable((get): AsyncResult.AsyncResult<Option.Option<Machine.ChildMachine.Ref<Child>>, StartError> => {
|
|
279
422
|
const parent = get(parentRef)
|
|
280
423
|
if (AsyncResult.isInitial(parent)) {
|
|
281
424
|
return AsyncResult.initial(parent.waiting)
|
|
@@ -301,11 +444,12 @@ const makeChildRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
|
301
444
|
)
|
|
302
445
|
get.addFinalizer(cancel)
|
|
303
446
|
return AsyncResult.success(current)
|
|
304
|
-
})
|
|
447
|
+
}).pipe(Atom.withServerValueInitial)
|
|
305
448
|
|
|
306
449
|
const makeChildFromRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
307
450
|
ref: Atom.Atom<AsyncResult.AsyncResult<Option.Option<Machine.ChildMachine.Ref<Child>>, StartError>>,
|
|
308
|
-
descriptor: Child
|
|
451
|
+
descriptor: Child,
|
|
452
|
+
plan: ChildServerPlan
|
|
309
453
|
): ChildMachineAtom<Child, StartError> => {
|
|
310
454
|
type Ref = Machine.ChildMachine.Ref<Child>
|
|
311
455
|
type State = RefState<Ref>
|
|
@@ -336,49 +480,38 @@ const makeChildFromRefAtom = <Child extends Machine.ChildMachine.Any, StartError
|
|
|
336
480
|
)
|
|
337
481
|
get.addFinalizer(cancel)
|
|
338
482
|
return AsyncResult.success(Option.some(Effect.runSync(handle.snapshot)))
|
|
339
|
-
})
|
|
483
|
+
}).pipe(
|
|
484
|
+
Atom.withServerValue((): AsyncResult.AsyncResult<
|
|
485
|
+
Option.Option<Machine.RuntimeSnapshot<State, Error, Output>>,
|
|
486
|
+
StartError
|
|
487
|
+
> => {
|
|
488
|
+
const planned = plan()
|
|
489
|
+
return planned === undefined
|
|
490
|
+
? AsyncResult.initial(true)
|
|
491
|
+
: AsyncResult.success(Option.map(planned, plannedSnapshot<State, Error, Output>), { waiting: true })
|
|
492
|
+
})
|
|
493
|
+
)
|
|
340
494
|
|
|
341
|
-
const
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
} else if (Option.isNone(result.value)) {
|
|
353
|
-
ctx.setSelf(AsyncResult.fail(new ChildNotActiveError({ id: descriptor.id })))
|
|
354
|
-
} else {
|
|
355
|
-
Effect.runCallback(result.value.value.send(event as never), {
|
|
356
|
-
onExit: (exit) => ctx.setSelf(AsyncResult.fromExit(exit))
|
|
357
|
-
})
|
|
358
|
-
}
|
|
359
|
-
}
|
|
495
|
+
const commands: CommandQueues = new WeakMap()
|
|
496
|
+
const activeChild = (current: Option.Option<Ref>): Effect.Effect<Ref, ChildNotActiveError> =>
|
|
497
|
+
Option.isSome(current)
|
|
498
|
+
? Effect.succeed(current.value)
|
|
499
|
+
: Effect.fail(new ChildNotActiveError({ id: descriptor.id }))
|
|
500
|
+
|
|
501
|
+
const send: ChildMachineAtom<Child, StartError>["send"] = makeCommandAtom(
|
|
502
|
+
ref,
|
|
503
|
+
commands,
|
|
504
|
+
(current, event: Machine.ChildMachine.Event<Child>) =>
|
|
505
|
+
Effect.flatMap(activeChild(current), (child) => child.send(event as never))
|
|
360
506
|
)
|
|
361
507
|
|
|
362
|
-
const stop =
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
(get) => AsyncResult.map(get(ref), () => undefined),
|
|
367
|
-
(ctx) => {
|
|
368
|
-
const result = ctx.get(ref)
|
|
369
|
-
if (AsyncResult.isInitial(result)) {
|
|
370
|
-
ctx.setSelf(AsyncResult.fail(new NotReadyError()))
|
|
371
|
-
} else if (AsyncResult.isFailure(result)) {
|
|
372
|
-
ctx.setSelf(AsyncResult.map(result, () => undefined))
|
|
373
|
-
} else if (Option.isNone(result.value)) {
|
|
374
|
-
ctx.setSelf(AsyncResult.fail(new ChildNotActiveError({ id: descriptor.id })))
|
|
375
|
-
} else {
|
|
376
|
-
Effect.runCallback(result.value.value.stop)
|
|
377
|
-
}
|
|
378
|
-
}
|
|
508
|
+
const stop: ChildMachineAtom<Child, StartError>["stop"] = makeCommandAtom(
|
|
509
|
+
ref,
|
|
510
|
+
commands,
|
|
511
|
+
(current, _: void) => Effect.flatMap(activeChild(current), (child) => child.stop)
|
|
379
512
|
)
|
|
380
513
|
|
|
381
|
-
const child = makeChildSelector<StartError>(ref as any)
|
|
514
|
+
const child = makeChildSelector<StartError>(ref as any, plan)
|
|
382
515
|
|
|
383
516
|
return {
|
|
384
517
|
ref,
|
|
@@ -393,7 +526,8 @@ const makeChildFromRefAtom = <Child extends Machine.ChildMachine.Any, StartError
|
|
|
393
526
|
const makeChildSelector = <StartError>(
|
|
394
527
|
parentRef: Atom.Atom<
|
|
395
528
|
AsyncResult.AsyncResult<Option.Option<Machine.MachineRef<any, any, any, any, any>>, StartError>
|
|
396
|
-
|
|
529
|
+
>,
|
|
530
|
+
parentPlan: ChildServerPlan
|
|
397
531
|
) => {
|
|
398
532
|
const byMachine = new WeakMap<object, (id: string) => ChildMachineAtom<Machine.ChildMachine.Any, StartError>>()
|
|
399
533
|
return <Child extends Machine.ChildMachine.Any>(descriptor: Child): ChildMachineAtom<Child, StartError> => {
|
|
@@ -404,7 +538,13 @@ const makeChildSelector = <StartError>(
|
|
|
404
538
|
const child = internalMachine.child(id, machine)
|
|
405
539
|
return makeChildFromRefAtom(
|
|
406
540
|
makeChildRefAtom(parentRef as any, child),
|
|
407
|
-
child
|
|
541
|
+
child,
|
|
542
|
+
lazyPlan(() => {
|
|
543
|
+
const parent = parentPlan()
|
|
544
|
+
return parent === undefined || Option.isNone(parent)
|
|
545
|
+
? parent
|
|
546
|
+
: PlannedSnapshot.planInvokedChild(parent.value, child)
|
|
547
|
+
})
|
|
408
548
|
)
|
|
409
549
|
})
|
|
410
550
|
family = (id) => atoms(id) as ChildMachineAtom<Machine.ChildMachine.Any, StartError>
|
|
@@ -415,9 +555,11 @@ const makeChildSelector = <StartError>(
|
|
|
415
555
|
}
|
|
416
556
|
|
|
417
557
|
const makeFromRefAtom = <State, Event, Error, Output, StartError, Emitted>(
|
|
418
|
-
|
|
419
|
-
machine: Machine.Machine.Any
|
|
558
|
+
liveRef: Atom.Atom<AsyncResult.AsyncResult<Machine.MachineRef<State, Event, Error, Output, Emitted>, StartError>>,
|
|
559
|
+
machine: Machine.Machine.Any,
|
|
560
|
+
plan: ServerPlan
|
|
420
561
|
): MachineAtom<State, Event, Error, Output, StartError, Emitted> => {
|
|
562
|
+
const ref = Atom.withServerValueInitial(liveRef)
|
|
421
563
|
const snapshot = Atom.readable((
|
|
422
564
|
get
|
|
423
565
|
): AsyncResult.AsyncResult<Machine.RuntimeSnapshot<State, Error, Output>, StartError> => {
|
|
@@ -451,46 +593,34 @@ const makeFromRefAtom = <State, Event, Error, Output, StartError, Emitted>(
|
|
|
451
593
|
return AsyncResult.success(current, {
|
|
452
594
|
waiting: current.status === "active"
|
|
453
595
|
})
|
|
454
|
-
})
|
|
596
|
+
}).pipe(
|
|
597
|
+
Atom.withServerValue((): AsyncResult.AsyncResult<Machine.RuntimeSnapshot<State, Error, Output>, StartError> => {
|
|
598
|
+
const planned = plan()
|
|
599
|
+
return planned === undefined
|
|
600
|
+
? AsyncResult.initial(true)
|
|
601
|
+
: AsyncResult.success(plannedSnapshot<State, Error, Output>(planned), { waiting: true })
|
|
602
|
+
})
|
|
603
|
+
)
|
|
455
604
|
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
(
|
|
462
|
-
const result = ctx.get(ref)
|
|
463
|
-
if (AsyncResult.isInitial(result)) {
|
|
464
|
-
ctx.setSelf(AsyncResult.fail(new NotReadyError()))
|
|
465
|
-
} else if (AsyncResult.isFailure(result)) {
|
|
466
|
-
ctx.setSelf(AsyncResult.map(result, () => undefined))
|
|
467
|
-
} else {
|
|
468
|
-
Effect.runCallback(result.value.send(event), {
|
|
469
|
-
onExit: (exit) =>
|
|
470
|
-
ctx.setSelf(
|
|
471
|
-
AsyncResult.fromExit(exit)
|
|
472
|
-
)
|
|
473
|
-
})
|
|
474
|
-
}
|
|
475
|
-
}
|
|
605
|
+
const commands: CommandQueues = new WeakMap()
|
|
606
|
+
|
|
607
|
+
const send: MachineAtom<State, Event, Error, Output, StartError, Emitted>["send"] = makeCommandAtom(
|
|
608
|
+
ref,
|
|
609
|
+
commands,
|
|
610
|
+
(handle, event: Event) => handle.send(event)
|
|
476
611
|
)
|
|
477
612
|
|
|
478
|
-
const stop
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
if (AsyncResult.isInitial(result)) {
|
|
483
|
-
ctx.setSelf(AsyncResult.fail(new NotReadyError()))
|
|
484
|
-
} else if (AsyncResult.isFailure(result)) {
|
|
485
|
-
ctx.setSelf(AsyncResult.map(result, () => undefined))
|
|
486
|
-
} else {
|
|
487
|
-
Effect.runCallback(result.value.stop)
|
|
488
|
-
}
|
|
489
|
-
}
|
|
613
|
+
const stop: MachineAtom<State, Event, Error, Output, StartError, Emitted>["stop"] = makeCommandAtom(
|
|
614
|
+
ref,
|
|
615
|
+
commands,
|
|
616
|
+
(handle, _: void) => handle.stop
|
|
490
617
|
)
|
|
491
618
|
|
|
492
619
|
const optionalRef = Atom.mapResult(ref, Option.some)
|
|
493
|
-
const child = makeChildSelector<StartError>(optionalRef as any)
|
|
620
|
+
const child = makeChildSelector<StartError>(optionalRef as any, () => {
|
|
621
|
+
const planned = plan()
|
|
622
|
+
return planned === undefined ? undefined : Option.some(planned)
|
|
623
|
+
})
|
|
494
624
|
|
|
495
625
|
const result = {
|
|
496
626
|
ref,
|
|
@@ -560,6 +690,16 @@ type SelectorKind =
|
|
|
560
690
|
| "selectSnapshot"
|
|
561
691
|
| "selectSnapshotChild"
|
|
562
692
|
|
|
693
|
+
// Selectors project the bridge result live and its planned result on the server.
|
|
694
|
+
const mapResultSelector = <A, B, E>(
|
|
695
|
+
source: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
696
|
+
f: (value: A) => B
|
|
697
|
+
): Atom.Atom<AsyncResult.AsyncResult<B, E>> =>
|
|
698
|
+
Atom.mapResult(source, f).pipe(
|
|
699
|
+
Atom.withEquality(Equal.equals),
|
|
700
|
+
Atom.withServerValue((get) => AsyncResult.map(readServerValue(source, get), f))
|
|
701
|
+
)
|
|
702
|
+
|
|
563
703
|
const selectorsByBridge = new WeakMap<object, Map<SelectorKind, Map<string, Atom.Atom<any>>>>()
|
|
564
704
|
|
|
565
705
|
const cachedSelector = <A>(
|
|
@@ -605,10 +745,7 @@ export const select = <
|
|
|
605
745
|
self,
|
|
606
746
|
"select",
|
|
607
747
|
path,
|
|
608
|
-
() =>
|
|
609
|
-
Atom.mapResult(self.result, (snapshot) => selectValueByPath(snapshot, path)).pipe(
|
|
610
|
-
Atom.withEquality(Equal.equals)
|
|
611
|
-
)
|
|
748
|
+
() => mapResultSelector(self.result, (snapshot) => selectValueByPath(snapshot, path))
|
|
612
749
|
)
|
|
613
750
|
|
|
614
751
|
export const selectSnapshot = <
|
|
@@ -629,10 +766,7 @@ export const selectSnapshot = <
|
|
|
629
766
|
self,
|
|
630
767
|
"selectSnapshot",
|
|
631
768
|
path,
|
|
632
|
-
() =>
|
|
633
|
-
Atom.mapResult(self.result, (snapshot) => selectSnapshotByPath(snapshot, path)).pipe(
|
|
634
|
-
Atom.withEquality(Equal.equals)
|
|
635
|
-
)
|
|
769
|
+
() => mapResultSelector(self.result, (snapshot) => selectSnapshotByPath(snapshot, path))
|
|
636
770
|
)
|
|
637
771
|
|
|
638
772
|
export const selectChild = <
|
|
@@ -649,10 +783,10 @@ export const selectChild = <
|
|
|
649
783
|
>
|
|
650
784
|
> =>
|
|
651
785
|
cachedSelector(self, "selectChild", path, () =>
|
|
652
|
-
|
|
786
|
+
mapResultSelector(
|
|
653
787
|
self.result,
|
|
654
788
|
Option.flatMap((snapshot) => selectValueByPath(snapshot, path))
|
|
655
|
-
)
|
|
789
|
+
))
|
|
656
790
|
|
|
657
791
|
export const selectSnapshotChild = <
|
|
658
792
|
Child extends Machine.ChildMachine.Any,
|
|
@@ -668,10 +802,10 @@ export const selectSnapshotChild = <
|
|
|
668
802
|
>
|
|
669
803
|
> =>
|
|
670
804
|
cachedSelector(self, "selectSnapshotChild", path, () =>
|
|
671
|
-
|
|
805
|
+
mapResultSelector(
|
|
672
806
|
self.result,
|
|
673
807
|
Option.flatMap((snapshot) => selectSnapshotByPath(snapshot, path))
|
|
674
|
-
)
|
|
808
|
+
))
|
|
675
809
|
|
|
676
810
|
export const matches = <
|
|
677
811
|
State extends Machine.Machine.AtomicSnapshot<string, unknown>,
|
|
@@ -689,10 +823,7 @@ export const matches = <
|
|
|
689
823
|
self,
|
|
690
824
|
"matches",
|
|
691
825
|
path,
|
|
692
|
-
() =>
|
|
693
|
-
Atom.mapResult(self.result, (snapshot) => Option.isSome(Topology.getSnapshotByPath(snapshot, path))).pipe(
|
|
694
|
-
Atom.withEquality(Equal.equals)
|
|
695
|
-
)
|
|
826
|
+
() => mapResultSelector(self.result, (snapshot) => Option.isSome(Topology.getSnapshotByPath(snapshot, path)))
|
|
696
827
|
)
|
|
697
828
|
|
|
698
829
|
export const matchesChild = <
|
|
@@ -706,10 +837,10 @@ export const matchesChild = <
|
|
|
706
837
|
AsyncResult.AsyncResult<boolean, StartError | RefError<Machine.ChildMachine.Ref<Child>>>
|
|
707
838
|
> =>
|
|
708
839
|
cachedSelector(self, "matchesChild", path, () =>
|
|
709
|
-
|
|
840
|
+
mapResultSelector(
|
|
710
841
|
self.result,
|
|
711
842
|
Option.exists((snapshot) => Option.isSome(Topology.getSnapshotByPath(snapshot, path)))
|
|
712
|
-
)
|
|
843
|
+
))
|
|
713
844
|
|
|
714
845
|
export const can = (event: unknown) => {
|
|
715
846
|
const byBridge = new WeakMap<object, Atom.Atom<AsyncResult.AsyncResult<boolean, any>>>()
|
|
@@ -725,9 +856,11 @@ export const can = (event: unknown) => {
|
|
|
725
856
|
? () => Effect.die(new Error("AtomMachine.can requires a machine atom created by AtomMachine"))
|
|
726
857
|
: internalMachine.can(machine)
|
|
727
858
|
|
|
728
|
-
const
|
|
729
|
-
|
|
730
|
-
|
|
859
|
+
const evaluate = (
|
|
860
|
+
current: AsyncResult.AsyncResult<Machine.RuntimeSnapshot<any, any, any>, any>,
|
|
861
|
+
input: unknown,
|
|
862
|
+
previous: Option.Option<AsyncResult.AsyncResult<boolean, any>>
|
|
863
|
+
): AsyncResult.AsyncResult<boolean, any> => {
|
|
731
864
|
if (AsyncResult.isInitial(current)) {
|
|
732
865
|
return AsyncResult.initial(current.waiting)
|
|
733
866
|
} else if (AsyncResult.isFailure(current)) {
|
|
@@ -744,7 +877,6 @@ export const can = (event: unknown) => {
|
|
|
744
877
|
return AsyncResult.success(false, { waiting: current.waiting })
|
|
745
878
|
}
|
|
746
879
|
|
|
747
|
-
const input = Atom.isAtom(event) ? get(event) : event
|
|
748
880
|
const exit = Effect.runSyncExit(query(current.value.state, input))
|
|
749
881
|
return exit._tag === "Success"
|
|
750
882
|
? AsyncResult.success(exit.value, { waiting: current.waiting })
|
|
@@ -752,7 +884,24 @@ export const can = (event: unknown) => {
|
|
|
752
884
|
previous,
|
|
753
885
|
waiting: current.waiting
|
|
754
886
|
})
|
|
755
|
-
}
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
const result = Atom.readable((get): AsyncResult.AsyncResult<boolean, any> =>
|
|
890
|
+
evaluate(
|
|
891
|
+
get(self.snapshot),
|
|
892
|
+
Atom.isAtom(event) ? get(event) : event,
|
|
893
|
+
get.self<AsyncResult.AsyncResult<boolean, any>>()
|
|
894
|
+
)
|
|
895
|
+
).pipe(
|
|
896
|
+
Atom.withEquality(Equal.equals),
|
|
897
|
+
Atom.withServerValue((get) =>
|
|
898
|
+
evaluate(
|
|
899
|
+
readServerValue(self.snapshot, get),
|
|
900
|
+
Atom.isAtom(event) ? readServerValue(event, get) : event,
|
|
901
|
+
Option.none()
|
|
902
|
+
)
|
|
903
|
+
)
|
|
904
|
+
)
|
|
756
905
|
|
|
757
906
|
byBridge.set(self, result)
|
|
758
907
|
return result
|
|
@@ -839,7 +988,7 @@ export const make: {
|
|
|
839
988
|
} = ((machine: Machine.Machine.Any, ...args: ReadonlyArray<unknown>) => {
|
|
840
989
|
const prepared = Atom.make(() => internalMachine.prepare(machine, ...args))
|
|
841
990
|
const ref = Atom.make((get) => startPreparedMachineAtomEffect(get, prepared as any))
|
|
842
|
-
const result = makeFromRefAtom(ref as any, machine)
|
|
991
|
+
const result = makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planStart(machine, args)))
|
|
843
992
|
preparedByMachineAtom.set(result, prepared as any)
|
|
844
993
|
return result
|
|
845
994
|
}) as any
|
|
@@ -858,7 +1007,7 @@ export const resume: {
|
|
|
858
1007
|
): ResumedMachineAtomOf<M, never>
|
|
859
1008
|
} = ((machine: Machine.Machine.Any, snapshot: Machine.Machine.Snapshot<any>) => {
|
|
860
1009
|
const ref = Atom.make((get) => resumeMachineAtomEffect(get, machine, snapshot))
|
|
861
|
-
return makeFromRefAtom(ref as any, machine)
|
|
1010
|
+
return makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planResume(machine, snapshot)))
|
|
862
1011
|
}) as any
|
|
863
1012
|
|
|
864
1013
|
const makeWithRuntime = (
|
|
@@ -868,7 +1017,7 @@ const makeWithRuntime = (
|
|
|
868
1017
|
): MachineAtom<any, any, any, any, any, any> => {
|
|
869
1018
|
const prepared = runtime.atom(() => internalMachine.prepare(machine, ...args))
|
|
870
1019
|
const ref = runtime.atom((get) => startPreparedMachineAtomEffect(get, prepared as any))
|
|
871
|
-
const result = makeFromRefAtom(ref as any, machine)
|
|
1020
|
+
const result = makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planStart(machine, args)))
|
|
872
1021
|
preparedByMachineAtom.set(result, prepared as any)
|
|
873
1022
|
return result
|
|
874
1023
|
}
|
|
@@ -879,10 +1028,10 @@ const resumeWithRuntime = (
|
|
|
879
1028
|
snapshot: Machine.Machine.Snapshot<any>
|
|
880
1029
|
): MachineAtom<any, any, any, any, any, any> => {
|
|
881
1030
|
const ref = runtime.atom((get) => resumeMachineAtomEffect(get, machine, snapshot))
|
|
882
|
-
return makeFromRefAtom(ref as any, machine)
|
|
1031
|
+
return makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planResume(machine, snapshot)))
|
|
883
1032
|
}
|
|
884
1033
|
|
|
885
|
-
type FamilyBridge = MachineAtom
|
|
1034
|
+
type FamilyBridge = MachineAtom.Any | ChildMachineAtom.Any
|
|
886
1035
|
|
|
887
1036
|
type FamilyOptions = {
|
|
888
1037
|
readonly atoms: Readonly<Record<string, (bridge: FamilyBridge) => Atom.Atom<any>>>
|
|
@@ -900,7 +1049,8 @@ const retainFamilyOwner = <Source extends Atom.Atom<any>>(
|
|
|
900
1049
|
(get) => get(retained.source),
|
|
901
1050
|
{ initialValueTarget: source }
|
|
902
1051
|
).pipe(
|
|
903
|
-
Atom.withEquality((value, next) => retained.source.equals(value, next))
|
|
1052
|
+
Atom.withEquality((value, next) => retained.source.equals(value, next)),
|
|
1053
|
+
Atom.withServerValue((get) => readServerValue(retained.source, get))
|
|
904
1054
|
)
|
|
905
1055
|
if (label !== undefined) {
|
|
906
1056
|
atom = atom.pipe(Atom.withLabel(label))
|
|
@@ -937,7 +1087,7 @@ export const family = (
|
|
|
937
1087
|
)
|
|
938
1088
|
|
|
939
1089
|
export const familyChild = (
|
|
940
|
-
parent:
|
|
1090
|
+
parent: FamilyBridge,
|
|
941
1091
|
options: FamilyOptions & { readonly child: (key: any) => Machine.ChildMachine.Any }
|
|
942
1092
|
): Readonly<Record<string, (key: any) => Atom.Atom<any>>> =>
|
|
943
1093
|
makeFamily(
|
|
@@ -28,6 +28,15 @@ export type ChildEntry =
|
|
|
28
28
|
ownerActive?: boolean
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Prefix of the child ids the runtime assigns to Effect, Stream, and timer
|
|
33
|
+
* invocations. Caller-chosen ids in this namespace are always treated as taken,
|
|
34
|
+
* so runtime-owned invocation ids can never collide with another child.
|
|
35
|
+
*/
|
|
36
|
+
export const invocationChildIdPrefix = "Machine.invocation:"
|
|
37
|
+
|
|
38
|
+
export const isReservedChildId = (id: string): boolean => id.startsWith(invocationChildIdPrefix)
|
|
39
|
+
|
|
31
40
|
export type ChildSelector = string | ChildDescriptor
|
|
32
41
|
export type ChildKey = string | symbol
|
|
33
42
|
|
|
@@ -8,8 +8,9 @@ import * as Cause from "effect/Cause"
|
|
|
8
8
|
import * as Effect from "effect/Effect"
|
|
9
9
|
import * as Stream from "effect/Stream"
|
|
10
10
|
import type { ChildMachine, ChildOwner, Inspection, Logic, Machine } from "../../Machine.js"
|
|
11
|
+
import * as ChildRegistry from "./childRegistry.js"
|
|
11
12
|
import * as Configuration from "./configuration.js"
|
|
12
|
-
import { InfiniteTransitionError, MachineSchemaDecodeError, StoppedError } from "./errors.js"
|
|
13
|
+
import { ChildAlreadyExistsError, InfiniteTransitionError, MachineSchemaDecodeError, StoppedError } from "./errors.js"
|
|
13
14
|
import * as InvocationDefinition from "./invocationDefinition.js"
|
|
14
15
|
import * as InvocationEvent from "./invocationEvent.js"
|
|
15
16
|
import * as Planner from "./planner.js"
|
|
@@ -33,7 +34,8 @@ export interface AnyConfig {
|
|
|
33
34
|
export const makeKey = (path: string, id: string): string => `${path.length}:${path}${id}`
|
|
34
35
|
|
|
35
36
|
/** @internal */
|
|
36
|
-
export const makeChildId = (path: string, id: string): string =>
|
|
37
|
+
export const makeChildId = (path: string, id: string): string =>
|
|
38
|
+
`${ChildRegistry.invocationChildIdPrefix}${makeKey(path, id)}`
|
|
37
39
|
|
|
38
40
|
const oneShot = (effect: Effect.Effect<any, any, any>): Logic<void, never, any, any, any> => ({
|
|
39
41
|
initial: () => Effect.void,
|
|
@@ -272,6 +274,15 @@ const startResolved = (
|
|
|
272
274
|
})
|
|
273
275
|
})
|
|
274
276
|
|
|
277
|
+
// Logic addresses and child descriptor ids are caller-chosen. Keep them out of
|
|
278
|
+
// the namespace reserved for runtime-owned invocation ids; runtime-owned ids
|
|
279
|
+
// skip this check.
|
|
280
|
+
const reservedChildIdFailure = (
|
|
281
|
+
childId: string,
|
|
282
|
+
invokeId: string
|
|
283
|
+
): Effect.Effect<never, ChildAlreadyExistsError> | undefined =>
|
|
284
|
+
ChildRegistry.isReservedChildId(childId) ? Effect.fail(new ChildAlreadyExistsError({ id: invokeId })) : undefined
|
|
285
|
+
|
|
275
286
|
const start = (
|
|
276
287
|
scope: Runtime.ProcessScope<any>,
|
|
277
288
|
ownedChildren: Runtime.OwnedChildRuntime,
|
|
@@ -279,6 +290,8 @@ const start = (
|
|
|
279
290
|
config: AnyConfig
|
|
280
291
|
): Effect.Effect<void, any, any> => {
|
|
281
292
|
const invokeId = String(config.id)
|
|
293
|
+
const reserved = config.address === undefined ? undefined : reservedChildIdFailure(String(config.address), invokeId)
|
|
294
|
+
if (reserved !== undefined) return reserved
|
|
282
295
|
return startResolved(
|
|
283
296
|
scope,
|
|
284
297
|
ownedChildren,
|
|
@@ -305,6 +318,8 @@ const startStaticChild = (
|
|
|
305
318
|
// source function so each running parent does not retain a resolved config
|
|
306
319
|
// object and an otherwise redundant source closure.
|
|
307
320
|
const descriptor = raw.child
|
|
321
|
+
const reserved = reservedChildIdFailure(descriptor.id, descriptor.id)
|
|
322
|
+
if (reserved !== undefined) return reserved
|
|
308
323
|
return startResolved(
|
|
309
324
|
scope,
|
|
310
325
|
ownedChildren,
|