@typeonce/effect-machine 0.41.1 → 0.43.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 +3 -7
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +153 -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 +95 -19
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +13 -7
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/docs/agent-guide.md +28 -5
- package/docs/effect-atom-react.md +262 -14
- package/docs/machine-review.md +18 -4
- package/package.json +1 -1
- package/src/Machine.ts +229 -32
- package/src/internal/machine/atom.ts +305 -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 +104 -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
|
}> {}
|
|
@@ -73,6 +72,9 @@ const preparedByMachineAtom = new WeakMap<
|
|
|
73
72
|
|
|
74
73
|
const machineByMachineAtom = new WeakMap<object, Machine.Machine.Any>()
|
|
75
74
|
|
|
75
|
+
export const isMachineAtom = (u: unknown): u is MachineAtom<any, never, any, any, any, any> =>
|
|
76
|
+
typeof u === "object" && u !== null && machineByMachineAtom.has(u)
|
|
77
|
+
|
|
76
78
|
type WeakFamilyEntry<Value extends object> = {
|
|
77
79
|
readonly ref: WeakRef<Value>
|
|
78
80
|
}
|
|
@@ -142,6 +144,71 @@ const resumeMachineAtomEffect = (
|
|
|
142
144
|
snapshot: Machine.Machine.Snapshot<any>
|
|
143
145
|
) => runMachineAtomEffect(get, internalMachine.resume(machine, snapshot))
|
|
144
146
|
|
|
147
|
+
// Server reads. A server read renders the planned startup snapshot and never
|
|
148
|
+
// evaluates a bridge ref, because evaluating it starts the machine. Effect
|
|
149
|
+
// passes plain registry reads to a server value, so every exported atom carries
|
|
150
|
+
// its own server value derived from the bridge plan.
|
|
151
|
+
|
|
152
|
+
type ServerRead = <A>(atom: Atom.Atom<A>) => A
|
|
153
|
+
|
|
154
|
+
interface ServerValued<A> {
|
|
155
|
+
readonly [Atom.ServerValueTypeId]: (get: ServerRead) => A
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Effect stores the server read on the atom without exposing it in `Atom`'s
|
|
159
|
+
// type. This is the only erased boundary of the server protocol.
|
|
160
|
+
const readServerValue = <A>(atom: Atom.Atom<A>, get: ServerRead): A =>
|
|
161
|
+
Atom.ServerValueTypeId in atom ? (atom as Atom.Atom<A> & ServerValued<A>)[Atom.ServerValueTypeId](get) : get(atom)
|
|
162
|
+
|
|
163
|
+
// React requires a referentially stable server snapshot, but Effect does not
|
|
164
|
+
// cache server reads. A server value is a pure function of the bridge plan and
|
|
165
|
+
// of the server values it reads, so each atom memoizes its latest result on the
|
|
166
|
+
// identity of those inputs. Unchanged inputs therefore return the same value
|
|
167
|
+
// for every read and registry, and derived atoms stay stable because their
|
|
168
|
+
// sources are.
|
|
169
|
+
const stableServerValue = <const Inputs extends ReadonlyArray<unknown>, A>(
|
|
170
|
+
read: (get: ServerRead) => Inputs,
|
|
171
|
+
compute: (...inputs: Inputs) => A
|
|
172
|
+
): (get: ServerRead) => A => {
|
|
173
|
+
let memo: { readonly inputs: Inputs; readonly value: A } | undefined
|
|
174
|
+
return (get) => {
|
|
175
|
+
const inputs = read(get)
|
|
176
|
+
if (
|
|
177
|
+
memo === undefined ||
|
|
178
|
+
memo.inputs.length !== inputs.length ||
|
|
179
|
+
!memo.inputs.every((input, index) => Object.is(input, inputs[index]))
|
|
180
|
+
) {
|
|
181
|
+
memo = { inputs, value: compute(...inputs) }
|
|
182
|
+
}
|
|
183
|
+
return memo.value
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** A bridge plan: `undefined` when no snapshot can be planned synchronously. */
|
|
188
|
+
type ServerPlan = () => PlannedSnapshot.PlannedSnapshot | undefined
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A child bridge plan: `Option.none()` when the planned parent does not run
|
|
192
|
+
* the child, `undefined` when the parent or child cannot be planned.
|
|
193
|
+
*/
|
|
194
|
+
type ChildServerPlan = () => Option.Option<PlannedSnapshot.PlannedSnapshot> | undefined
|
|
195
|
+
|
|
196
|
+
// Plans are computed on the first server read, at most once per bridge, so
|
|
197
|
+
// client starts never pay for them.
|
|
198
|
+
const lazyPlan = <A>(plan: () => A): () => A => {
|
|
199
|
+
let planned: { readonly value: A } | undefined
|
|
200
|
+
return () => {
|
|
201
|
+
if (planned === undefined) planned = { value: plan() }
|
|
202
|
+
return planned.value
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Planned snapshots are erased to the machine's snapshot schema; the bridge
|
|
207
|
+
// that owns a plan types its state, error and output.
|
|
208
|
+
const plannedSnapshot = <State, Error, Output>(
|
|
209
|
+
planned: PlannedSnapshot.PlannedSnapshot
|
|
210
|
+
): Machine.RuntimeSnapshot<State, Error, Output> => planned.snapshot as Machine.RuntimeSnapshot<State, Error, Output>
|
|
211
|
+
|
|
145
212
|
type RefState<Ref> = Ref extends Machine.MachineRef<infer State, any, any, any, any> ? State : never
|
|
146
213
|
type RefError<Ref> = Ref extends Machine.MachineRef<any, any, infer Error, any, any> ? Error : never
|
|
147
214
|
type RefOutput<Ref> = Ref extends Machine.MachineRef<any, any, any, infer Output, any> ? Output : never
|
|
@@ -213,6 +280,100 @@ export const childEmissions = <Child extends Machine.ChildMachine.Any, StartErro
|
|
|
213
280
|
)
|
|
214
281
|
)
|
|
215
282
|
|
|
283
|
+
// A write context exposes no registry, but a read through it does. Commands
|
|
284
|
+
// written before a machine is ready wait on that registry for the ref.
|
|
285
|
+
const registryAtom: Atom.Atom<AtomRegistry.AtomRegistry> = Atom.readable((get) => get.registry).pipe(
|
|
286
|
+
Atom.keepAlive
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Ordered delivery of held commands for one bridge in one registry.
|
|
291
|
+
*
|
|
292
|
+
* Commands written while the machine ref is still `Initial` are held here and
|
|
293
|
+
* delivered in write order once it settles. While held commands remain, later
|
|
294
|
+
* commands join the same queue so they cannot overtake earlier writes.
|
|
295
|
+
*/
|
|
296
|
+
interface CommandQueue {
|
|
297
|
+
readonly pending: Array<Effect.Effect<void>>
|
|
298
|
+
draining: boolean
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
type CommandQueues = WeakMap<AtomRegistry.AtomRegistry, CommandQueue>
|
|
302
|
+
|
|
303
|
+
const drainCommands = (queue: CommandQueue): Effect.Effect<void> =>
|
|
304
|
+
Effect.suspend(() => {
|
|
305
|
+
const next = queue.pending.shift()
|
|
306
|
+
if (next === undefined) {
|
|
307
|
+
queue.draining = false
|
|
308
|
+
return Effect.void
|
|
309
|
+
}
|
|
310
|
+
return Effect.andThen(next, drainCommands(queue))
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
const getCommandQueue = (queues: CommandQueues, registry: AtomRegistry.AtomRegistry): CommandQueue => {
|
|
314
|
+
let queue = queues.get(registry)
|
|
315
|
+
if (queue === undefined) {
|
|
316
|
+
queue = { pending: [], draining: false }
|
|
317
|
+
queues.set(registry, queue)
|
|
318
|
+
}
|
|
319
|
+
return queue
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Builds a command atom over a bridge ref.
|
|
324
|
+
*
|
|
325
|
+
* A write runs the command at once when the ref is ready and no earlier
|
|
326
|
+
* command is held. Otherwise the command is held until the ref settles, the
|
|
327
|
+
* write result stays waiting, and it settles with the command exit or with the
|
|
328
|
+
* startup failure.
|
|
329
|
+
*/
|
|
330
|
+
const makeCommandAtom = <Ref, StartError, E, W>(
|
|
331
|
+
ref: Atom.Atom<AsyncResult.AsyncResult<Ref, StartError>>,
|
|
332
|
+
queues: CommandQueues,
|
|
333
|
+
command: (ref: Ref, value: W) => Effect.Effect<void, E>
|
|
334
|
+
): Atom.Writable<AsyncResult.AsyncResult<void, StartError | E>, W> =>
|
|
335
|
+
Atom.writable<AsyncResult.AsyncResult<void, StartError | E>, W>(
|
|
336
|
+
(get): AsyncResult.AsyncResult<void, StartError | E> => {
|
|
337
|
+
const current = AsyncResult.map(get(ref), () => undefined)
|
|
338
|
+
const queue = queues.get(get.registry)
|
|
339
|
+
if (queue === undefined || !queue.draining) {
|
|
340
|
+
return current
|
|
341
|
+
}
|
|
342
|
+
return Option.getOrElse(
|
|
343
|
+
Option.map(get.self<AsyncResult.AsyncResult<void, StartError | E>>(), AsyncResult.waiting),
|
|
344
|
+
() => AsyncResult.initial(true)
|
|
345
|
+
)
|
|
346
|
+
},
|
|
347
|
+
(ctx, value) => {
|
|
348
|
+
const current = ctx.get(ref)
|
|
349
|
+
const registry = ctx.get(registryAtom)
|
|
350
|
+
const queue = getCommandQueue(queues, registry)
|
|
351
|
+
if (AsyncResult.isInitial(current) || queue.draining) {
|
|
352
|
+
ctx.setSelf(AsyncResult.initial(true))
|
|
353
|
+
queue.pending.push(
|
|
354
|
+
AtomRegistry.getResult(registry, ref).pipe(
|
|
355
|
+
Effect.flatMap((ready) => command(ready, value)),
|
|
356
|
+
Effect.exit,
|
|
357
|
+
Effect.map((exit) => {
|
|
358
|
+
const result = AsyncResult.fromExit(exit)
|
|
359
|
+
ctx.setSelf(queue.pending.length === 0 ? result : AsyncResult.waiting(result))
|
|
360
|
+
})
|
|
361
|
+
)
|
|
362
|
+
)
|
|
363
|
+
if (!queue.draining) {
|
|
364
|
+
queue.draining = true
|
|
365
|
+
Effect.runFork(drainCommands(queue))
|
|
366
|
+
}
|
|
367
|
+
} else if (AsyncResult.isFailure(current)) {
|
|
368
|
+
ctx.setSelf(AsyncResult.map(current, () => undefined))
|
|
369
|
+
} else {
|
|
370
|
+
Effect.runCallback(command(current.value, value), {
|
|
371
|
+
onExit: (exit) => ctx.setSelf(AsyncResult.fromExit(exit))
|
|
372
|
+
})
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
).pipe(Atom.withServerValueInitial)
|
|
376
|
+
|
|
216
377
|
const makeRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
217
378
|
snapshot: Atom.Atom<AsyncResult.AsyncResult<Machine.RuntimeSnapshot<State, Error, Output>, StartError>>
|
|
218
379
|
): Atom.Atom<AsyncResult.AsyncResult<State, StartError | Error>> =>
|
|
@@ -234,7 +395,16 @@ const makeRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
|
234
395
|
return AsyncResult.success(current.value.state, {
|
|
235
396
|
waiting: current.waiting
|
|
236
397
|
})
|
|
237
|
-
}).pipe(
|
|
398
|
+
}).pipe(
|
|
399
|
+
Atom.withEquality(Equal.equals),
|
|
400
|
+
// A planned snapshot is never an error, so its state is the server result.
|
|
401
|
+
Atom.withServerValue(
|
|
402
|
+
stableServerValue(
|
|
403
|
+
(get) => [readServerValue(snapshot, get)],
|
|
404
|
+
(current) => AsyncResult.map(current, (current) => current.state)
|
|
405
|
+
)
|
|
406
|
+
)
|
|
407
|
+
)
|
|
238
408
|
|
|
239
409
|
const makeChildRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
240
410
|
snapshot: Atom.Atom<
|
|
@@ -267,7 +437,15 @@ const makeChildRuntimeResultAtom = <State, Error, Output, StartError>(
|
|
|
267
437
|
return AsyncResult.success(Option.some(current.value.value.state), {
|
|
268
438
|
waiting: current.waiting
|
|
269
439
|
})
|
|
270
|
-
}).pipe(
|
|
440
|
+
}).pipe(
|
|
441
|
+
Atom.withEquality(Equal.equals),
|
|
442
|
+
Atom.withServerValue(
|
|
443
|
+
stableServerValue(
|
|
444
|
+
(get) => [readServerValue(snapshot, get)],
|
|
445
|
+
(current) => AsyncResult.map(current, Option.map((current) => current.state))
|
|
446
|
+
)
|
|
447
|
+
)
|
|
448
|
+
)
|
|
271
449
|
|
|
272
450
|
const makeChildRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
273
451
|
parentRef: Atom.Atom<
|
|
@@ -275,7 +453,7 @@ const makeChildRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
|
275
453
|
>,
|
|
276
454
|
child: Child
|
|
277
455
|
): Atom.Atom<AsyncResult.AsyncResult<Option.Option<Machine.ChildMachine.Ref<Child>>, StartError>> =>
|
|
278
|
-
Atom.readable((get) => {
|
|
456
|
+
Atom.readable((get): AsyncResult.AsyncResult<Option.Option<Machine.ChildMachine.Ref<Child>>, StartError> => {
|
|
279
457
|
const parent = get(parentRef)
|
|
280
458
|
if (AsyncResult.isInitial(parent)) {
|
|
281
459
|
return AsyncResult.initial(parent.waiting)
|
|
@@ -301,11 +479,12 @@ const makeChildRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
|
301
479
|
)
|
|
302
480
|
get.addFinalizer(cancel)
|
|
303
481
|
return AsyncResult.success(current)
|
|
304
|
-
})
|
|
482
|
+
}).pipe(Atom.withServerValueInitial)
|
|
305
483
|
|
|
306
484
|
const makeChildFromRefAtom = <Child extends Machine.ChildMachine.Any, StartError>(
|
|
307
485
|
ref: Atom.Atom<AsyncResult.AsyncResult<Option.Option<Machine.ChildMachine.Ref<Child>>, StartError>>,
|
|
308
|
-
descriptor: Child
|
|
486
|
+
descriptor: Child,
|
|
487
|
+
plan: ChildServerPlan
|
|
309
488
|
): ChildMachineAtom<Child, StartError> => {
|
|
310
489
|
type Ref = Machine.ChildMachine.Ref<Child>
|
|
311
490
|
type State = RefState<Ref>
|
|
@@ -336,49 +515,38 @@ const makeChildFromRefAtom = <Child extends Machine.ChildMachine.Any, StartError
|
|
|
336
515
|
)
|
|
337
516
|
get.addFinalizer(cancel)
|
|
338
517
|
return AsyncResult.success(Option.some(Effect.runSync(handle.snapshot)))
|
|
339
|
-
})
|
|
518
|
+
}).pipe(
|
|
519
|
+
Atom.withServerValue(stableServerValue(() => [], (): AsyncResult.AsyncResult<
|
|
520
|
+
Option.Option<Machine.RuntimeSnapshot<State, Error, Output>>,
|
|
521
|
+
StartError
|
|
522
|
+
> => {
|
|
523
|
+
const planned = plan()
|
|
524
|
+
return planned === undefined
|
|
525
|
+
? AsyncResult.initial(true)
|
|
526
|
+
: AsyncResult.success(Option.map(planned, plannedSnapshot<State, Error, Output>), { waiting: true })
|
|
527
|
+
}))
|
|
528
|
+
)
|
|
340
529
|
|
|
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
|
-
}
|
|
530
|
+
const commands: CommandQueues = new WeakMap()
|
|
531
|
+
const activeChild = (current: Option.Option<Ref>): Effect.Effect<Ref, ChildNotActiveError> =>
|
|
532
|
+
Option.isSome(current)
|
|
533
|
+
? Effect.succeed(current.value)
|
|
534
|
+
: Effect.fail(new ChildNotActiveError({ id: descriptor.id }))
|
|
535
|
+
|
|
536
|
+
const send: ChildMachineAtom<Child, StartError>["send"] = makeCommandAtom(
|
|
537
|
+
ref,
|
|
538
|
+
commands,
|
|
539
|
+
(current, event: Machine.ChildMachine.Event<Child>) =>
|
|
540
|
+
Effect.flatMap(activeChild(current), (child) => child.send(event as never))
|
|
360
541
|
)
|
|
361
542
|
|
|
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
|
-
}
|
|
543
|
+
const stop: ChildMachineAtom<Child, StartError>["stop"] = makeCommandAtom(
|
|
544
|
+
ref,
|
|
545
|
+
commands,
|
|
546
|
+
(current, _: void) => Effect.flatMap(activeChild(current), (child) => child.stop)
|
|
379
547
|
)
|
|
380
548
|
|
|
381
|
-
const child = makeChildSelector<StartError>(ref as any)
|
|
549
|
+
const child = makeChildSelector<StartError>(ref as any, plan)
|
|
382
550
|
|
|
383
551
|
return {
|
|
384
552
|
ref,
|
|
@@ -393,7 +561,8 @@ const makeChildFromRefAtom = <Child extends Machine.ChildMachine.Any, StartError
|
|
|
393
561
|
const makeChildSelector = <StartError>(
|
|
394
562
|
parentRef: Atom.Atom<
|
|
395
563
|
AsyncResult.AsyncResult<Option.Option<Machine.MachineRef<any, any, any, any, any>>, StartError>
|
|
396
|
-
|
|
564
|
+
>,
|
|
565
|
+
parentPlan: ChildServerPlan
|
|
397
566
|
) => {
|
|
398
567
|
const byMachine = new WeakMap<object, (id: string) => ChildMachineAtom<Machine.ChildMachine.Any, StartError>>()
|
|
399
568
|
return <Child extends Machine.ChildMachine.Any>(descriptor: Child): ChildMachineAtom<Child, StartError> => {
|
|
@@ -404,7 +573,13 @@ const makeChildSelector = <StartError>(
|
|
|
404
573
|
const child = internalMachine.child(id, machine)
|
|
405
574
|
return makeChildFromRefAtom(
|
|
406
575
|
makeChildRefAtom(parentRef as any, child),
|
|
407
|
-
child
|
|
576
|
+
child,
|
|
577
|
+
lazyPlan(() => {
|
|
578
|
+
const parent = parentPlan()
|
|
579
|
+
return parent === undefined || Option.isNone(parent)
|
|
580
|
+
? parent
|
|
581
|
+
: PlannedSnapshot.planInvokedChild(parent.value, child)
|
|
582
|
+
})
|
|
408
583
|
)
|
|
409
584
|
})
|
|
410
585
|
family = (id) => atoms(id) as ChildMachineAtom<Machine.ChildMachine.Any, StartError>
|
|
@@ -415,9 +590,11 @@ const makeChildSelector = <StartError>(
|
|
|
415
590
|
}
|
|
416
591
|
|
|
417
592
|
const makeFromRefAtom = <State, Event, Error, Output, StartError, Emitted>(
|
|
418
|
-
|
|
419
|
-
machine: Machine.Machine.Any
|
|
593
|
+
liveRef: Atom.Atom<AsyncResult.AsyncResult<Machine.MachineRef<State, Event, Error, Output, Emitted>, StartError>>,
|
|
594
|
+
machine: Machine.Machine.Any,
|
|
595
|
+
plan: ServerPlan
|
|
420
596
|
): MachineAtom<State, Event, Error, Output, StartError, Emitted> => {
|
|
597
|
+
const ref = Atom.withServerValueInitial(liveRef)
|
|
421
598
|
const snapshot = Atom.readable((
|
|
422
599
|
get
|
|
423
600
|
): AsyncResult.AsyncResult<Machine.RuntimeSnapshot<State, Error, Output>, StartError> => {
|
|
@@ -451,46 +628,39 @@ const makeFromRefAtom = <State, Event, Error, Output, StartError, Emitted>(
|
|
|
451
628
|
return AsyncResult.success(current, {
|
|
452
629
|
waiting: current.status === "active"
|
|
453
630
|
})
|
|
454
|
-
})
|
|
631
|
+
}).pipe(
|
|
632
|
+
Atom.withServerValue(
|
|
633
|
+
stableServerValue(
|
|
634
|
+
() => [],
|
|
635
|
+
(): AsyncResult.AsyncResult<Machine.RuntimeSnapshot<State, Error, Output>, StartError> => {
|
|
636
|
+
const planned = plan()
|
|
637
|
+
return planned === undefined
|
|
638
|
+
? AsyncResult.initial(true)
|
|
639
|
+
: AsyncResult.success(plannedSnapshot<State, Error, Output>(planned), { waiting: true })
|
|
640
|
+
}
|
|
641
|
+
)
|
|
642
|
+
)
|
|
643
|
+
)
|
|
455
644
|
|
|
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
|
-
}
|
|
645
|
+
const commands: CommandQueues = new WeakMap()
|
|
646
|
+
|
|
647
|
+
const send: MachineAtom<State, Event, Error, Output, StartError, Emitted>["send"] = makeCommandAtom(
|
|
648
|
+
ref,
|
|
649
|
+
commands,
|
|
650
|
+
(handle, event: Event) => handle.send(event)
|
|
476
651
|
)
|
|
477
652
|
|
|
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
|
-
}
|
|
653
|
+
const stop: MachineAtom<State, Event, Error, Output, StartError, Emitted>["stop"] = makeCommandAtom(
|
|
654
|
+
ref,
|
|
655
|
+
commands,
|
|
656
|
+
(handle, _: void) => handle.stop
|
|
490
657
|
)
|
|
491
658
|
|
|
492
659
|
const optionalRef = Atom.mapResult(ref, Option.some)
|
|
493
|
-
const child = makeChildSelector<StartError>(optionalRef as any)
|
|
660
|
+
const child = makeChildSelector<StartError>(optionalRef as any, () => {
|
|
661
|
+
const planned = plan()
|
|
662
|
+
return planned === undefined ? undefined : Option.some(planned)
|
|
663
|
+
})
|
|
494
664
|
|
|
495
665
|
const result = {
|
|
496
666
|
ref,
|
|
@@ -560,6 +730,18 @@ type SelectorKind =
|
|
|
560
730
|
| "selectSnapshot"
|
|
561
731
|
| "selectSnapshotChild"
|
|
562
732
|
|
|
733
|
+
// Selectors project the bridge result live and its planned result on the server.
|
|
734
|
+
const mapResultSelector = <A, B, E>(
|
|
735
|
+
source: Atom.Atom<AsyncResult.AsyncResult<A, E>>,
|
|
736
|
+
f: (value: A) => B
|
|
737
|
+
): Atom.Atom<AsyncResult.AsyncResult<B, E>> =>
|
|
738
|
+
Atom.mapResult(source, f).pipe(
|
|
739
|
+
Atom.withEquality(Equal.equals),
|
|
740
|
+
Atom.withServerValue(
|
|
741
|
+
stableServerValue((get) => [readServerValue(source, get)], (current) => AsyncResult.map(current, f))
|
|
742
|
+
)
|
|
743
|
+
)
|
|
744
|
+
|
|
563
745
|
const selectorsByBridge = new WeakMap<object, Map<SelectorKind, Map<string, Atom.Atom<any>>>>()
|
|
564
746
|
|
|
565
747
|
const cachedSelector = <A>(
|
|
@@ -605,10 +787,7 @@ export const select = <
|
|
|
605
787
|
self,
|
|
606
788
|
"select",
|
|
607
789
|
path,
|
|
608
|
-
() =>
|
|
609
|
-
Atom.mapResult(self.result, (snapshot) => selectValueByPath(snapshot, path)).pipe(
|
|
610
|
-
Atom.withEquality(Equal.equals)
|
|
611
|
-
)
|
|
790
|
+
() => mapResultSelector(self.result, (snapshot) => selectValueByPath(snapshot, path))
|
|
612
791
|
)
|
|
613
792
|
|
|
614
793
|
export const selectSnapshot = <
|
|
@@ -629,10 +808,7 @@ export const selectSnapshot = <
|
|
|
629
808
|
self,
|
|
630
809
|
"selectSnapshot",
|
|
631
810
|
path,
|
|
632
|
-
() =>
|
|
633
|
-
Atom.mapResult(self.result, (snapshot) => selectSnapshotByPath(snapshot, path)).pipe(
|
|
634
|
-
Atom.withEquality(Equal.equals)
|
|
635
|
-
)
|
|
811
|
+
() => mapResultSelector(self.result, (snapshot) => selectSnapshotByPath(snapshot, path))
|
|
636
812
|
)
|
|
637
813
|
|
|
638
814
|
export const selectChild = <
|
|
@@ -649,10 +825,10 @@ export const selectChild = <
|
|
|
649
825
|
>
|
|
650
826
|
> =>
|
|
651
827
|
cachedSelector(self, "selectChild", path, () =>
|
|
652
|
-
|
|
828
|
+
mapResultSelector(
|
|
653
829
|
self.result,
|
|
654
830
|
Option.flatMap((snapshot) => selectValueByPath(snapshot, path))
|
|
655
|
-
)
|
|
831
|
+
))
|
|
656
832
|
|
|
657
833
|
export const selectSnapshotChild = <
|
|
658
834
|
Child extends Machine.ChildMachine.Any,
|
|
@@ -668,10 +844,10 @@ export const selectSnapshotChild = <
|
|
|
668
844
|
>
|
|
669
845
|
> =>
|
|
670
846
|
cachedSelector(self, "selectSnapshotChild", path, () =>
|
|
671
|
-
|
|
847
|
+
mapResultSelector(
|
|
672
848
|
self.result,
|
|
673
849
|
Option.flatMap((snapshot) => selectSnapshotByPath(snapshot, path))
|
|
674
|
-
)
|
|
850
|
+
))
|
|
675
851
|
|
|
676
852
|
export const matches = <
|
|
677
853
|
State extends Machine.Machine.AtomicSnapshot<string, unknown>,
|
|
@@ -689,10 +865,7 @@ export const matches = <
|
|
|
689
865
|
self,
|
|
690
866
|
"matches",
|
|
691
867
|
path,
|
|
692
|
-
() =>
|
|
693
|
-
Atom.mapResult(self.result, (snapshot) => Option.isSome(Topology.getSnapshotByPath(snapshot, path))).pipe(
|
|
694
|
-
Atom.withEquality(Equal.equals)
|
|
695
|
-
)
|
|
868
|
+
() => mapResultSelector(self.result, (snapshot) => Option.isSome(Topology.getSnapshotByPath(snapshot, path)))
|
|
696
869
|
)
|
|
697
870
|
|
|
698
871
|
export const matchesChild = <
|
|
@@ -706,10 +879,10 @@ export const matchesChild = <
|
|
|
706
879
|
AsyncResult.AsyncResult<boolean, StartError | RefError<Machine.ChildMachine.Ref<Child>>>
|
|
707
880
|
> =>
|
|
708
881
|
cachedSelector(self, "matchesChild", path, () =>
|
|
709
|
-
|
|
882
|
+
mapResultSelector(
|
|
710
883
|
self.result,
|
|
711
884
|
Option.exists((snapshot) => Option.isSome(Topology.getSnapshotByPath(snapshot, path)))
|
|
712
|
-
)
|
|
885
|
+
))
|
|
713
886
|
|
|
714
887
|
export const can = (event: unknown) => {
|
|
715
888
|
const byBridge = new WeakMap<object, Atom.Atom<AsyncResult.AsyncResult<boolean, any>>>()
|
|
@@ -725,9 +898,11 @@ export const can = (event: unknown) => {
|
|
|
725
898
|
? () => Effect.die(new Error("AtomMachine.can requires a machine atom created by AtomMachine"))
|
|
726
899
|
: internalMachine.can(machine)
|
|
727
900
|
|
|
728
|
-
const
|
|
729
|
-
|
|
730
|
-
|
|
901
|
+
const evaluate = (
|
|
902
|
+
current: AsyncResult.AsyncResult<Machine.RuntimeSnapshot<any, any, any>, any>,
|
|
903
|
+
input: unknown,
|
|
904
|
+
previous: Option.Option<AsyncResult.AsyncResult<boolean, any>>
|
|
905
|
+
): AsyncResult.AsyncResult<boolean, any> => {
|
|
731
906
|
if (AsyncResult.isInitial(current)) {
|
|
732
907
|
return AsyncResult.initial(current.waiting)
|
|
733
908
|
} else if (AsyncResult.isFailure(current)) {
|
|
@@ -744,7 +919,6 @@ export const can = (event: unknown) => {
|
|
|
744
919
|
return AsyncResult.success(false, { waiting: current.waiting })
|
|
745
920
|
}
|
|
746
921
|
|
|
747
|
-
const input = Atom.isAtom(event) ? get(event) : event
|
|
748
922
|
const exit = Effect.runSyncExit(query(current.value.state, input))
|
|
749
923
|
return exit._tag === "Success"
|
|
750
924
|
? AsyncResult.success(exit.value, { waiting: current.waiting })
|
|
@@ -752,7 +926,23 @@ export const can = (event: unknown) => {
|
|
|
752
926
|
previous,
|
|
753
927
|
waiting: current.waiting
|
|
754
928
|
})
|
|
755
|
-
}
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
const result = Atom.readable((get): AsyncResult.AsyncResult<boolean, any> =>
|
|
932
|
+
evaluate(
|
|
933
|
+
get(self.snapshot),
|
|
934
|
+
Atom.isAtom(event) ? get(event) : event,
|
|
935
|
+
get.self<AsyncResult.AsyncResult<boolean, any>>()
|
|
936
|
+
)
|
|
937
|
+
).pipe(
|
|
938
|
+
Atom.withEquality(Equal.equals),
|
|
939
|
+
Atom.withServerValue(
|
|
940
|
+
stableServerValue(
|
|
941
|
+
(get) => [readServerValue(self.snapshot, get), Atom.isAtom(event) ? readServerValue(event, get) : event],
|
|
942
|
+
(current, input) => evaluate(current, input, Option.none())
|
|
943
|
+
)
|
|
944
|
+
)
|
|
945
|
+
)
|
|
756
946
|
|
|
757
947
|
byBridge.set(self, result)
|
|
758
948
|
return result
|
|
@@ -839,7 +1029,7 @@ export const make: {
|
|
|
839
1029
|
} = ((machine: Machine.Machine.Any, ...args: ReadonlyArray<unknown>) => {
|
|
840
1030
|
const prepared = Atom.make(() => internalMachine.prepare(machine, ...args))
|
|
841
1031
|
const ref = Atom.make((get) => startPreparedMachineAtomEffect(get, prepared as any))
|
|
842
|
-
const result = makeFromRefAtom(ref as any, machine)
|
|
1032
|
+
const result = makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planStart(machine, args)))
|
|
843
1033
|
preparedByMachineAtom.set(result, prepared as any)
|
|
844
1034
|
return result
|
|
845
1035
|
}) as any
|
|
@@ -858,7 +1048,7 @@ export const resume: {
|
|
|
858
1048
|
): ResumedMachineAtomOf<M, never>
|
|
859
1049
|
} = ((machine: Machine.Machine.Any, snapshot: Machine.Machine.Snapshot<any>) => {
|
|
860
1050
|
const ref = Atom.make((get) => resumeMachineAtomEffect(get, machine, snapshot))
|
|
861
|
-
return makeFromRefAtom(ref as any, machine)
|
|
1051
|
+
return makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planResume(machine, snapshot)))
|
|
862
1052
|
}) as any
|
|
863
1053
|
|
|
864
1054
|
const makeWithRuntime = (
|
|
@@ -868,7 +1058,7 @@ const makeWithRuntime = (
|
|
|
868
1058
|
): MachineAtom<any, any, any, any, any, any> => {
|
|
869
1059
|
const prepared = runtime.atom(() => internalMachine.prepare(machine, ...args))
|
|
870
1060
|
const ref = runtime.atom((get) => startPreparedMachineAtomEffect(get, prepared as any))
|
|
871
|
-
const result = makeFromRefAtom(ref as any, machine)
|
|
1061
|
+
const result = makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planStart(machine, args)))
|
|
872
1062
|
preparedByMachineAtom.set(result, prepared as any)
|
|
873
1063
|
return result
|
|
874
1064
|
}
|
|
@@ -879,10 +1069,10 @@ const resumeWithRuntime = (
|
|
|
879
1069
|
snapshot: Machine.Machine.Snapshot<any>
|
|
880
1070
|
): MachineAtom<any, any, any, any, any, any> => {
|
|
881
1071
|
const ref = runtime.atom((get) => resumeMachineAtomEffect(get, machine, snapshot))
|
|
882
|
-
return makeFromRefAtom(ref as any, machine)
|
|
1072
|
+
return makeFromRefAtom(ref as any, machine, lazyPlan(() => PlannedSnapshot.planResume(machine, snapshot)))
|
|
883
1073
|
}
|
|
884
1074
|
|
|
885
|
-
type FamilyBridge = MachineAtom
|
|
1075
|
+
type FamilyBridge = MachineAtom.Any | ChildMachineAtom.Any
|
|
886
1076
|
|
|
887
1077
|
type FamilyOptions = {
|
|
888
1078
|
readonly atoms: Readonly<Record<string, (bridge: FamilyBridge) => Atom.Atom<any>>>
|
|
@@ -900,7 +1090,9 @@ const retainFamilyOwner = <Source extends Atom.Atom<any>>(
|
|
|
900
1090
|
(get) => get(retained.source),
|
|
901
1091
|
{ initialValueTarget: source }
|
|
902
1092
|
).pipe(
|
|
903
|
-
Atom.withEquality((value, next) => retained.source.equals(value, next))
|
|
1093
|
+
Atom.withEquality((value, next) => retained.source.equals(value, next)),
|
|
1094
|
+
// The source's server value is already stable, so it passes through as is.
|
|
1095
|
+
Atom.withServerValue((get) => readServerValue(retained.source, get))
|
|
904
1096
|
)
|
|
905
1097
|
if (label !== undefined) {
|
|
906
1098
|
atom = atom.pipe(Atom.withLabel(label))
|
|
@@ -937,7 +1129,7 @@ export const family = (
|
|
|
937
1129
|
)
|
|
938
1130
|
|
|
939
1131
|
export const familyChild = (
|
|
940
|
-
parent:
|
|
1132
|
+
parent: FamilyBridge,
|
|
941
1133
|
options: FamilyOptions & { readonly child: (key: any) => Machine.ChildMachine.Any }
|
|
942
1134
|
): Readonly<Record<string, (key: any) => Atom.Atom<any>>> =>
|
|
943
1135
|
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
|
|