MCPcopy Create free account
hub / github.com/Effect-TS/effect / setup

Function setup

packages/platform/node/src/NodeWorker.ts:33–66  ·  view source on GitHub ↗
({ scope, worker })

Source from the content-addressed store, hash-verified

31export const layerPlatform: Layer.Layer<Worker.WorkerPlatform> = Layer.succeed(Worker.WorkerPlatform)(
32 Worker.makePlatform<WorkerThreads.Worker | ChildProcess.ChildProcess>()({
33 setup({ scope, worker }) {
34 const exitDeferred = Deferred.makeUnsafe<void, WorkerError>()
35 const thing = "postMessage" in worker ?
36 {
37 postMessage(msg: any, t?: any) {
38 worker.postMessage(msg, t)
39 },
40 kill: () => worker.terminate(),
41 worker
42 } :
43 {
44 postMessage(msg: any, _?: any) {
45 worker.send(msg)
46 },
47 kill: () => worker.kill("SIGKILL"),
48 worker
49 }
50 worker.on("exit", () => {
51 Deferred.doneUnsafe(exitDeferred, Exit.void)
52 })
53 return Effect.as(
54 Scope.addFinalizer(
55 scope,
56 Effect.suspend(() => {
57 thing.postMessage([1])
58 return Deferred.await(exitDeferred)
59 }).pipe(
60 Effect.timeout(5000),
61 Effect.catchCause(() => Effect.sync(() => thing.kill()))
62 )
63 ),
64 thing
65 )
66 },
67 listen({ deferred, emit, port }) {
68 port.worker.on("message", (message) => {
69 emit(message)

Callers

nothing calls this directly

Calls 6

addFinalizerMethod · 0.80
onMethod · 0.65
pipeMethod · 0.65
awaitMethod · 0.65
postMessageMethod · 0.45
syncMethod · 0.45

Tested by

no test coverage detected