@@ -2,17 +2,15 @@ module Examples.SharedCounterDemo where
22
33import Prelude
44
5−import Prelude
6−
7−import Control.Parallel (parTraverse)
5+import Control.Monad.Error.Class (throwError)
86import Data.Array as Array
97import Data.Traversable (for_)
108import Effect (Effect)
11−import Effect.Aff (Aff, launchAff_)
9+import Effect.Aff (launchAff_)
1210import Effect.Class (liftEffect)
1311import Effect.Class.Console (log)
14−import Node.WorkerBees.Aff.Pool as Pool
15−import Node.WorkerBees as WB
12+import Yoga.Om as Om
13+import Yoga.Om.WorkerBees (WorkerPool, makePoolWithData, distributeWork, terminatePool)
1614import Yoga.Om.WorkerBees.SharedInt as SharedInt
1715
1816-- | Console demo of SharedInt (atomic counter) across worker threads
@@ -25,47 +23,49 @@ import Yoga.Om.WorkerBees.SharedInt as SharedInt
2523type CounterInput = { n :: Int }
2624type CounterOutput = { result :: Int, count :: Int }
2725
28−counterDemo :: Aff Unit
26+counterDemo :: forall ctx. Om.Om ctx () Unit
2927counterDemo = do
30− log "=== Shared Counter Demo ==="
31− log ""
28+ Om.fromAff $ log "=== Shared Counter Demo ==="
29+ Om.fromAff $ log ""
3230
3331 -- Create shared counter starting at 0
34− log "Creating shared counter..."
35− counter <- liftEffect $ SharedInt.new 0
32+ Om.fromAff $ log "Creating shared counter..."
33+ counter <- Om.fromAff $ liftEffect $ SharedInt.new 0
3634
37− -- Create worker pool with shared counter as workerData
38− log "Creating worker pool with shared counter..."
39− let counterSendable = SharedInt.toSendable counter
40− let worker = (WB.unsafeWorkerFromPath "./dist/workers/CounterWorker.js" :: WB.Worker (WB.SendWrapper SharedInt.SharedInt) CounterInput CounterOutput)
41− pool <- Pool.make worker counterSendable 4
35+ -- Create worker pool with shared counter as workerData (Om API)
36+ Om.fromAff $ log "Creating worker pool with shared counter..."
37+ pool <- (makePoolWithData
38+ { workerPath: "./dist/workers/CounterWorker.js"
39+ , numWorkers: 4
40+ }
41+ (SharedInt.toSendable counter) :: Om.Om ctx () (WorkerPool CounterInput CounterOutput))
4242
4343 -- Send 100 increment tasks (each worker will increment the shared counter)
4444 let tasks = Array.range 1 100
4545 let counterInputs = map (\n -> { n }) tasks
4646
47− log "Distributing 100 increment tasks across workers..."
48− log ""
47+ Om.fromAff $ log "Distributing 100 increment tasks across workers..."
48+ Om.fromAff $ log ""
4949
50− results <- parTraverse (Pool.invoke pool) counterInputs
50+ results <- distributeWork pool counterInputs
5151
5252 -- Print some sample results (showing concurrent increments)
53− log "Sample results from workers:"
54− for_ (Array.take 10 results) \result -> do
53+ Om.fromAff $ log "Sample results from workers:"
54+ Om.fromAff $ for_ (Array.take 10 results) \result -> do
5555 log $ " Result: " <> show result.result <> ", Counter: " <> show result.count
5656
57− log "..."
58− log ""
57+ Om.fromAff $ log "..."
58+ Om.fromAff $ log ""
5959
6060 -- Read final counter value
61− finalValue <- liftEffect $ SharedInt.read counter
62− log $ "Final counter value: " <> show finalValue
61+ finalValue <- Om.fromAff $ liftEffect $ SharedInt.read counter
62+ Om.fromAff $ log $ "Final counter value: " <> show finalValue
6363
64− log ""
65− log "Cleaning up..."
66− Pool.terminate pool
64+ Om.fromAff $ log ""
65+ Om.fromAff $ log "Cleaning up..."
66+ terminatePool pool
6767
68− log "Done!"
68+ Om.fromAff $ log "Done!"
6969
7070main :: Effect Unit
71−main = launchAff_ counterDemo
71+main = launchAff_ $ Om.runOm unit { exception: throwError } counterDemo