Skip to content

Commit ef4db1c

Browse files
committed
pass initData in
1 parent 57f1197 commit ef4db1c

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

packages/server/bin/run-simulation-child.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
import { main, suspend, until, type Operation } from "effection";
2+
import { main, suspend, until } from "effection";
33
import { pathToFileURL } from "node:url";
44
import type {
55
FoundationSimulator,

packages/server/src/service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
type Stream,
55
each,
66
lift,
7-
race,
87
resource,
98
scoped,
109
sleep,

packages/server/src/simulation.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,27 @@ import { SimulacrumEndpoint } from "./services.ts";
1616
*/
1717
export function useSimulation<L extends object = Record<string, unknown>>(
1818
name: string,
19-
createFactory: () => FoundationSimulator<L>
19+
createFactory: (initData?: unknown) => FoundationSimulator<L>
2020
): Operation<FoundationSimulatorListening<L>> {
2121
return resource(function* (provide) {
22-
const createSim = createFactory();
22+
// attempt to read the simulacrum port from context; if not present, continue without it
23+
const simulacrumPort = yield* SimulacrumEndpoint.get();
24+
25+
// if present fetch the data chunk and pass it to the factory
26+
let initData: unknown | undefined = undefined;
27+
if (typeof simulacrumPort === "number" && !Number.isNaN(simulacrumPort)) {
28+
try {
29+
const res = yield* until(
30+
fetch(`http://127.0.0.1:${simulacrumPort}/data`)
31+
);
32+
initData = yield* until(res.json());
33+
} catch (err) {
34+
// ignore fetch failures
35+
console.error("failed to fetch simulacrum data:", err);
36+
}
37+
}
38+
39+
const createSim = createFactory(initData);
2340
const listening: FoundationSimulatorListening<L> = yield* until(
2441
createSim.listen()
2542
);

0 commit comments

Comments
 (0)