@typeonce/effect-machine 0.33.0 → 0.35.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 +272 -628
- package/dist/Machine.d.ts +1076 -896
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +141 -90
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/childRegistry.d.ts +1 -1
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/cluster.d.ts +2 -50
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +4 -27
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts +35 -0
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
- package/dist/internal/machine/clusterProtocol.js +28 -0
- package/dist/internal/machine/clusterProtocol.js.map +1 -0
- package/dist/internal/machine/commandRuntime.d.ts +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/configuration.d.ts +2 -2
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -64
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +17 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -0
- package/dist/internal/machine/declaration.js +120 -0
- package/dist/internal/machine/declaration.js.map +1 -0
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +15 -22
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +34 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -0
- package/dist/internal/machine/implementation.js +3 -0
- package/dist/internal/machine/implementation.js.map +1 -0
- package/dist/internal/machine/initialDeclaration.d.ts +14 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
- package/dist/internal/machine/initialDeclaration.js +137 -0
- package/dist/internal/machine/initialDeclaration.js.map +1 -0
- package/dist/internal/machine/invocation.d.ts +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +1 -1
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
- package/dist/internal/machine/machine.d.ts +13 -4
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +169 -388
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +8 -0
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +37 -78
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +8 -7
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +16 -12
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +2 -230
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +8 -1857
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
- package/dist/internal/machine/runtimeCompiled.js +867 -0
- package/dist/internal/machine/runtimeCompiled.js.map +1 -0
- package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
- package/dist/internal/machine/runtimeGeneric.js +471 -0
- package/dist/internal/machine/runtimeGeneric.js.map +1 -0
- package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
- package/dist/internal/machine/runtimeProtocol.js +561 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -0
- package/dist/internal/machine/stateDefinition.d.ts +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +1 -13
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +41 -0
- package/dist/internal/machine/targetReference.js.map +1 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +5 -3
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +53 -46
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +7 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +105 -73
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +105 -73
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +18 -13
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +18 -25
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +41 -24
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +62 -62
- package/package.json +1 -1
- package/src/Machine.ts +1836 -2907
- package/src/internal/machine/childRegistry.ts +1 -1
- package/src/internal/machine/cluster.ts +7 -73
- package/src/internal/machine/clusterProtocol.ts +56 -0
- package/src/internal/machine/commandRuntime.ts +1 -1
- package/src/internal/machine/configuration.ts +53 -65
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +16 -22
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +225 -576
- package/src/internal/machine/planner.ts +84 -118
- package/src/internal/machine/process.ts +40 -33
- package/src/internal/machine/protocol.ts +3 -3
- package/src/internal/machine/runtime.ts +20 -3037
- package/src/internal/machine/runtimeCompiled.ts +1166 -0
- package/src/internal/machine/runtimeGeneric.ts +799 -0
- package/src/internal/machine/runtimeProtocol.ts +1213 -0
- package/src/internal/machine/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +55 -0
- package/src/internal/machine/topology.ts +5 -3
- package/src/internal/testing/machine/finiteModel.ts +58 -53
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +22 -25
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
|
@@ -0,0 +1,799 @@
|
|
|
1
|
+
/** General Effect process worker and supervisor strategy. */
|
|
2
|
+
import * as Cause from "effect/Cause"
|
|
3
|
+
import * as Channel from "effect/Channel"
|
|
4
|
+
import * as Deferred from "effect/Deferred"
|
|
5
|
+
import * as Effect from "effect/Effect"
|
|
6
|
+
import * as Exit from "effect/Exit"
|
|
7
|
+
import * as Fiber from "effect/Fiber"
|
|
8
|
+
import * as Option from "effect/Option"
|
|
9
|
+
import * as PubSub from "effect/PubSub"
|
|
10
|
+
import * as Queue from "effect/Queue"
|
|
11
|
+
import * as Stream from "effect/Stream"
|
|
12
|
+
import * as SynchronizedRef from "effect/SynchronizedRef"
|
|
13
|
+
import type * as Take from "effect/Take"
|
|
14
|
+
import type { Inspection } from "../../Machine.js"
|
|
15
|
+
import { type ChildSelector } from "./childRegistry.js"
|
|
16
|
+
import { StoppedError } from "./errors.js"
|
|
17
|
+
import * as InspectionRuntime from "./inspectionRuntime.js"
|
|
18
|
+
import {
|
|
19
|
+
type AcknowledgedDelivery,
|
|
20
|
+
AcknowledgedMessageTypeId,
|
|
21
|
+
acknowledgedSend,
|
|
22
|
+
childlessRuntime,
|
|
23
|
+
classifyOutcome,
|
|
24
|
+
executionIsChildless,
|
|
25
|
+
failAcknowledgedMessage,
|
|
26
|
+
type InspectedOffer,
|
|
27
|
+
inspectionSubject,
|
|
28
|
+
isAcknowledgedMessage,
|
|
29
|
+
isMachineTarget,
|
|
30
|
+
type MachineRef,
|
|
31
|
+
makeChildRuntime,
|
|
32
|
+
makeEmissionRuntime,
|
|
33
|
+
makeInspectedMessage,
|
|
34
|
+
messageCausation,
|
|
35
|
+
messageEvent,
|
|
36
|
+
noCausation,
|
|
37
|
+
noInspectInitial,
|
|
38
|
+
noParentSend,
|
|
39
|
+
notifyActiveSnapshot,
|
|
40
|
+
type ProcessAddress,
|
|
41
|
+
type ProcessContext,
|
|
42
|
+
type ProcessLogic,
|
|
43
|
+
type ProcessMessage,
|
|
44
|
+
type ProcessScope,
|
|
45
|
+
publishInspectedSent,
|
|
46
|
+
type RuntimeSnapshot,
|
|
47
|
+
sendMachineTarget,
|
|
48
|
+
type StartInternalOptions,
|
|
49
|
+
type StartProcess,
|
|
50
|
+
stopAcknowledgedMessage,
|
|
51
|
+
succeedAcknowledgedMessage,
|
|
52
|
+
type VersionedSnapshot
|
|
53
|
+
} from "./runtimeProtocol.js"
|
|
54
|
+
|
|
55
|
+
// `Machine.logic` permits an arbitrary Effect program, including programs that
|
|
56
|
+
// suspend or supervise their own fibers. Keep its two-fiber worker/supervisor
|
|
57
|
+
// protocol as the general contract rather than weakening it for statecharts.
|
|
58
|
+
export const startGenericInternal: StartProcess = Effect.fnUntraced(
|
|
59
|
+
function*<State, Event, Error, Requirements, Output, InitialError>(
|
|
60
|
+
logic: ProcessLogic<State, Event, Error, Requirements, Output, InitialError>,
|
|
61
|
+
options: StartInternalOptions
|
|
62
|
+
) {
|
|
63
|
+
const {
|
|
64
|
+
detached,
|
|
65
|
+
id: requestedId,
|
|
66
|
+
onOutcome,
|
|
67
|
+
onReady,
|
|
68
|
+
onReadySync,
|
|
69
|
+
onSnapshot,
|
|
70
|
+
onStop,
|
|
71
|
+
onStopSync,
|
|
72
|
+
parent,
|
|
73
|
+
runtime,
|
|
74
|
+
sendParent: overrideSendParent
|
|
75
|
+
} = options
|
|
76
|
+
type ProcessTermination =
|
|
77
|
+
| { readonly _tag: "Stopped" }
|
|
78
|
+
| { readonly _tag: "Done"; readonly output: Output }
|
|
79
|
+
| { readonly _tag: "Failure"; readonly cause: Cause.Cause<Error> }
|
|
80
|
+
|
|
81
|
+
const sessionId = options.sessionId ?? (yield* runtime.nextSessionId)
|
|
82
|
+
const id = requestedId ?? sessionId
|
|
83
|
+
const inspector = runtime.inspection
|
|
84
|
+
const subject = inspector === undefined ? undefined : inspectionSubject(logic, id, sessionId)
|
|
85
|
+
const activity: Inspection.Activity | undefined = inspector === undefined || options.activity === undefined
|
|
86
|
+
? undefined
|
|
87
|
+
: { ...options.activity, sessionId }
|
|
88
|
+
let initialEntryPaths: ReadonlyArray<string> | undefined
|
|
89
|
+
let initialMicrosteps: ReadonlyArray<unknown> | undefined
|
|
90
|
+
const queue = yield* Queue.unbounded<ProcessMessage<Event>>()
|
|
91
|
+
const emissions = options.emissions ?? makeEmissionRuntime()
|
|
92
|
+
const termination = yield* Deferred.make<ProcessTermination>()
|
|
93
|
+
const done = yield* Deferred.make<Output, Error | StoppedError>()
|
|
94
|
+
const awaitCompletion = Deferred.await(done).pipe(Effect.exit, Effect.asVoid)
|
|
95
|
+
let initializing = true
|
|
96
|
+
let inFlightMessage: ProcessMessage<Event> | undefined
|
|
97
|
+
const requestStop = Deferred.succeed(termination, { _tag: "Stopped" }).pipe(Effect.asVoid)
|
|
98
|
+
const offerDirect = (message: ProcessMessage<Event>): Effect.Effect<void, StoppedError> =>
|
|
99
|
+
Queue.offer(queue, message).pipe(
|
|
100
|
+
Effect.flatMap((accepted) => accepted ? Effect.void : Effect.fail(new StoppedError()))
|
|
101
|
+
)
|
|
102
|
+
const offerInspected: InspectedOffer<Event> | undefined = inspector === undefined ? undefined : (
|
|
103
|
+
event,
|
|
104
|
+
source,
|
|
105
|
+
causedBy,
|
|
106
|
+
deferred?: Deferred.Deferred<AcknowledgedDelivery<unknown>, unknown>
|
|
107
|
+
) =>
|
|
108
|
+
Effect.suspend(() => {
|
|
109
|
+
const message: ProcessMessage<Event> = inspector.isActive()
|
|
110
|
+
? makeInspectedMessage(inspector, subject!, event, source, causedBy, deferred)
|
|
111
|
+
: deferred === undefined
|
|
112
|
+
? event
|
|
113
|
+
: { [AcknowledgedMessageTypeId]: true as const, event, deferred }
|
|
114
|
+
return offerDirect(message).pipe(
|
|
115
|
+
Effect.tap(() => Effect.sync(() => publishInspectedSent(inspector, subject!, message)))
|
|
116
|
+
)
|
|
117
|
+
})
|
|
118
|
+
const sendAcknowledged:
|
|
119
|
+
| ((event: Event) => Effect.Effect<AcknowledgedDelivery<State>, Error | StoppedError>)
|
|
120
|
+
| undefined = logic.execution?._tag !== "Compiled"
|
|
121
|
+
? undefined
|
|
122
|
+
: (event) =>
|
|
123
|
+
Effect.uninterruptibleMask((restore) =>
|
|
124
|
+
Deferred.make<AcknowledgedDelivery<unknown>, unknown>().pipe(
|
|
125
|
+
Effect.flatMap((deferred) => {
|
|
126
|
+
const offered = inspector === undefined
|
|
127
|
+
? offerDirect({ [AcknowledgedMessageTypeId]: true as const, event, deferred })
|
|
128
|
+
: offerInspected!(event, undefined, undefined, deferred)
|
|
129
|
+
return offered.pipe(Effect.andThen(restore(Deferred.await(deferred))))
|
|
130
|
+
}),
|
|
131
|
+
Effect.map((delivery) => delivery as AcknowledgedDelivery<State>)
|
|
132
|
+
)
|
|
133
|
+
) as Effect.Effect<AcknowledgedDelivery<State>, Error | StoppedError>
|
|
134
|
+
|
|
135
|
+
const self: ProcessAddress<Event> = {
|
|
136
|
+
id,
|
|
137
|
+
sessionId,
|
|
138
|
+
// Initialization must finish constructing a state before a stopped
|
|
139
|
+
// snapshot can be published. A stop requested there is therefore recorded
|
|
140
|
+
// and returns so initialization can finish. Once running, the requesting
|
|
141
|
+
// process waits forever and is interrupted by the supervisor after the
|
|
142
|
+
// stop request wins, so execution never continues after `self.stop`.
|
|
143
|
+
stop: Effect.suspend(() =>
|
|
144
|
+
initializing
|
|
145
|
+
? requestStop
|
|
146
|
+
: requestStop.pipe(Effect.andThen(Effect.never))
|
|
147
|
+
),
|
|
148
|
+
send: inspector === undefined
|
|
149
|
+
? (event) => offerDirect(event)
|
|
150
|
+
: (event) => offerInspected!(event, undefined, undefined),
|
|
151
|
+
...(inspector === undefined
|
|
152
|
+
? undefined
|
|
153
|
+
: { inspectionSubject: subject!, sendInspected: offerInspected! })
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let {
|
|
157
|
+
changes: childChanges,
|
|
158
|
+
close: closeChildren,
|
|
159
|
+
get: getChild,
|
|
160
|
+
owned: ownedChildren,
|
|
161
|
+
sendTo,
|
|
162
|
+
spawn,
|
|
163
|
+
stop: stopChild
|
|
164
|
+
} = childlessRuntime
|
|
165
|
+
if (!executionIsChildless(logic.execution)) {
|
|
166
|
+
;({
|
|
167
|
+
changes: childChanges,
|
|
168
|
+
close: closeChildren,
|
|
169
|
+
get: getChild,
|
|
170
|
+
owned: ownedChildren,
|
|
171
|
+
sendTo,
|
|
172
|
+
spawn,
|
|
173
|
+
stop: stopChild
|
|
174
|
+
} = yield* makeChildRuntime(
|
|
175
|
+
self,
|
|
176
|
+
runtime,
|
|
177
|
+
undefined,
|
|
178
|
+
sendAcknowledged === undefined ? undefined : (event) => sendAcknowledged(event as Event)
|
|
179
|
+
))
|
|
180
|
+
}
|
|
181
|
+
const cleanupStartupFailure = <A, E>(exit: Exit.Exit<A, E>): Effect.Effect<void> => {
|
|
182
|
+
if (Exit.isSuccess(exit)) return Effect.void
|
|
183
|
+
if (inspector !== undefined) {
|
|
184
|
+
inspector.publishUnsafe(
|
|
185
|
+
activity === undefined
|
|
186
|
+
? { _tag: "StartFailed", subject: subject!, cause: exit.cause }
|
|
187
|
+
: { _tag: "ActivityStopped", subject: activity.owner, activity, exit }
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
return closeChildren(exit).pipe(
|
|
191
|
+
Effect.andThen(emissions.close()),
|
|
192
|
+
Effect.andThen(options.inspectionRoot === true && inspector !== undefined ? inspector.close : Effect.void)
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
const cleanup = onStopSync === undefined ? onStop ?? Effect.void : Effect.sync(onStopSync)
|
|
196
|
+
const currentCausation = inspector === undefined
|
|
197
|
+
? noCausation
|
|
198
|
+
: (): Inspection.Causation | undefined => messageCausation(inFlightMessage, initializing)
|
|
199
|
+
const sendParent = overrideSendParent ?? (parent === undefined
|
|
200
|
+
? noParentSend
|
|
201
|
+
: inspector === undefined
|
|
202
|
+
? parent.send
|
|
203
|
+
: (event) => sendMachineTarget(parent, event, subject, currentCausation()))
|
|
204
|
+
const emit = inspector === undefined
|
|
205
|
+
? emissions.emit
|
|
206
|
+
: (event: unknown) =>
|
|
207
|
+
emissions.emit(event).pipe(
|
|
208
|
+
Effect.tap(() =>
|
|
209
|
+
Effect.sync(() =>
|
|
210
|
+
inspector.publishUnsafe({
|
|
211
|
+
_tag: "Emitted",
|
|
212
|
+
subject: subject!,
|
|
213
|
+
emission: event,
|
|
214
|
+
causedBy: currentCausation()
|
|
215
|
+
})
|
|
216
|
+
)
|
|
217
|
+
)
|
|
218
|
+
)
|
|
219
|
+
const sendToTarget: ProcessScope<Event>["sendTo"] = inspector === undefined
|
|
220
|
+
? ((target: unknown, event: unknown) =>
|
|
221
|
+
isMachineTarget(target) ? target.send(event) : sendTo(target as ChildSelector, event)) as ProcessScope<
|
|
222
|
+
Event
|
|
223
|
+
>["sendTo"]
|
|
224
|
+
: ((target: unknown, event: unknown) =>
|
|
225
|
+
isMachineTarget(target)
|
|
226
|
+
? sendMachineTarget(target, event, subject, currentCausation())
|
|
227
|
+
: sendTo(target as ChildSelector, event, subject, currentCausation())) as ProcessScope<Event>["sendTo"]
|
|
228
|
+
|
|
229
|
+
const scope: ProcessScope<Event> = {
|
|
230
|
+
self,
|
|
231
|
+
parent,
|
|
232
|
+
spawn,
|
|
233
|
+
sendParent,
|
|
234
|
+
emit,
|
|
235
|
+
sendTo: sendToTarget,
|
|
236
|
+
stopChild,
|
|
237
|
+
failCause: (cause) =>
|
|
238
|
+
Deferred.succeed(termination, {
|
|
239
|
+
_tag: "Failure",
|
|
240
|
+
cause: cause as Cause.Cause<Error>
|
|
241
|
+
}).pipe(Effect.asVoid),
|
|
242
|
+
inspectInitial: inspector === undefined
|
|
243
|
+
? noInspectInitial
|
|
244
|
+
: (paths, microsteps = []) => {
|
|
245
|
+
initialEntryPaths = paths
|
|
246
|
+
initialMicrosteps = microsteps
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (inspector !== undefined) {
|
|
251
|
+
inspector.publishUnsafe(
|
|
252
|
+
activity === undefined
|
|
253
|
+
? {
|
|
254
|
+
_tag: "Created",
|
|
255
|
+
subject: subject!,
|
|
256
|
+
parent: parent?.inspectionSubject,
|
|
257
|
+
origin: options.origin ?? { _tag: "Root" },
|
|
258
|
+
definition: logic.inspection?.definition
|
|
259
|
+
}
|
|
260
|
+
: { _tag: "ActivityStarted", subject: activity.owner, activity }
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const initial = yield* logic.initial(scope).pipe(
|
|
265
|
+
Effect.onExit(cleanupStartupFailure),
|
|
266
|
+
Effect.ensuring(Effect.sync(() => {
|
|
267
|
+
initializing = false
|
|
268
|
+
}))
|
|
269
|
+
)
|
|
270
|
+
const current = yield* SynchronizedRef.make<VersionedSnapshot<State, Error, Output>>({
|
|
271
|
+
revision: 0,
|
|
272
|
+
terminalizing: false,
|
|
273
|
+
changes: undefined,
|
|
274
|
+
snapshot: {
|
|
275
|
+
status: "active",
|
|
276
|
+
state: initial
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
if (activity === undefined) {
|
|
280
|
+
inspector?.publishUnsafe({
|
|
281
|
+
_tag: "Initialized",
|
|
282
|
+
subject: subject!,
|
|
283
|
+
snapshot: { status: "active", state: initial },
|
|
284
|
+
initialEntryPaths: initialEntryPaths ?? [],
|
|
285
|
+
microsteps: InspectionRuntime.microsteps({ microsteps: initialMicrosteps ?? [] })
|
|
286
|
+
})
|
|
287
|
+
}
|
|
288
|
+
const publishSnapshot: (
|
|
289
|
+
snapshot: VersionedSnapshot<State, Error, Output>
|
|
290
|
+
) => Effect.Effect<VersionedSnapshot<State, Error, Output>> = onSnapshot === undefined
|
|
291
|
+
? (snapshot) =>
|
|
292
|
+
snapshot.changes === undefined
|
|
293
|
+
? Effect.succeed(snapshot)
|
|
294
|
+
: PubSub.publish(snapshot.changes, [snapshot] as const).pipe(Effect.as(snapshot))
|
|
295
|
+
: (snapshot) => {
|
|
296
|
+
const publish = snapshot.changes === undefined
|
|
297
|
+
? Effect.succeed(snapshot)
|
|
298
|
+
: PubSub.publish(snapshot.changes, [snapshot] as const).pipe(Effect.as(snapshot))
|
|
299
|
+
const runtimeSnapshot = snapshot.snapshot
|
|
300
|
+
return runtimeSnapshot.status !== "active"
|
|
301
|
+
? publish
|
|
302
|
+
: publish.pipe(Effect.tap(() => notifyActiveSnapshot(onSnapshot, runtimeSnapshot)))
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const completeChanges = (
|
|
306
|
+
snapshot: VersionedSnapshot<State, Error, Output>
|
|
307
|
+
): Effect.Effect<void> =>
|
|
308
|
+
snapshot.changes === undefined
|
|
309
|
+
? Effect.void
|
|
310
|
+
: PubSub.publish(snapshot.changes, Exit.succeed<void>(undefined)).pipe(Effect.asVoid)
|
|
311
|
+
|
|
312
|
+
const completeIfTerminal = (
|
|
313
|
+
snapshot: VersionedSnapshot<State, Error, Output>
|
|
314
|
+
): Effect.Effect<VersionedSnapshot<State, Error, Output>> => {
|
|
315
|
+
if (snapshot.snapshot.status === "active") {
|
|
316
|
+
return Effect.succeed(snapshot)
|
|
317
|
+
}
|
|
318
|
+
return completeChanges(snapshot).pipe(Effect.as(snapshot))
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const publishIfCurrent = (
|
|
322
|
+
snapshot: VersionedSnapshot<State, Error, Output>
|
|
323
|
+
): Effect.Effect<VersionedSnapshot<State, Error, Output> | undefined> =>
|
|
324
|
+
SynchronizedRef.get(current).pipe(
|
|
325
|
+
Effect.flatMap((
|
|
326
|
+
currentSnapshot
|
|
327
|
+
): Effect.Effect<VersionedSnapshot<State, Error, Output> | undefined> =>
|
|
328
|
+
currentSnapshot.revision === snapshot.revision
|
|
329
|
+
? publishSnapshot(snapshot).pipe(Effect.flatMap(completeIfTerminal))
|
|
330
|
+
: Effect.succeed(undefined)
|
|
331
|
+
)
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
type SnapshotModification = readonly [
|
|
335
|
+
VersionedSnapshot<State, Error, Output> | undefined,
|
|
336
|
+
VersionedSnapshot<State, Error, Output>
|
|
337
|
+
]
|
|
338
|
+
|
|
339
|
+
const updateSnapshot = <E2, R2>(
|
|
340
|
+
f: (
|
|
341
|
+
snapshot: RuntimeSnapshot<State, Error, Output>
|
|
342
|
+
) => Effect.Effect<RuntimeSnapshot<State, Error, Output> | undefined, E2, R2>
|
|
343
|
+
): Effect.Effect<RuntimeSnapshot<State, Error, Output> | undefined, E2, R2> =>
|
|
344
|
+
SynchronizedRef.modifyEffect(
|
|
345
|
+
current,
|
|
346
|
+
(current) =>
|
|
347
|
+
current.terminalizing
|
|
348
|
+
? Effect.succeed([undefined, current] as const)
|
|
349
|
+
: Effect.map(
|
|
350
|
+
f(current.snapshot),
|
|
351
|
+
(next) => {
|
|
352
|
+
if (next === undefined) {
|
|
353
|
+
return [undefined, current] as const
|
|
354
|
+
}
|
|
355
|
+
const versioned = {
|
|
356
|
+
revision: current.revision + 1,
|
|
357
|
+
snapshot: next,
|
|
358
|
+
terminalizing: false,
|
|
359
|
+
changes: current.changes
|
|
360
|
+
}
|
|
361
|
+
return [versioned, versioned] as const
|
|
362
|
+
}
|
|
363
|
+
)
|
|
364
|
+
).pipe(
|
|
365
|
+
Effect.flatMap((versioned) =>
|
|
366
|
+
versioned === undefined ? Effect.succeed(undefined) : publishIfCurrent(versioned)
|
|
367
|
+
),
|
|
368
|
+
Effect.map((published) => published?.snapshot)
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
const reserveTerminalSnapshot = (
|
|
372
|
+
f: (
|
|
373
|
+
snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "active" }>
|
|
374
|
+
) => RuntimeSnapshot<State, Error, Output>
|
|
375
|
+
): Effect.Effect<RuntimeSnapshot<State, Error, Output> | undefined> =>
|
|
376
|
+
SynchronizedRef.modify(
|
|
377
|
+
current,
|
|
378
|
+
(current): SnapshotModification => {
|
|
379
|
+
if (current.terminalizing || current.snapshot.status !== "active") {
|
|
380
|
+
return [undefined, current]
|
|
381
|
+
}
|
|
382
|
+
return [
|
|
383
|
+
{
|
|
384
|
+
revision: current.revision + 1,
|
|
385
|
+
snapshot: f(current.snapshot),
|
|
386
|
+
terminalizing: true,
|
|
387
|
+
changes: current.changes
|
|
388
|
+
},
|
|
389
|
+
{ ...current, terminalizing: true }
|
|
390
|
+
]
|
|
391
|
+
}
|
|
392
|
+
).pipe(Effect.map((versioned) => versioned?.snapshot))
|
|
393
|
+
|
|
394
|
+
const setAndPublishSnapshot = (
|
|
395
|
+
snapshot: RuntimeSnapshot<State, Error, Output>
|
|
396
|
+
): Effect.Effect<void> =>
|
|
397
|
+
SynchronizedRef.updateAndGet(current, (current) => ({
|
|
398
|
+
revision: current.revision + 1,
|
|
399
|
+
snapshot,
|
|
400
|
+
terminalizing: true,
|
|
401
|
+
changes: current.changes
|
|
402
|
+
})).pipe(
|
|
403
|
+
Effect.flatMap(publishSnapshot),
|
|
404
|
+
Effect.flatMap(completeIfTerminal),
|
|
405
|
+
Effect.asVoid
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
const setActiveStateDirect = (state: State) =>
|
|
409
|
+
updateSnapshot((snapshot) =>
|
|
410
|
+
Effect.succeed(
|
|
411
|
+
snapshot.status === "active"
|
|
412
|
+
? {
|
|
413
|
+
status: "active",
|
|
414
|
+
state
|
|
415
|
+
}
|
|
416
|
+
: undefined
|
|
417
|
+
)
|
|
418
|
+
).pipe(Effect.asVoid)
|
|
419
|
+
|
|
420
|
+
const setActiveState = inspector === undefined ?
|
|
421
|
+
setActiveStateDirect :
|
|
422
|
+
(state: State) =>
|
|
423
|
+
SynchronizedRef.get(current).pipe(
|
|
424
|
+
Effect.flatMap((before) =>
|
|
425
|
+
updateSnapshot((snapshot) =>
|
|
426
|
+
Effect.succeed(
|
|
427
|
+
snapshot.status === "active"
|
|
428
|
+
? {
|
|
429
|
+
status: "active",
|
|
430
|
+
state
|
|
431
|
+
}
|
|
432
|
+
: undefined
|
|
433
|
+
)
|
|
434
|
+
).pipe(
|
|
435
|
+
Effect.tap((after) =>
|
|
436
|
+
Effect.sync(() => {
|
|
437
|
+
if (logic.inspection?.kind === "Machine" || after === undefined) return
|
|
438
|
+
inspector.publishUnsafe({
|
|
439
|
+
_tag: "StateChanged",
|
|
440
|
+
subject: subject!,
|
|
441
|
+
before: before.snapshot.state,
|
|
442
|
+
after: state,
|
|
443
|
+
causedByDeliveryId: inFlightMessage !== undefined && isAcknowledgedMessage(inFlightMessage)
|
|
444
|
+
? inFlightMessage.inspection?.deliveryId
|
|
445
|
+
: undefined
|
|
446
|
+
})
|
|
447
|
+
})
|
|
448
|
+
)
|
|
449
|
+
)
|
|
450
|
+
),
|
|
451
|
+
Effect.asVoid
|
|
452
|
+
)
|
|
453
|
+
|
|
454
|
+
const terminalizeWith = (
|
|
455
|
+
snapshot: RuntimeSnapshot<State, Error, Output>,
|
|
456
|
+
exit: Exit.Exit<unknown, unknown>,
|
|
457
|
+
completeDone: Effect.Effect<void>
|
|
458
|
+
): Effect.Effect<void> => {
|
|
459
|
+
const notifyOutcome =
|
|
460
|
+
onOutcome === undefined || (snapshot.status === "stopped" && options.skipStoppedOutcome === true)
|
|
461
|
+
? Effect.void
|
|
462
|
+
: Effect.suspend(() => onOutcome(classifyOutcome(snapshot)!)).pipe(
|
|
463
|
+
Effect.exit,
|
|
464
|
+
Effect.asVoid
|
|
465
|
+
)
|
|
466
|
+
const closeEmissionsAndInspect = inspector === undefined
|
|
467
|
+
? emissions.close()
|
|
468
|
+
: emissions.close().pipe(
|
|
469
|
+
Effect.andThen(Effect.sync(() =>
|
|
470
|
+
inspector.publishUnsafe(
|
|
471
|
+
activity === undefined
|
|
472
|
+
? { _tag: "Terminated", subject: subject!, snapshot }
|
|
473
|
+
: {
|
|
474
|
+
_tag: "ActivityStopped",
|
|
475
|
+
subject: activity.owner,
|
|
476
|
+
activity,
|
|
477
|
+
exit: snapshot.status === "stopped" ? Exit.interrupt() : exit
|
|
478
|
+
}
|
|
479
|
+
)
|
|
480
|
+
))
|
|
481
|
+
)
|
|
482
|
+
return Effect.uninterruptible(
|
|
483
|
+
Effect.sync(() => {
|
|
484
|
+
while (true) {
|
|
485
|
+
const pending = Queue.takeUnsafe(queue)
|
|
486
|
+
if (pending === undefined || Exit.isFailure(pending)) break
|
|
487
|
+
stopAcknowledgedMessage(pending.value)
|
|
488
|
+
}
|
|
489
|
+
}).pipe(
|
|
490
|
+
Effect.andThen(Queue.shutdown(queue)),
|
|
491
|
+
Effect.andThen(closeChildren(exit)),
|
|
492
|
+
Effect.andThen(setAndPublishSnapshot(snapshot)),
|
|
493
|
+
Effect.andThen(closeEmissionsAndInspect),
|
|
494
|
+
Effect.andThen(Effect.sync(() => {
|
|
495
|
+
if (Exit.isFailure(exit)) {
|
|
496
|
+
failAcknowledgedMessage(inFlightMessage, exit.cause)
|
|
497
|
+
} else {
|
|
498
|
+
stopAcknowledgedMessage(inFlightMessage)
|
|
499
|
+
}
|
|
500
|
+
inFlightMessage = undefined
|
|
501
|
+
})),
|
|
502
|
+
Effect.andThen(notifyOutcome),
|
|
503
|
+
Effect.andThen(cleanup),
|
|
504
|
+
Effect.andThen(options.inspectionRoot === true && inspector !== undefined ? inspector.close : Effect.void),
|
|
505
|
+
Effect.andThen(completeDone)
|
|
506
|
+
)
|
|
507
|
+
)
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const reserveStoppedSnapshot = reserveTerminalSnapshot((snapshot) => ({
|
|
511
|
+
status: "stopped",
|
|
512
|
+
state: snapshot.state
|
|
513
|
+
}))
|
|
514
|
+
|
|
515
|
+
const reserveFailureSnapshot = (cause: Cause.Cause<Error>) =>
|
|
516
|
+
reserveTerminalSnapshot((snapshot) => ({
|
|
517
|
+
status: "error",
|
|
518
|
+
state: snapshot.state,
|
|
519
|
+
cause
|
|
520
|
+
}))
|
|
521
|
+
|
|
522
|
+
const reserveSuccessSnapshot = (output: Output) =>
|
|
523
|
+
reserveTerminalSnapshot((snapshot) => ({
|
|
524
|
+
status: "done",
|
|
525
|
+
state: snapshot.state,
|
|
526
|
+
output
|
|
527
|
+
}))
|
|
528
|
+
|
|
529
|
+
const terminalizeReservedStop = (
|
|
530
|
+
snapshot: RuntimeSnapshot<State, Error, Output>
|
|
531
|
+
): Effect.Effect<void> => {
|
|
532
|
+
const exit = Exit.void
|
|
533
|
+
return terminalizeWith(
|
|
534
|
+
snapshot,
|
|
535
|
+
exit,
|
|
536
|
+
Deferred.fail(done, new StoppedError())
|
|
537
|
+
)
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const terminalizeReservedFailure = (
|
|
541
|
+
snapshot: RuntimeSnapshot<State, Error, Output>,
|
|
542
|
+
cause: Cause.Cause<Error>
|
|
543
|
+
): Effect.Effect<void> => {
|
|
544
|
+
const exit = Exit.failCause(cause)
|
|
545
|
+
return terminalizeWith(snapshot, exit, Deferred.failCause(done, cause))
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
const terminalizeReservedSuccess = (
|
|
549
|
+
snapshot: RuntimeSnapshot<State, Error, Output>,
|
|
550
|
+
output: Output
|
|
551
|
+
): Effect.Effect<void> => {
|
|
552
|
+
const exit = Exit.succeed(output)
|
|
553
|
+
return terminalizeWith(snapshot, exit, Deferred.succeed(done, output))
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const stop: Effect.Effect<void> = Effect.uninterruptible(
|
|
557
|
+
requestStop.pipe(Effect.andThen(awaitCompletion))
|
|
558
|
+
)
|
|
559
|
+
|
|
560
|
+
const acknowledgedContext:
|
|
561
|
+
| Pick<
|
|
562
|
+
ProcessContext<State, Event>,
|
|
563
|
+
"receiveMessage" | "pollMessage" | "completeMessage"
|
|
564
|
+
>
|
|
565
|
+
| undefined = logic.execution?._tag !== "Compiled" ? undefined : {
|
|
566
|
+
receiveMessage: Queue.take(queue).pipe(
|
|
567
|
+
Effect.tap((message) =>
|
|
568
|
+
Effect.sync(() => {
|
|
569
|
+
inFlightMessage = isAcknowledgedMessage(message) ? message : undefined
|
|
570
|
+
})
|
|
571
|
+
)
|
|
572
|
+
),
|
|
573
|
+
pollMessage: Queue.poll(queue).pipe(
|
|
574
|
+
Effect.tap((message) =>
|
|
575
|
+
Effect.sync(() => {
|
|
576
|
+
if (Option.isSome(message)) {
|
|
577
|
+
inFlightMessage = isAcknowledgedMessage(message.value) ? message.value : undefined
|
|
578
|
+
}
|
|
579
|
+
})
|
|
580
|
+
)
|
|
581
|
+
),
|
|
582
|
+
completeMessage: (delivery) => {
|
|
583
|
+
succeedAcknowledgedMessage(inFlightMessage, delivery)
|
|
584
|
+
inFlightMessage = undefined
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
const receive = inspector === undefined || logic.execution?._tag === "Compiled"
|
|
588
|
+
? Queue.take(queue).pipe(Effect.map(messageEvent))
|
|
589
|
+
: Queue.take(queue).pipe(
|
|
590
|
+
Effect.tap((message) =>
|
|
591
|
+
Effect.sync(() => {
|
|
592
|
+
inFlightMessage = message
|
|
593
|
+
})
|
|
594
|
+
),
|
|
595
|
+
Effect.map(messageEvent)
|
|
596
|
+
)
|
|
597
|
+
const poll = inspector === undefined || logic.execution?._tag === "Compiled"
|
|
598
|
+
? Queue.poll(queue).pipe(Effect.map(Option.map(messageEvent)))
|
|
599
|
+
: Queue.poll(queue).pipe(
|
|
600
|
+
Effect.tap((message) =>
|
|
601
|
+
Effect.sync(() => {
|
|
602
|
+
if (Option.isSome(message)) inFlightMessage = message.value
|
|
603
|
+
})
|
|
604
|
+
),
|
|
605
|
+
Effect.map(Option.map(messageEvent))
|
|
606
|
+
)
|
|
607
|
+
const updateStateDirect = <E2, R2>(f: (state: State) => Effect.Effect<State, E2, R2>) =>
|
|
608
|
+
updateSnapshot((snapshot) =>
|
|
609
|
+
snapshot.status === "active"
|
|
610
|
+
? f(snapshot.state).pipe(
|
|
611
|
+
Effect.map((state) => ({
|
|
612
|
+
status: "active" as const,
|
|
613
|
+
state
|
|
614
|
+
}))
|
|
615
|
+
)
|
|
616
|
+
: Effect.succeed(undefined)
|
|
617
|
+
).pipe(Effect.asVoid)
|
|
618
|
+
const updateState: ProcessContext<State, Event>["updateState"] = inspector === undefined
|
|
619
|
+
? updateStateDirect
|
|
620
|
+
: (f) =>
|
|
621
|
+
SynchronizedRef.get(current).pipe(
|
|
622
|
+
Effect.flatMap((before) =>
|
|
623
|
+
updateSnapshot((snapshot) =>
|
|
624
|
+
snapshot.status === "active"
|
|
625
|
+
? f(snapshot.state).pipe(
|
|
626
|
+
Effect.map((state) => ({
|
|
627
|
+
status: "active" as const,
|
|
628
|
+
state
|
|
629
|
+
}))
|
|
630
|
+
)
|
|
631
|
+
: Effect.succeed(undefined)
|
|
632
|
+
).pipe(
|
|
633
|
+
Effect.tap((after) =>
|
|
634
|
+
Effect.sync(() => {
|
|
635
|
+
if (logic.inspection?.kind === "Machine" || after?.status !== "active") return
|
|
636
|
+
inspector.publishUnsafe({
|
|
637
|
+
_tag: "StateChanged",
|
|
638
|
+
subject: subject!,
|
|
639
|
+
before: before.snapshot.state,
|
|
640
|
+
after: after.state,
|
|
641
|
+
causedByDeliveryId: inFlightMessage !== undefined && isAcknowledgedMessage(inFlightMessage)
|
|
642
|
+
? inFlightMessage.inspection?.deliveryId
|
|
643
|
+
: undefined
|
|
644
|
+
})
|
|
645
|
+
})
|
|
646
|
+
)
|
|
647
|
+
)
|
|
648
|
+
),
|
|
649
|
+
Effect.asVoid
|
|
650
|
+
)
|
|
651
|
+
const context: ProcessContext<State, Event> = {
|
|
652
|
+
...scope,
|
|
653
|
+
...(logic.execution?._tag === "Compiled" && !logic.execution.childless ? { ownedChildren } : undefined),
|
|
654
|
+
...acknowledgedContext,
|
|
655
|
+
receive,
|
|
656
|
+
poll,
|
|
657
|
+
state: SynchronizedRef.get(current).pipe(Effect.map((current) => current.snapshot.state)),
|
|
658
|
+
setState: setActiveState,
|
|
659
|
+
updateState
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const getOrCreateChanges = SynchronizedRef.modifyEffect(
|
|
663
|
+
current,
|
|
664
|
+
(current) => {
|
|
665
|
+
if (current.snapshot.status !== "active") {
|
|
666
|
+
return Effect.succeed([undefined, current] as const)
|
|
667
|
+
}
|
|
668
|
+
if (current.changes !== undefined) {
|
|
669
|
+
return Effect.succeed([current.changes, current] as const)
|
|
670
|
+
}
|
|
671
|
+
return PubSub.unbounded<Take.Take<VersionedSnapshot<State, Error, Output>>>({ replay: 1 }).pipe(
|
|
672
|
+
Effect.map((changes) => [changes, { ...current, changes }] as const)
|
|
673
|
+
)
|
|
674
|
+
}
|
|
675
|
+
)
|
|
676
|
+
|
|
677
|
+
const changesStream: Stream.Stream<RuntimeSnapshot<State, Error, Output>> = Stream.unwrap(
|
|
678
|
+
Effect.gen(function*() {
|
|
679
|
+
const changes = yield* getOrCreateChanges
|
|
680
|
+
if (changes === undefined) {
|
|
681
|
+
return Stream.succeed((yield* SynchronizedRef.get(current)).snapshot)
|
|
682
|
+
}
|
|
683
|
+
const subscription = yield* PubSub.subscribe(changes)
|
|
684
|
+
const captured = yield* SynchronizedRef.get(current)
|
|
685
|
+
if (captured.snapshot.status !== "active") {
|
|
686
|
+
return Stream.succeed(captured.snapshot)
|
|
687
|
+
}
|
|
688
|
+
return Stream.succeed(captured.snapshot).pipe(
|
|
689
|
+
Stream.concat(
|
|
690
|
+
Stream.fromChannel(Channel.fromEffectTake(PubSub.take(subscription))).pipe(
|
|
691
|
+
Stream.filter((next) => next.revision > captured.revision),
|
|
692
|
+
Stream.map((next) => next.snapshot)
|
|
693
|
+
)
|
|
694
|
+
)
|
|
695
|
+
)
|
|
696
|
+
})
|
|
697
|
+
)
|
|
698
|
+
|
|
699
|
+
const ref: MachineRef<State, Event, Error, Output> = {
|
|
700
|
+
id,
|
|
701
|
+
sessionId,
|
|
702
|
+
...(inspector === undefined
|
|
703
|
+
? undefined
|
|
704
|
+
: { inspectionSubject: subject!, sendInspected: offerInspected! }),
|
|
705
|
+
state: SynchronizedRef.get(current).pipe(Effect.map((current) => current.snapshot.state)),
|
|
706
|
+
snapshot: SynchronizedRef.get(current).pipe(Effect.map((current) => current.snapshot)),
|
|
707
|
+
changes: changesStream,
|
|
708
|
+
emissions: emissions.stream as Stream.Stream<never>,
|
|
709
|
+
join: Deferred.await(done),
|
|
710
|
+
stop,
|
|
711
|
+
send: self.send,
|
|
712
|
+
...(sendAcknowledged === undefined ? undefined : { [acknowledgedSend]: sendAcknowledged }),
|
|
713
|
+
child: getChild,
|
|
714
|
+
childChanges
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
if (onReadySync !== undefined && !onReadySync(ref)) {
|
|
718
|
+
yield* requestStop
|
|
719
|
+
} else if (onReady !== undefined) {
|
|
720
|
+
yield* onReady(ref, requestStop)
|
|
721
|
+
}
|
|
722
|
+
if (onSnapshot !== undefined) {
|
|
723
|
+
yield* notifyActiveSnapshot(onSnapshot, { status: "active", state: initial })
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const reserveTermination = (termination: ProcessTermination) => {
|
|
727
|
+
switch (termination._tag) {
|
|
728
|
+
case "Stopped":
|
|
729
|
+
return reserveStoppedSnapshot
|
|
730
|
+
case "Done":
|
|
731
|
+
return reserveSuccessSnapshot(termination.output)
|
|
732
|
+
case "Failure":
|
|
733
|
+
return reserveFailureSnapshot(termination.cause)
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const completeTermination = (
|
|
738
|
+
termination: ProcessTermination,
|
|
739
|
+
snapshot: RuntimeSnapshot<State, Error, Output>
|
|
740
|
+
) => {
|
|
741
|
+
switch (termination._tag) {
|
|
742
|
+
case "Stopped":
|
|
743
|
+
return terminalizeReservedStop(snapshot)
|
|
744
|
+
case "Done":
|
|
745
|
+
return terminalizeReservedSuccess(snapshot, termination.output)
|
|
746
|
+
case "Failure":
|
|
747
|
+
return terminalizeReservedFailure(snapshot, termination.cause)
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
const forkRuntime = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
752
|
+
detached === true
|
|
753
|
+
? Effect.forkDetach(effect)
|
|
754
|
+
: Effect.forkChild(effect)
|
|
755
|
+
|
|
756
|
+
const pendingTermination = yield* Deferred.poll(termination)
|
|
757
|
+
const worker = Option.isNone(pendingTermination)
|
|
758
|
+
? yield* Effect.uninterruptibleMask((restore) =>
|
|
759
|
+
restore(Effect.suspend(() => logic.run(context))).pipe(
|
|
760
|
+
Effect.exit,
|
|
761
|
+
Effect.flatMap((exit) =>
|
|
762
|
+
Deferred.succeed(
|
|
763
|
+
termination,
|
|
764
|
+
Exit.isFailure(exit)
|
|
765
|
+
? { _tag: "Failure", cause: exit.cause }
|
|
766
|
+
: { _tag: "Done", output: exit.value }
|
|
767
|
+
)
|
|
768
|
+
)
|
|
769
|
+
)
|
|
770
|
+
).pipe(forkRuntime)
|
|
771
|
+
: undefined
|
|
772
|
+
|
|
773
|
+
// One Deferred arbitrates all terminal causes. The supervisor reserves the
|
|
774
|
+
// terminal snapshot before interrupting the worker, so worker finalizers
|
|
775
|
+
// cannot mutate the frozen state. It then waits for those finalizers before
|
|
776
|
+
// publishing and completing `join` / `stop`.
|
|
777
|
+
const runFiber: Effect.Effect<void, never, Requirements> = Effect.uninterruptibleMask((restore) =>
|
|
778
|
+
Effect.gen(function*() {
|
|
779
|
+
const requested = Option.isSome(pendingTermination)
|
|
780
|
+
? yield* pendingTermination.value
|
|
781
|
+
: yield* restore(Deferred.await(termination))
|
|
782
|
+
|
|
783
|
+
const snapshot = yield* reserveTermination(requested)
|
|
784
|
+
if (worker !== undefined) {
|
|
785
|
+
yield* Fiber.interrupt(worker)
|
|
786
|
+
}
|
|
787
|
+
if (snapshot === undefined) {
|
|
788
|
+
return yield* awaitCompletion
|
|
789
|
+
}
|
|
790
|
+
return yield* completeTermination(requested, snapshot)
|
|
791
|
+
})
|
|
792
|
+
)
|
|
793
|
+
|
|
794
|
+
yield* forkRuntime(runFiber)
|
|
795
|
+
yield* Effect.yieldNow
|
|
796
|
+
|
|
797
|
+
return ref
|
|
798
|
+
}
|
|
799
|
+
)
|