@@ -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