git.iko.soy/rowtype-yoga/purescript-yoga-om-workerbees
on main help sign in
Simplify API: makePool always uses context

- Remove makePoolWithData - use context pattern instead - makePool automatically passes Om context to workers via workerData - Simpler API: one function instead of two - More Om-idiomatic: context is the canonical way to share resources Examples: - FibonacciDemo: Use Unit context (no shared data needed) - SharedCounterDemo: Pass SharedInt wrapper as context Benefits: - Less API surface (removed makePoolWithData export) - Encourages proper Om patterns - Just as convenient for both simple and complex cases - Workers always receive context consistently Note: SharedCounterDemo still has communication issue (hangs)

commit9b77ea5c725079e009ea4a9071e044c690559e5f authorMark <mark.eibes@gmail.com> dateSat 7 Feb 2026, 09:48 (8 months ago) parentf250eba
3 files changed+29−29
src/Examples/FibonacciDemo.purs+2−2 src/Examples/SharedCounterDemo.purs+19−10 src/Yoga/Om/WorkerBees.purs+8−17
src/Examples/FibonacciDemo.purs+2−2
@@ -22,12 +22,12 @@ import Yoga.Om.WorkerBees (WorkerPool, makePool, distributeWork, terminatePool)
2222type FibInput = { n :: Int }
2323type FibOutput = { result :: Int, thread :: Int }
2424
25−fibDemo :: forall ctx. Om.Om ctx () Unit
25+fibDemo :: Om.Om Unit () Unit
2626fibDemo = do
2727 Om.fromAff $ log "=== Fibonacci Worker Demo ==="
2828 Om.fromAff $ log ""
2929
30− -- Create worker pool with 4 workers (Om API)
30+ -- Create worker pool with 4 workers (Om API, no shared context needed)
3131 Om.fromAff $ log "Creating worker pool..."
3232 (pool :: WorkerPool FibInput FibOutput) <- makePool
3333 { workerPath: "./dist/workers/FibonacciWorker.js"
src/Examples/SharedCounterDemo.purs+19−10
@@ -9,8 +9,9 @@ import Effect (Effect)
99import Effect.Aff (launchAff_)
1010import Effect.Class (liftEffect)
1111import Effect.Class.Console (log)
12+import Node.WorkerBees as WB
1213import Yoga.Om as Om
13−import Yoga.Om.WorkerBees (WorkerPool, makePoolWithData, distributeWork, terminatePool)
14+import Yoga.Om.WorkerBees (WorkerPool, makePool, distributeWork, terminatePool)
1415import Yoga.Om.WorkerBees.SharedInt as SharedInt
1516
1617-- | Console demo of SharedInt (atomic counter) across worker threads
@@ -23,22 +24,24 @@ import Yoga.Om.WorkerBees.SharedInt as SharedInt
2324type CounterInput = { n :: Int }
2425type CounterOutput = { result :: Int, count :: Int }
2526
26−counterDemo :: forall ctx. Om.Om ctx () Unit
27+-- Context is the SendWrapper for SharedInt - workers receive this
28+type CounterContext = WB.SendWrapper SharedInt.SharedInt
29+
30+counterDemo :: Om.Om CounterContext () Unit
2731counterDemo = do
2832 Om.fromAff $ log "=== Shared Counter Demo ==="
2933 Om.fromAff $ log ""
3034
31− -- Create shared counter starting at 0
32− Om.fromAff $ log "Creating shared counter..."
33− counter <- Om.fromAff $ liftEffect $ SharedInt.new 0
35+ -- Get shared counter from context
36+ Om.fromAff $ log "Creating worker pool (shared counter passed via context)..."
37+ counterWrapper <- Om.ask
38+ let counter = SharedInt.fromSendable counterWrapper
3439
35− -- Create worker pool with shared counter as workerData (Om API)
36− Om.fromAff $ log "Creating worker pool with shared counter..."
37− pool <- (makePoolWithData
40+ -- Create worker pool - workers automatically get the context
41+ (pool :: WorkerPool CounterInput CounterOutput) <- makePool
3842 { workerPath: "./dist/workers/CounterWorker.js"
3943 , numWorkers: 4
4044 }
41− (SharedInt.toSendable counter) :: Om.Om ctx () (WorkerPool CounterInput CounterOutput))
4245
4346 -- Send 100 increment tasks (each worker will increment the shared counter)
4447 let tasks = Array.range 1 100
@@ -68,4 +71,10 @@ counterDemo = do
6871 Om.fromAff $ log "Done!"
6972
7073main :: Effect Unit
71−main = launchAff_ $ Om.runOm unit { exception: throwError } counterDemo
74+main = launchAff_ do
75+ -- Create shared counter and wrap for sending to workers
76+ counter <- liftEffect $ SharedInt.new 0
77+ let counterContext = SharedInt.toSendable counter
78+
79+ -- Run Om with counter as context
80+ Om.runOm counterContext { exception: throwError } counterDemo
src/Yoga/Om/WorkerBees.purs+8−17
@@ -3,7 +3,6 @@ module Yoga.Om.WorkerBees
33 , PoolConfig
44 , defaultPoolConfig
55 , makePool
6− , makePoolWithData
76 , terminatePool
87 , distributeWork
98 , module Exports
@@ -42,28 +41,20 @@ defaultPoolConfig workerPath =
4241type WorkerPool input output = Pool.WorkerPool input output
4342
4443-- | Create a worker pool. Must be paired with `terminatePool`.
44+-- | Workers receive the current Om context via `ctx.workerData`.
45+-- | If you don't need shared data, use `Unit` as the context type.
4546makePool
4647 :: forall ctx errs input output
4748 . WB.Sendable input
4849 => WB.Sendable output
50+ => WB.Sendable ctx
4951 => PoolConfig
5052 -> Om ctx errs (WorkerPool input output)
51−makePool config = Om.fromAff do
52− let worker = WB.unsafeWorkerFromPath config.workerPath
53− Pool.make worker unit config.numWorkers
54−
55−-- | Create a worker pool with shared data accessible to all workers via `workerData`.
56−makePoolWithData
57− :: forall ctx errs input output a
58− . WB.Sendable input
59− => WB.Sendable output
60− => WB.Sendable a
61− => PoolConfig
62− -> a
63− -> Om ctx errs (WorkerPool input output)
64−makePoolWithData config workerData = Om.fromAff do
65− let worker = WB.unsafeWorkerFromPath config.workerPath
66− Pool.make worker workerData config.numWorkers
53+makePool config = do
54+ ctx <- Om.ask
55+ Om.fromAff do
56+ let worker = WB.unsafeWorkerFromPath config.workerPath
57+ Pool.make worker ctx config.numWorkers
6758
6859-- | Terminate a worker pool and all its threads.
6960terminatePool