@typeonce/effect-machine 0.32.0 → 0.34.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 +216 -635
- package/dist/Machine.d.ts +814 -545
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +20 -8
- 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 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -45
- 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 +5 -4
- 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/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 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +149 -280
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +12 -11
- 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/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/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +31 -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 +2 -1
- 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 +38 -40
- 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/unstable/cluster/ClusterMachine.d.ts +5 -5
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +5 -17
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +68 -23
- package/package.json +1 -1
- package/src/Machine.ts +1387 -2011
- 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 -45
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +6 -5
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +210 -389
- package/src/internal/machine/planner.ts +54 -35
- package/src/internal/machine/process.ts +40 -33
- 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/targetReference.ts +40 -0
- package/src/internal/machine/topology.ts +2 -1
- package/src/internal/testing/machine/finiteModel.ts +42 -43
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/unstable/cluster/ClusterMachine.ts +9 -17
|
@@ -0,0 +1,867 @@
|
|
|
1
|
+
/** Compiled statechart execution strategy. */
|
|
2
|
+
import * as Cause from "effect/Cause";
|
|
3
|
+
import * as Channel from "effect/Channel";
|
|
4
|
+
import * as Context from "effect/Context";
|
|
5
|
+
import * as Deferred from "effect/Deferred";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import * as Exit from "effect/Exit";
|
|
8
|
+
import * as Fiber from "effect/Fiber";
|
|
9
|
+
import * as Option from "effect/Option";
|
|
10
|
+
import * as PubSub from "effect/PubSub";
|
|
11
|
+
import * as Stream from "effect/Stream";
|
|
12
|
+
import {} from "./childRegistry.js";
|
|
13
|
+
import { StoppedError } from "./errors.js";
|
|
14
|
+
import * as InspectionRuntime from "./inspectionRuntime.js";
|
|
15
|
+
import { AcknowledgedMessageTypeId, acknowledgedSend, childlessRuntime, classifyOutcome, closeCompactMailbox, EmissionsClosed, failAcknowledgedMessage, inspectionSubject, isAcknowledgedMessage, isMachineTarget, makeChildRuntime, makeChildRuntimeSync, makeInspectedMessage, messageCausation, messageEvent, noInspectInitial, noParentSend, notifyActiveSnapshot, offerCompactMailbox, pollCompactMailbox, publishInspectedSent, sendMachineTarget, stopAcknowledgedMessage, succeedAcknowledgedMessage } from "./runtimeProtocol.js";
|
|
16
|
+
const CompiledStoppedCompletion = Symbol("effect/Machine/CompiledStoppedCompletion");
|
|
17
|
+
/**
|
|
18
|
+
* Compact runtime for compiled statecharts.
|
|
19
|
+
*
|
|
20
|
+
* All long-lived state is stored directly on this object. Operations are
|
|
21
|
+
* implemented by shared prototype methods, and public Effect / Stream values
|
|
22
|
+
* are materialized only when accessed. Arbitrary process logic continues to
|
|
23
|
+
* use the general runtime above.
|
|
24
|
+
*/
|
|
25
|
+
class CompiledProcess {
|
|
26
|
+
logic;
|
|
27
|
+
options;
|
|
28
|
+
services;
|
|
29
|
+
id;
|
|
30
|
+
sessionId;
|
|
31
|
+
inspectionSubject;
|
|
32
|
+
send;
|
|
33
|
+
sendInspected(event, source, causedBy) {
|
|
34
|
+
return this.offerEvent(event, source, causedBy);
|
|
35
|
+
}
|
|
36
|
+
[acknowledgedSend](event) {
|
|
37
|
+
return this.sendAcknowledgedEffect(event);
|
|
38
|
+
}
|
|
39
|
+
mailbox = {
|
|
40
|
+
items: undefined,
|
|
41
|
+
index: 0,
|
|
42
|
+
closed: false
|
|
43
|
+
};
|
|
44
|
+
address;
|
|
45
|
+
childRuntime = childlessRuntime;
|
|
46
|
+
processScope;
|
|
47
|
+
processContext;
|
|
48
|
+
compiledContext;
|
|
49
|
+
current;
|
|
50
|
+
/**
|
|
51
|
+
* `Active` has no terminal payload, `TerminationRequested` owns
|
|
52
|
+
* `termination` and its optional reserved snapshot, and `Completed` owns
|
|
53
|
+
* `completion`. `runState` independently tracks worker activity.
|
|
54
|
+
*/
|
|
55
|
+
lifecycle = "Active";
|
|
56
|
+
runState = "Initializing";
|
|
57
|
+
completion;
|
|
58
|
+
waiter;
|
|
59
|
+
termination;
|
|
60
|
+
terminationSnapshot;
|
|
61
|
+
worker;
|
|
62
|
+
interruptRequested = false;
|
|
63
|
+
offerRevision = 0;
|
|
64
|
+
inFlightMessage;
|
|
65
|
+
externalEmissions;
|
|
66
|
+
emissionsPubSub;
|
|
67
|
+
activity;
|
|
68
|
+
initialEntryPaths;
|
|
69
|
+
initialMicrosteps;
|
|
70
|
+
initializing;
|
|
71
|
+
constructor(logic, options, services, sessionId) {
|
|
72
|
+
this.logic = logic;
|
|
73
|
+
this.options = options;
|
|
74
|
+
this.services = services;
|
|
75
|
+
this.sessionId = sessionId;
|
|
76
|
+
this.id = options.id ?? sessionId;
|
|
77
|
+
const inspector = options.runtime.inspection;
|
|
78
|
+
if (inspector !== undefined) {
|
|
79
|
+
this.inspectionSubject = inspectionSubject(logic, this.id, sessionId);
|
|
80
|
+
this.initializing = true;
|
|
81
|
+
if (options.activity !== undefined)
|
|
82
|
+
this.activity = { ...options.activity, sessionId };
|
|
83
|
+
}
|
|
84
|
+
this.externalEmissions = options.emissions;
|
|
85
|
+
this.send = inspector === undefined
|
|
86
|
+
? (event) => this.offerMessage(event)
|
|
87
|
+
: (event) => this.offerEvent(event, undefined, undefined);
|
|
88
|
+
this.address = {
|
|
89
|
+
id: this.id,
|
|
90
|
+
sessionId,
|
|
91
|
+
stop: Effect.suspend(() => this.stopFromProcess()),
|
|
92
|
+
send: this.send,
|
|
93
|
+
...(inspector === undefined
|
|
94
|
+
? undefined
|
|
95
|
+
: {
|
|
96
|
+
inspectionSubject: this.inspectionSubject,
|
|
97
|
+
sendInspected: (event, source, causedBy) => this.offerEvent(event, source, causedBy)
|
|
98
|
+
})
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
get inspector() {
|
|
102
|
+
return this.options.runtime.inspection;
|
|
103
|
+
}
|
|
104
|
+
causation() {
|
|
105
|
+
return messageCausation(this.inFlightMessage, this.initializing === true);
|
|
106
|
+
}
|
|
107
|
+
publishCreated() {
|
|
108
|
+
const inspector = this.inspector;
|
|
109
|
+
const subject = this.inspectionSubject;
|
|
110
|
+
if (inspector === undefined || subject === undefined)
|
|
111
|
+
return;
|
|
112
|
+
inspector.publishUnsafe(this.activity === undefined
|
|
113
|
+
? {
|
|
114
|
+
_tag: "Created",
|
|
115
|
+
subject,
|
|
116
|
+
parent: this.options.parent?.inspectionSubject,
|
|
117
|
+
origin: this.options.origin ?? { _tag: "Root" },
|
|
118
|
+
definition: this.logic.inspection?.definition
|
|
119
|
+
}
|
|
120
|
+
: { _tag: "ActivityStarted", subject: this.activity.owner, activity: this.activity });
|
|
121
|
+
}
|
|
122
|
+
publishInitialized(state) {
|
|
123
|
+
this.initializing = false;
|
|
124
|
+
if (this.activity !== undefined)
|
|
125
|
+
return;
|
|
126
|
+
const inspector = this.inspector;
|
|
127
|
+
const subject = this.inspectionSubject;
|
|
128
|
+
if (inspector === undefined || subject === undefined)
|
|
129
|
+
return;
|
|
130
|
+
inspector.publishUnsafe({
|
|
131
|
+
_tag: "Initialized",
|
|
132
|
+
subject,
|
|
133
|
+
snapshot: { status: "active", state },
|
|
134
|
+
initialEntryPaths: this.initialEntryPaths ?? [],
|
|
135
|
+
microsteps: InspectionRuntime.microsteps({ microsteps: this.initialMicrosteps ?? [] })
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
publishStartFailed(cause) {
|
|
139
|
+
this.initializing = false;
|
|
140
|
+
const inspector = this.inspector;
|
|
141
|
+
const subject = this.inspectionSubject;
|
|
142
|
+
if (inspector === undefined || subject === undefined)
|
|
143
|
+
return;
|
|
144
|
+
inspector.publishUnsafe(this.activity === undefined
|
|
145
|
+
? { _tag: "StartFailed", subject, cause }
|
|
146
|
+
: {
|
|
147
|
+
_tag: "ActivityStopped",
|
|
148
|
+
subject: this.activity.owner,
|
|
149
|
+
activity: this.activity,
|
|
150
|
+
exit: Exit.failCause(cause)
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
get execution() {
|
|
154
|
+
return this.logic.execution;
|
|
155
|
+
}
|
|
156
|
+
initializeCompiledSync() {
|
|
157
|
+
this.publishCreated();
|
|
158
|
+
if (!this.execution.childless) {
|
|
159
|
+
this.childRuntime = makeChildRuntimeSync(this.address, this.options.runtime, this.services, (event) => this.sendAcknowledgedEffect(event));
|
|
160
|
+
}
|
|
161
|
+
const parent = this.options.parent;
|
|
162
|
+
const sendParent = this.options.sendParent ?? (parent === undefined
|
|
163
|
+
? noParentSend
|
|
164
|
+
: this.inspector === undefined
|
|
165
|
+
? parent.send
|
|
166
|
+
: (event) => sendMachineTarget(parent, event, this.inspectionSubject, this.causation()));
|
|
167
|
+
this.processScope = {
|
|
168
|
+
self: this.address,
|
|
169
|
+
parent,
|
|
170
|
+
spawn: this.childRuntime.spawn,
|
|
171
|
+
sendParent,
|
|
172
|
+
emit: (event) => this.emitEvent(event),
|
|
173
|
+
sendTo: ((target, event) => isMachineTarget(target)
|
|
174
|
+
? this.inspector === undefined
|
|
175
|
+
? target.send(event)
|
|
176
|
+
: sendMachineTarget(target, event, this.inspectionSubject, this.causation())
|
|
177
|
+
: this.childRuntime.sendTo(target, event, this.inspectionSubject, this.causation())),
|
|
178
|
+
stopChild: this.childRuntime.stop,
|
|
179
|
+
failCause: (cause) => this.failCause(cause),
|
|
180
|
+
inspectInitial: this.inspector === undefined
|
|
181
|
+
? noInspectInitial
|
|
182
|
+
: (paths, microsteps = []) => {
|
|
183
|
+
this.initialEntryPaths = paths;
|
|
184
|
+
this.initialMicrosteps = microsteps;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
const compiledInitial = this.execution.initialSync;
|
|
188
|
+
let initialized;
|
|
189
|
+
try {
|
|
190
|
+
initialized = compiledInitial(this.processScope);
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
this.runState = "Idle";
|
|
194
|
+
this.publishStartFailed(Cause.fail(error));
|
|
195
|
+
return Effect.fail(error);
|
|
196
|
+
}
|
|
197
|
+
this.runState = "Idle";
|
|
198
|
+
this.current = {
|
|
199
|
+
revision: 0,
|
|
200
|
+
terminalizing: false,
|
|
201
|
+
changes: undefined,
|
|
202
|
+
snapshot: { status: "active", state: initialized.state }
|
|
203
|
+
};
|
|
204
|
+
this.publishInitialized(initialized.state);
|
|
205
|
+
this.compiledContext = new CompiledProcessContextImpl(this.processScope, this.childRuntime.owned, this);
|
|
206
|
+
if ("executionState" in initialized) {
|
|
207
|
+
this.compiledContext.executionState = initialized.executionState;
|
|
208
|
+
}
|
|
209
|
+
if (this.options.onReadySync !== undefined && !this.options.onReadySync(this)) {
|
|
210
|
+
this.requestTerminationSync({ _tag: "Stopped" });
|
|
211
|
+
}
|
|
212
|
+
if (initialized.done === true && this.lifecycle === "Active") {
|
|
213
|
+
this.requestTerminationSync({ _tag: "Done", output: initialized.output });
|
|
214
|
+
}
|
|
215
|
+
if (initialized.done === false && this.execution.childless &&
|
|
216
|
+
this.lifecycle === "Active" &&
|
|
217
|
+
this.mailbox.items === undefined) {
|
|
218
|
+
return Effect.succeed(this);
|
|
219
|
+
}
|
|
220
|
+
this.runState = "Draining";
|
|
221
|
+
return Effect.provideContext(this.drainRuntime(), this.services).pipe(Effect.as(this));
|
|
222
|
+
}
|
|
223
|
+
initialize() {
|
|
224
|
+
const self = this;
|
|
225
|
+
return Effect.gen(function* () {
|
|
226
|
+
self.publishCreated();
|
|
227
|
+
if (!self.execution.childless) {
|
|
228
|
+
self.childRuntime = yield* makeChildRuntime(self.address, self.options.runtime, self.services, (event) => self.sendAcknowledgedEffect(event));
|
|
229
|
+
}
|
|
230
|
+
const parent = self.options.parent;
|
|
231
|
+
const sendParent = self.options.sendParent ?? (parent === undefined
|
|
232
|
+
? noParentSend
|
|
233
|
+
: self.inspector === undefined
|
|
234
|
+
? parent.send
|
|
235
|
+
: (event) => sendMachineTarget(parent, event, self.inspectionSubject, self.causation()));
|
|
236
|
+
self.processScope = {
|
|
237
|
+
self: self.address,
|
|
238
|
+
parent,
|
|
239
|
+
spawn: self.childRuntime.spawn,
|
|
240
|
+
sendParent,
|
|
241
|
+
emit: (event) => self.emitEvent(event),
|
|
242
|
+
sendTo: ((target, event) => isMachineTarget(target)
|
|
243
|
+
? self.inspector === undefined
|
|
244
|
+
? target.send(event)
|
|
245
|
+
: sendMachineTarget(target, event, self.inspectionSubject, self.causation())
|
|
246
|
+
: self.childRuntime.sendTo(target, event, self.inspectionSubject, self.causation())),
|
|
247
|
+
stopChild: self.childRuntime.stop,
|
|
248
|
+
failCause: (cause) => self.failCause(cause),
|
|
249
|
+
inspectInitial: self.inspector === undefined
|
|
250
|
+
? noInspectInitial
|
|
251
|
+
: (paths, microsteps = []) => {
|
|
252
|
+
self.initialEntryPaths = paths;
|
|
253
|
+
self.initialMicrosteps = microsteps;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const cleanupStartupFailure = (exit) => Exit.isFailure(exit) ? self.childRuntime.close(exit) : Effect.void;
|
|
257
|
+
const compiledInitial = self.execution.initial;
|
|
258
|
+
const initializeEffect = compiledInitial === undefined
|
|
259
|
+
? self.logic.initial(self.processScope).pipe(Effect.map((state) => ({ state, done: undefined, output: undefined })))
|
|
260
|
+
: compiledInitial(self.processScope);
|
|
261
|
+
const initialized = yield* initializeEffect.pipe(Effect.onExit((exit) => {
|
|
262
|
+
if (Exit.isFailure(exit))
|
|
263
|
+
self.publishStartFailed(exit.cause);
|
|
264
|
+
return cleanupStartupFailure(exit);
|
|
265
|
+
}), Effect.ensuring(Effect.sync(() => {
|
|
266
|
+
self.runState = "Idle";
|
|
267
|
+
})));
|
|
268
|
+
const initial = initialized.state;
|
|
269
|
+
self.current = {
|
|
270
|
+
revision: 0,
|
|
271
|
+
terminalizing: false,
|
|
272
|
+
changes: undefined,
|
|
273
|
+
snapshot: { status: "active", state: initial }
|
|
274
|
+
};
|
|
275
|
+
self.publishInitialized(initial);
|
|
276
|
+
if (self.execution.drain._tag === "Process") {
|
|
277
|
+
self.processContext = {
|
|
278
|
+
...self.processScope,
|
|
279
|
+
receive: Effect.never,
|
|
280
|
+
poll: Effect.sync(() => Option.map(pollCompactMailbox(self.mailbox), messageEvent)),
|
|
281
|
+
receiveMessage: Effect.never,
|
|
282
|
+
pollMessage: Effect.sync(() => self.pollCompiledMessage()),
|
|
283
|
+
completeMessage: (delivery) => self.completeCompiledMessage(delivery),
|
|
284
|
+
state: Effect.sync(() => self.current.snapshot.state),
|
|
285
|
+
setState: (state) => self.setActiveState(state),
|
|
286
|
+
updateState: (f) => self.updateState(f)
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
self.compiledContext = new CompiledProcessContextImpl(self.processScope, self.childRuntime.owned, self);
|
|
291
|
+
if ("executionState" in initialized) {
|
|
292
|
+
self.compiledContext.executionState = initialized.executionState;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (self.options.onReadySync !== undefined && !self.options.onReadySync(self)) {
|
|
296
|
+
yield* self.requestTermination({ _tag: "Stopped" });
|
|
297
|
+
}
|
|
298
|
+
else if (self.options.onReady !== undefined) {
|
|
299
|
+
yield* self.options.onReady(self, self.requestTermination({ _tag: "Stopped" }).pipe(Effect.asVoid));
|
|
300
|
+
}
|
|
301
|
+
if (self.options.onSnapshot !== undefined) {
|
|
302
|
+
yield* notifyActiveSnapshot(self.options.onSnapshot, { status: "active", state: initial });
|
|
303
|
+
}
|
|
304
|
+
if (initialized.done === true && self.lifecycle === "Active") {
|
|
305
|
+
yield* self.requestTermination({ _tag: "Done", output: initialized.output });
|
|
306
|
+
}
|
|
307
|
+
// A compiled machine startup plan has already settled entry actions,
|
|
308
|
+
// raised events, and eventless transitions. If it is known active and
|
|
309
|
+
// neither startup hooks nor emitted work queued an event, there is no
|
|
310
|
+
// first drain to perform. Future sends observe an idle run state and
|
|
311
|
+
// schedule the ordinary compiled worker.
|
|
312
|
+
if (initialized.done === false && self.execution.childless &&
|
|
313
|
+
self.lifecycle === "Active" && self.mailbox.items === undefined) {
|
|
314
|
+
return self;
|
|
315
|
+
}
|
|
316
|
+
self.runState = "Draining";
|
|
317
|
+
yield* self.drainRuntime();
|
|
318
|
+
return self;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
get state() {
|
|
322
|
+
return Effect.sync(() => this.current.snapshot.state);
|
|
323
|
+
}
|
|
324
|
+
get snapshot() {
|
|
325
|
+
return Effect.sync(() => this.current.snapshot);
|
|
326
|
+
}
|
|
327
|
+
get changes() {
|
|
328
|
+
return this.changesStream();
|
|
329
|
+
}
|
|
330
|
+
get emissions() {
|
|
331
|
+
return (this.externalEmissions?.stream ?? this.emissionsStream());
|
|
332
|
+
}
|
|
333
|
+
get join() {
|
|
334
|
+
return Effect.suspend(() => {
|
|
335
|
+
if (this.lifecycle === "Completed") {
|
|
336
|
+
return this.resolveCompletion(this.completion);
|
|
337
|
+
}
|
|
338
|
+
this.waiter ??= Deferred.makeUnsafe();
|
|
339
|
+
return Deferred.await(this.waiter);
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
get stop() {
|
|
343
|
+
return Effect.uninterruptible(this.stopEffect());
|
|
344
|
+
}
|
|
345
|
+
child(child) {
|
|
346
|
+
return this.childRuntime.get(child);
|
|
347
|
+
}
|
|
348
|
+
childChanges(child) {
|
|
349
|
+
return this.childRuntime.changes(child);
|
|
350
|
+
}
|
|
351
|
+
requestTermination(requested) {
|
|
352
|
+
return Effect.sync(() => this.requestTerminationSync(requested));
|
|
353
|
+
}
|
|
354
|
+
hasTerminationRequest() {
|
|
355
|
+
return this.lifecycle === "TerminationRequested";
|
|
356
|
+
}
|
|
357
|
+
requestTerminationSync(requested) {
|
|
358
|
+
if (this.lifecycle !== "Active") {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
this.lifecycle = "TerminationRequested";
|
|
362
|
+
this.termination = requested;
|
|
363
|
+
this.terminationSnapshot = this.reserveTermination(requested);
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
reserveTermination(requested) {
|
|
367
|
+
const latest = this.current;
|
|
368
|
+
if (latest === undefined || latest.terminalizing || latest.snapshot.status !== "active") {
|
|
369
|
+
return undefined;
|
|
370
|
+
}
|
|
371
|
+
const snapshot = requested._tag === "Stopped"
|
|
372
|
+
? { status: "stopped", state: latest.snapshot.state }
|
|
373
|
+
: requested._tag === "Done"
|
|
374
|
+
? { status: "done", state: latest.snapshot.state, output: requested.output }
|
|
375
|
+
: { status: "error", state: latest.snapshot.state, cause: requested.cause };
|
|
376
|
+
this.current = { ...latest, terminalizing: true };
|
|
377
|
+
return snapshot;
|
|
378
|
+
}
|
|
379
|
+
stopFromProcess() {
|
|
380
|
+
const request = this.requestTermination({ _tag: "Stopped" }).pipe(Effect.asVoid);
|
|
381
|
+
return this.runState === "Initializing" ? request : request.pipe(Effect.andThen(Effect.interrupt));
|
|
382
|
+
}
|
|
383
|
+
failCause(cause) {
|
|
384
|
+
const requested = { _tag: "Failure", cause };
|
|
385
|
+
return this.requestTermination(requested).pipe(Effect.flatMap((accepted) => accepted
|
|
386
|
+
? Effect.forkDetach(this.settleRequestedTermination()).pipe(Effect.asVoid)
|
|
387
|
+
: Effect.void));
|
|
388
|
+
}
|
|
389
|
+
sendAcknowledgedEffect(event) {
|
|
390
|
+
return Effect.uninterruptibleMask((restore) => Deferred.make().pipe(Effect.flatMap((deferred) => this.offerEvent(event, undefined, undefined, deferred).pipe(Effect.andThen(restore(Deferred.await(deferred)))))));
|
|
391
|
+
}
|
|
392
|
+
offerEvent(event, source, causedBy, deferred) {
|
|
393
|
+
return Effect.suspend(() => {
|
|
394
|
+
const inspector = this.inspector;
|
|
395
|
+
const subject = this.inspectionSubject;
|
|
396
|
+
const message = inspector?.isActive() === true && subject !== undefined
|
|
397
|
+
? makeInspectedMessage(inspector, subject, event, source, causedBy, deferred)
|
|
398
|
+
: deferred === undefined
|
|
399
|
+
? event
|
|
400
|
+
: { [AcknowledgedMessageTypeId]: true, event, deferred };
|
|
401
|
+
return this.offerMessage(message);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
offerMessage(message) {
|
|
405
|
+
return Effect.uninterruptible(Effect.suspend(() => {
|
|
406
|
+
if (this.mailbox.closed || this.lifecycle !== "Active") {
|
|
407
|
+
return Effect.fail(new StoppedError());
|
|
408
|
+
}
|
|
409
|
+
offerCompactMailbox(this.mailbox, message);
|
|
410
|
+
const inspector = this.inspector;
|
|
411
|
+
const subject = this.inspectionSubject;
|
|
412
|
+
if (inspector !== undefined && subject !== undefined)
|
|
413
|
+
publishInspectedSent(inspector, subject, message);
|
|
414
|
+
this.offerRevision += 1;
|
|
415
|
+
if (this.runState === "Draining") {
|
|
416
|
+
return Effect.void;
|
|
417
|
+
}
|
|
418
|
+
this.runState = "Draining";
|
|
419
|
+
const scheduled = Effect.yieldNow.pipe(Effect.andThen(Effect.provideContext(this.drainRuntime(), this.services)));
|
|
420
|
+
const fork = this.options.detached === true
|
|
421
|
+
? Effect.forkDetach(scheduled, { startImmediately: true })
|
|
422
|
+
: Effect.forkChild(scheduled, { startImmediately: true });
|
|
423
|
+
return fork.pipe(Effect.flatMap((fiber) => Effect.sync(() => {
|
|
424
|
+
this.worker = fiber;
|
|
425
|
+
if (!this.interruptRequested) {
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
this.interruptRequested = false;
|
|
429
|
+
return true;
|
|
430
|
+
}).pipe(Effect.flatMap((interrupt) => interrupt ? this.interruptAndFinish(fiber) : Effect.void))), Effect.asVoid);
|
|
431
|
+
}));
|
|
432
|
+
}
|
|
433
|
+
stopEffect() {
|
|
434
|
+
return Effect.suspend(() => {
|
|
435
|
+
if (this.lifecycle === "Completed") {
|
|
436
|
+
return Effect.void;
|
|
437
|
+
}
|
|
438
|
+
if (this.finishIdleChildlessStop()) {
|
|
439
|
+
return Effect.void;
|
|
440
|
+
}
|
|
441
|
+
const requested = { _tag: "Stopped" };
|
|
442
|
+
return this.requestTermination(requested).pipe(Effect.flatMap((accepted) => accepted
|
|
443
|
+
? this.settleRequestedTermination()
|
|
444
|
+
: this.awaitCompletion()));
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
finishIdleChildlessStop() {
|
|
448
|
+
if (!this.execution.childless || this.runState !== "Idle" || this.worker !== undefined ||
|
|
449
|
+
this.lifecycle !== "Active" ||
|
|
450
|
+
(this.options.onOutcome !== undefined && this.options.skipStoppedOutcome !== true) ||
|
|
451
|
+
this.options.onStop !== undefined ||
|
|
452
|
+
this.inspector !== undefined ||
|
|
453
|
+
this.current.changes !== undefined ||
|
|
454
|
+
this.current.terminalizing || this.current.snapshot.status !== "active") {
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
const snapshot = { status: "stopped", state: this.current.snapshot.state };
|
|
458
|
+
this.lifecycle = "TerminationRequested";
|
|
459
|
+
this.termination = { _tag: "Stopped" };
|
|
460
|
+
this.terminationSnapshot = snapshot;
|
|
461
|
+
closeCompactMailbox(this.mailbox);
|
|
462
|
+
this.current = {
|
|
463
|
+
revision: this.current.revision + 1,
|
|
464
|
+
terminalizing: true,
|
|
465
|
+
changes: undefined,
|
|
466
|
+
snapshot
|
|
467
|
+
};
|
|
468
|
+
stopAcknowledgedMessage(this.inFlightMessage);
|
|
469
|
+
this.inFlightMessage = undefined;
|
|
470
|
+
this.interruptRequested = false;
|
|
471
|
+
if (this.compiledContext !== undefined) {
|
|
472
|
+
this.compiledContext.executionState = undefined;
|
|
473
|
+
}
|
|
474
|
+
this.options.onStopSync?.();
|
|
475
|
+
this.completion = CompiledStoppedCompletion;
|
|
476
|
+
this.lifecycle = "Completed";
|
|
477
|
+
if (this.waiter !== undefined) {
|
|
478
|
+
Deferred.doneUnsafe(this.waiter, this.resolveCompletion(CompiledStoppedCompletion));
|
|
479
|
+
this.waiter = undefined;
|
|
480
|
+
}
|
|
481
|
+
return true;
|
|
482
|
+
}
|
|
483
|
+
settleRequestedTermination() {
|
|
484
|
+
return Effect.suspend(() => {
|
|
485
|
+
if (this.runState !== "Draining") {
|
|
486
|
+
return this.finishRequestedTermination();
|
|
487
|
+
}
|
|
488
|
+
if (this.worker === undefined) {
|
|
489
|
+
this.interruptRequested = true;
|
|
490
|
+
return this.awaitCompletion();
|
|
491
|
+
}
|
|
492
|
+
return this.interruptAndFinish(this.worker).pipe(Effect.andThen(this.awaitCompletion()));
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
interruptAndFinish(worker) {
|
|
496
|
+
return Fiber.interrupt(worker).pipe(Effect.andThen(Effect.suspend(() => this.lifecycle !== "Completed"
|
|
497
|
+
? this.finishRequestedTermination()
|
|
498
|
+
: Effect.void)));
|
|
499
|
+
}
|
|
500
|
+
awaitCompletion() {
|
|
501
|
+
return this.lifecycle !== "Completed"
|
|
502
|
+
? this.join.pipe(Effect.exit, Effect.asVoid)
|
|
503
|
+
: Effect.void;
|
|
504
|
+
}
|
|
505
|
+
resolveCompletion(completion) {
|
|
506
|
+
if (completion !== CompiledStoppedCompletion) {
|
|
507
|
+
return completion;
|
|
508
|
+
}
|
|
509
|
+
const stopped = Effect.fail(new StoppedError());
|
|
510
|
+
this.completion = stopped;
|
|
511
|
+
return stopped;
|
|
512
|
+
}
|
|
513
|
+
drainRuntime() {
|
|
514
|
+
const self = this;
|
|
515
|
+
return Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
516
|
+
let observedRevision = self.offerRevision;
|
|
517
|
+
while (true) {
|
|
518
|
+
if (self.hasTerminationRequest()) {
|
|
519
|
+
return yield* self.finishRequestedTermination();
|
|
520
|
+
}
|
|
521
|
+
const exit = yield* restore(Effect.suspend(() => {
|
|
522
|
+
const drain = self.execution.drain;
|
|
523
|
+
return drain._tag === "Process"
|
|
524
|
+
? drain.run(self.processContext)
|
|
525
|
+
: drain.run(self.compiledContext);
|
|
526
|
+
})).pipe(Effect.exit);
|
|
527
|
+
self.flushPendingChanges();
|
|
528
|
+
if (Exit.isFailure(exit)) {
|
|
529
|
+
if (self.lifecycle === "Active") {
|
|
530
|
+
yield* self.requestTermination({ _tag: "Failure", cause: exit.cause });
|
|
531
|
+
}
|
|
532
|
+
return yield* self.finishRequestedTermination();
|
|
533
|
+
}
|
|
534
|
+
if (Option.isSome(exit.value)) {
|
|
535
|
+
yield* self.requestTermination({ _tag: "Done", output: exit.value.value });
|
|
536
|
+
return yield* self.finishRequestedTermination();
|
|
537
|
+
}
|
|
538
|
+
if (self.hasTerminationRequest()) {
|
|
539
|
+
return yield* self.finishRequestedTermination();
|
|
540
|
+
}
|
|
541
|
+
if (self.offerRevision !== observedRevision) {
|
|
542
|
+
observedRevision = self.offerRevision;
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
self.runState = "Idle";
|
|
546
|
+
self.worker = undefined;
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
}));
|
|
550
|
+
}
|
|
551
|
+
finishRequestedTermination() {
|
|
552
|
+
return Effect.suspend(() => {
|
|
553
|
+
if (this.lifecycle !== "TerminationRequested") {
|
|
554
|
+
return Effect.void;
|
|
555
|
+
}
|
|
556
|
+
const requested = this.termination;
|
|
557
|
+
if (requested === undefined) {
|
|
558
|
+
return Effect.void;
|
|
559
|
+
}
|
|
560
|
+
const snapshot = this.terminationSnapshot ?? this.reserveTermination(requested);
|
|
561
|
+
if (snapshot === undefined) {
|
|
562
|
+
return this.awaitCompletion();
|
|
563
|
+
}
|
|
564
|
+
const exit = requested._tag === "Stopped"
|
|
565
|
+
? Exit.void
|
|
566
|
+
: requested._tag === "Done"
|
|
567
|
+
? Exit.succeed(requested.output)
|
|
568
|
+
: Exit.failCause(requested.cause);
|
|
569
|
+
const completion = requested._tag === "Stopped"
|
|
570
|
+
? CompiledStoppedCompletion
|
|
571
|
+
: requested._tag === "Done"
|
|
572
|
+
? Effect.succeed(requested.output)
|
|
573
|
+
: Effect.failCause(requested.cause);
|
|
574
|
+
const notifyOutcome = this.options.onOutcome === undefined ||
|
|
575
|
+
(requested._tag === "Stopped" && this.options.skipStoppedOutcome === true)
|
|
576
|
+
? Effect.void
|
|
577
|
+
: Effect.suspend(() => this.options.onOutcome(classifyOutcome(snapshot))).pipe(Effect.exit, Effect.asVoid);
|
|
578
|
+
const inspector = this.inspector;
|
|
579
|
+
const subject = this.inspectionSubject;
|
|
580
|
+
const closeEmissionsAndInspect = inspector === undefined || subject === undefined
|
|
581
|
+
? this.closeEmissions()
|
|
582
|
+
: this.closeEmissions().pipe(Effect.andThen(Effect.sync(() => inspector.publishUnsafe(this.activity === undefined
|
|
583
|
+
? { _tag: "Terminated", subject, snapshot }
|
|
584
|
+
: {
|
|
585
|
+
_tag: "ActivityStopped",
|
|
586
|
+
subject: this.activity.owner,
|
|
587
|
+
activity: this.activity,
|
|
588
|
+
exit: snapshot.status === "stopped" ? Exit.interrupt() : exit
|
|
589
|
+
}))));
|
|
590
|
+
return Effect.uninterruptible(Effect.sync(() => {
|
|
591
|
+
closeCompactMailbox(this.mailbox);
|
|
592
|
+
}).pipe(Effect.andThen(this.childRuntime.close(exit)), Effect.andThen(this.setAndPublishSnapshot(snapshot)), Effect.andThen(closeEmissionsAndInspect), Effect.andThen(Effect.sync(() => {
|
|
593
|
+
if (requested._tag === "Failure") {
|
|
594
|
+
failAcknowledgedMessage(this.inFlightMessage, requested.cause);
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
stopAcknowledgedMessage(this.inFlightMessage);
|
|
598
|
+
}
|
|
599
|
+
this.inFlightMessage = undefined;
|
|
600
|
+
})), Effect.andThen(notifyOutcome), Effect.andThen(this.options.onStop ?? Effect.void), Effect.andThen(this.options.inspectionRoot === true && this.inspector !== undefined
|
|
601
|
+
? this.inspector.close
|
|
602
|
+
: Effect.void), Effect.andThen(Effect.sync(() => {
|
|
603
|
+
this.options.onStopSync?.();
|
|
604
|
+
this.runState = "Idle";
|
|
605
|
+
this.worker = undefined;
|
|
606
|
+
this.interruptRequested = false;
|
|
607
|
+
if (this.compiledContext !== undefined) {
|
|
608
|
+
this.compiledContext.executionState = undefined;
|
|
609
|
+
}
|
|
610
|
+
this.completion = completion;
|
|
611
|
+
this.lifecycle = "Completed";
|
|
612
|
+
if (this.waiter !== undefined) {
|
|
613
|
+
Deferred.doneUnsafe(this.waiter, this.resolveCompletion(completion));
|
|
614
|
+
this.waiter = undefined;
|
|
615
|
+
}
|
|
616
|
+
}))));
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
publishSnapshot(snapshot) {
|
|
620
|
+
const publish = snapshot.changes === undefined
|
|
621
|
+
? Effect.succeed(snapshot)
|
|
622
|
+
: PubSub.publish(snapshot.changes, [snapshot]).pipe(Effect.as(snapshot));
|
|
623
|
+
const current = snapshot.snapshot;
|
|
624
|
+
return this.options.onSnapshot === undefined || current.status !== "active"
|
|
625
|
+
? publish
|
|
626
|
+
: publish.pipe(Effect.tap(() => notifyActiveSnapshot(this.options.onSnapshot, current)));
|
|
627
|
+
}
|
|
628
|
+
completeChanges(snapshot) {
|
|
629
|
+
return snapshot.changes === undefined
|
|
630
|
+
? Effect.void
|
|
631
|
+
: PubSub.publish(snapshot.changes, Exit.succeed(undefined)).pipe(Effect.asVoid);
|
|
632
|
+
}
|
|
633
|
+
emitEvent(event) {
|
|
634
|
+
const publish = this.externalEmissions !== undefined ?
|
|
635
|
+
this.externalEmissions.emit(event) :
|
|
636
|
+
Effect.suspend(() => this.emissionsPubSub === undefined || this.emissionsPubSub === EmissionsClosed
|
|
637
|
+
? Effect.void
|
|
638
|
+
: PubSub.publish(this.emissionsPubSub, event).pipe(Effect.asVoid));
|
|
639
|
+
const inspector = this.inspector;
|
|
640
|
+
const subject = this.inspectionSubject;
|
|
641
|
+
if (inspector === undefined || subject === undefined)
|
|
642
|
+
return publish;
|
|
643
|
+
return publish.pipe(Effect.tap(() => Effect.sync(() => inspector.publishUnsafe({
|
|
644
|
+
_tag: "Emitted",
|
|
645
|
+
subject,
|
|
646
|
+
emission: event,
|
|
647
|
+
causedBy: this.causation()
|
|
648
|
+
}))));
|
|
649
|
+
}
|
|
650
|
+
shutdownEmissions() {
|
|
651
|
+
return this.closeEmissions();
|
|
652
|
+
}
|
|
653
|
+
closeEmissions() {
|
|
654
|
+
if (this.externalEmissions !== undefined)
|
|
655
|
+
return this.externalEmissions.close();
|
|
656
|
+
const observed = this.emissionsPubSub;
|
|
657
|
+
this.emissionsPubSub = EmissionsClosed;
|
|
658
|
+
return observed === undefined || observed === EmissionsClosed ? Effect.void : PubSub.shutdown(observed);
|
|
659
|
+
}
|
|
660
|
+
getOrCreateEmissions() {
|
|
661
|
+
return Effect.suspend(() => {
|
|
662
|
+
const observed = this.emissionsPubSub;
|
|
663
|
+
if (observed === EmissionsClosed)
|
|
664
|
+
return Effect.succeed(undefined);
|
|
665
|
+
if (observed !== undefined)
|
|
666
|
+
return Effect.succeed(observed);
|
|
667
|
+
return PubSub.unbounded().pipe(Effect.flatMap((candidate) => Effect.sync(() => {
|
|
668
|
+
const latest = this.emissionsPubSub;
|
|
669
|
+
if (latest === EmissionsClosed)
|
|
670
|
+
return [undefined, true];
|
|
671
|
+
if (latest !== undefined)
|
|
672
|
+
return [latest, true];
|
|
673
|
+
this.emissionsPubSub = candidate;
|
|
674
|
+
return [candidate, false];
|
|
675
|
+
}).pipe(Effect.flatMap(([selected, discard]) => discard ? PubSub.shutdown(candidate).pipe(Effect.as(selected)) : Effect.succeed(selected)))));
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
emissionsStream() {
|
|
679
|
+
return Stream.unwrap(this.getOrCreateEmissions().pipe(Effect.map((emissions) => emissions === undefined ? Stream.empty : Stream.fromPubSub(emissions))));
|
|
680
|
+
}
|
|
681
|
+
setAndPublishSnapshot(snapshot) {
|
|
682
|
+
return Effect.suspend(() => {
|
|
683
|
+
this.flushPendingChanges();
|
|
684
|
+
const versioned = {
|
|
685
|
+
revision: this.current.revision + 1,
|
|
686
|
+
snapshot,
|
|
687
|
+
terminalizing: true,
|
|
688
|
+
changes: this.current.changes
|
|
689
|
+
};
|
|
690
|
+
this.current = versioned;
|
|
691
|
+
return this.publishSnapshot(versioned).pipe(Effect.flatMap((published) => this.completeChanges(published)), Effect.asVoid);
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
setActiveState(state) {
|
|
695
|
+
return Effect.suspend(() => this.commitActiveState(state) ?? Effect.void);
|
|
696
|
+
}
|
|
697
|
+
pollCompiledMessage() {
|
|
698
|
+
const message = pollCompactMailbox(this.mailbox);
|
|
699
|
+
if (Option.isSome(message)) {
|
|
700
|
+
this.inFlightMessage = isAcknowledgedMessage(message.value) ? message.value : undefined;
|
|
701
|
+
}
|
|
702
|
+
return message;
|
|
703
|
+
}
|
|
704
|
+
completeCompiledMessage(delivery) {
|
|
705
|
+
succeedAcknowledgedMessage(this.inFlightMessage, delivery);
|
|
706
|
+
this.inFlightMessage = undefined;
|
|
707
|
+
}
|
|
708
|
+
compiledState() {
|
|
709
|
+
return this.current.snapshot.state;
|
|
710
|
+
}
|
|
711
|
+
commitCompiledState(state) {
|
|
712
|
+
return this.commitActiveState(state, true);
|
|
713
|
+
}
|
|
714
|
+
commitActiveState(state, batchChanges = false) {
|
|
715
|
+
const latest = this.current;
|
|
716
|
+
if (latest.terminalizing || latest.snapshot.status !== "active") {
|
|
717
|
+
return undefined;
|
|
718
|
+
}
|
|
719
|
+
const pendingChanges = latest.pendingChanges;
|
|
720
|
+
if (pendingChanges !== undefined) {
|
|
721
|
+
latest.pendingChanges = undefined;
|
|
722
|
+
}
|
|
723
|
+
const activeSnapshot = { status: "active", state };
|
|
724
|
+
const versioned = {
|
|
725
|
+
revision: latest.revision + 1,
|
|
726
|
+
snapshot: activeSnapshot,
|
|
727
|
+
terminalizing: false,
|
|
728
|
+
changes: latest.changes
|
|
729
|
+
};
|
|
730
|
+
this.current = versioned;
|
|
731
|
+
if (versioned.changes !== undefined) {
|
|
732
|
+
if (pendingChanges === undefined) {
|
|
733
|
+
if (batchChanges) {
|
|
734
|
+
versioned.pendingChanges = [versioned];
|
|
735
|
+
}
|
|
736
|
+
else {
|
|
737
|
+
PubSub.publishUnsafe(versioned.changes, [versioned]);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
else {
|
|
741
|
+
pendingChanges.push(versioned);
|
|
742
|
+
if (batchChanges) {
|
|
743
|
+
versioned.pendingChanges = pendingChanges;
|
|
744
|
+
}
|
|
745
|
+
else {
|
|
746
|
+
PubSub.publishUnsafe(versioned.changes, pendingChanges);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
return this.options.onSnapshot === undefined
|
|
751
|
+
? undefined
|
|
752
|
+
: notifyActiveSnapshot(this.options.onSnapshot, activeSnapshot);
|
|
753
|
+
}
|
|
754
|
+
flushPendingChanges() {
|
|
755
|
+
const current = this.current;
|
|
756
|
+
const pendingChanges = current?.pendingChanges;
|
|
757
|
+
if (pendingChanges === undefined || current.changes === undefined) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
current.pendingChanges = undefined;
|
|
761
|
+
PubSub.publishUnsafe(current.changes, pendingChanges);
|
|
762
|
+
}
|
|
763
|
+
updateState(f) {
|
|
764
|
+
return Effect.suspend(() => {
|
|
765
|
+
const observed = this.current;
|
|
766
|
+
if (observed.terminalizing || observed.snapshot.status !== "active") {
|
|
767
|
+
return Effect.void;
|
|
768
|
+
}
|
|
769
|
+
return f(observed.snapshot.state).pipe(Effect.flatMap((state) => {
|
|
770
|
+
const latest = this.current;
|
|
771
|
+
return latest.terminalizing || latest.revision !== observed.revision
|
|
772
|
+
? Effect.void
|
|
773
|
+
: this.setActiveState(state);
|
|
774
|
+
}));
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
getOrCreateChanges() {
|
|
778
|
+
return Effect.suspend(() => {
|
|
779
|
+
const observed = this.current;
|
|
780
|
+
if (observed.snapshot.status !== "active") {
|
|
781
|
+
return Effect.succeed(undefined);
|
|
782
|
+
}
|
|
783
|
+
if (observed.changes !== undefined) {
|
|
784
|
+
return Effect.succeed(observed.changes);
|
|
785
|
+
}
|
|
786
|
+
return PubSub.unbounded({ replay: 1 }).pipe(Effect.flatMap((candidate) => Effect.sync(() => {
|
|
787
|
+
const latest = this.current;
|
|
788
|
+
if (latest.snapshot.status !== "active") {
|
|
789
|
+
return [undefined, true];
|
|
790
|
+
}
|
|
791
|
+
if (latest.changes !== undefined) {
|
|
792
|
+
return [latest.changes, true];
|
|
793
|
+
}
|
|
794
|
+
this.current = { ...latest, changes: candidate };
|
|
795
|
+
return [candidate, false];
|
|
796
|
+
}).pipe(Effect.flatMap(([changes, discard]) => discard ? PubSub.shutdown(candidate).pipe(Effect.as(changes)) : Effect.succeed(changes)))));
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
changesStream() {
|
|
800
|
+
const self = this;
|
|
801
|
+
return Stream.unwrap(Effect.gen(function* () {
|
|
802
|
+
const changes = yield* self.getOrCreateChanges();
|
|
803
|
+
if (changes === undefined) {
|
|
804
|
+
return Stream.succeed(self.current.snapshot);
|
|
805
|
+
}
|
|
806
|
+
const subscription = yield* PubSub.subscribe(changes);
|
|
807
|
+
const captured = self.current;
|
|
808
|
+
if (captured.snapshot.status !== "active") {
|
|
809
|
+
return Stream.succeed(captured.snapshot);
|
|
810
|
+
}
|
|
811
|
+
return Stream.succeed(captured.snapshot).pipe(Stream.concat(Stream.fromChannel(Channel.fromEffectTake(PubSub.take(subscription))).pipe(Stream.filter((next) => next.revision > captured.revision), Stream.map((next) => next.snapshot))));
|
|
812
|
+
}));
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
class CompiledProcessContextImpl {
|
|
816
|
+
scope;
|
|
817
|
+
ownedChildren;
|
|
818
|
+
process;
|
|
819
|
+
executionState;
|
|
820
|
+
constructor(scope, ownedChildren, process) {
|
|
821
|
+
this.scope = scope;
|
|
822
|
+
this.ownedChildren = ownedChildren;
|
|
823
|
+
this.process = process;
|
|
824
|
+
}
|
|
825
|
+
poll() {
|
|
826
|
+
return Option.map(this.process.pollCompiledMessage(), messageEvent);
|
|
827
|
+
}
|
|
828
|
+
pollMessage() {
|
|
829
|
+
return this.process.pollCompiledMessage();
|
|
830
|
+
}
|
|
831
|
+
state() {
|
|
832
|
+
return this.process.compiledState();
|
|
833
|
+
}
|
|
834
|
+
completeMessage(delivery) {
|
|
835
|
+
this.process.completeCompiledMessage(delivery);
|
|
836
|
+
}
|
|
837
|
+
commit(state) {
|
|
838
|
+
return this.process.commitCompiledState(state);
|
|
839
|
+
}
|
|
840
|
+
runAfterChanges(effect) {
|
|
841
|
+
this.process.flushPendingChanges();
|
|
842
|
+
return effect;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
export const startCompactCompiledInternal = Effect.fnUntraced(function* (logic, options) {
|
|
846
|
+
const sessionId = options.sessionId ?? (yield* options.runtime.nextSessionId);
|
|
847
|
+
const services = yield* Effect.context();
|
|
848
|
+
const execution = logic.execution;
|
|
849
|
+
const process = new CompiledProcess(logic, options, services, sessionId);
|
|
850
|
+
// A compiled initializer is synchronous by construction. Only startup
|
|
851
|
+
// callbacks that themselves return Effects need the generic initialization
|
|
852
|
+
// program; the compiled drain is still provided the complete service context.
|
|
853
|
+
const initialize = execution.initialSync !== undefined &&
|
|
854
|
+
options.onReady === undefined && options.onSnapshot === undefined
|
|
855
|
+
? process.initializeCompiledSync()
|
|
856
|
+
: process.initialize();
|
|
857
|
+
return yield* initialize.pipe(Effect.onExit((exit) => Exit.isFailure(exit)
|
|
858
|
+
? process.shutdownEmissions().pipe(Effect.andThen(options.inspectionRoot === true && options.runtime.inspection !== undefined
|
|
859
|
+
? options.runtime.inspection.close
|
|
860
|
+
: Effect.void))
|
|
861
|
+
: Effect.void));
|
|
862
|
+
});
|
|
863
|
+
/** Preserves synchronous startup for owned compiled children. */
|
|
864
|
+
export const startCompiledSync = (logic, options, services, sessionId) => new CompiledProcess(logic, options, services, sessionId).initializeCompiledSync();
|
|
865
|
+
// Stopping is commonly used only for resource cleanup. Keep that path free of
|
|
866
|
+
// Error stack capture and materialize the typed join failure only if observed.
|
|
867
|
+
//# sourceMappingURL=runtimeCompiled.js.map
|