git.iko.soy/rowtype-yoga/purescript-yoga-om-workerbees
on main help sign in
Convert examples to use Om API instead of raw Aff

- FibonacciDemo now uses makePool/distributeWork/terminatePool Om functions - SharedCounterDemo uses makePoolWithData Om function - Examples demonstrate the Om convenience layer over WorkerBees - Properly use Om.runOm with context and exception handlers - SharedState and SharedMap demos remain Aff-based (don't need Om) Note: SharedCounterDemo still hangs - needs debugging of worker communication

commitf250ebad9d2fcfb064c12e10817463860c049ed4 authorMark <mark.eibes@gmail.com> dateSat 7 Feb 2026, 09:24 (8 months ago) parent4562f5a
2 files changed+53−53
src/Examples/FibonacciDemo.purs+23−23 src/Examples/SharedCounterDemo.purs+30−30
src/Examples/FibonacciDemo.purs+23−23
@@ -2,17 +2,15 @@ module Examples.FibonacciDemo 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 Data.Tuple (Tuple(..))
119import Effect (Effect)
12−import Effect.Aff (Aff, launchAff_)
10+import Effect.Aff (launchAff_)
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, makePool, distributeWork, terminatePool)
1614
1715-- | Simple console demo of Fibonacci calculation using worker pool
1816-- |
@@ -24,35 +22,37 @@ import Node.WorkerBees as WB
2422type FibInput = { n :: Int }
2523type FibOutput = { result :: Int, thread :: Int }
2624
27−fibDemo :: Aff Unit
25+fibDemo :: forall ctx. Om.Om ctx () Unit
2826fibDemo = do
29− log "=== Fibonacci Worker Demo ==="
30− log ""
27+ Om.fromAff $ log "=== Fibonacci Worker Demo ==="
28+ Om.fromAff $ log ""
3129
32− -- Create worker pool with 4 workers
33− log "Creating worker pool..."
34− let worker = (WB.unsafeWorkerFromPath "./dist/workers/FibonacciWorker.js" :: WB.Worker Unit FibInput FibOutput)
35− pool <- Pool.make worker unit 4
30+ -- Create worker pool with 4 workers (Om API)
31+ Om.fromAff $ log "Creating worker pool..."
32+ (pool :: WorkerPool FibInput FibOutput) <- makePool
33+ { workerPath: "./dist/workers/FibonacciWorker.js"
34+ , numWorkers: 4
35+ }
3636
3737 -- Test inputs: Calculate Fibonacci for these numbers
3838 let inputs = [35, 36, 37, 38, 39, 40]
3939 let fibInputs = map (\n -> { n }) inputs
4040
41− log $ "Calculating Fibonacci for: " <> show inputs
42− log ""
41+ Om.fromAff $ log $ "Calculating Fibonacci for: " <> show inputs
42+ Om.fromAff $ log ""
4343
44− -- Distribute work and collect results
45− results <- parTraverse (Pool.invoke pool) fibInputs
44+ -- Distribute work using Om API
45+ results <- distributeWork pool fibInputs
4646
4747 -- Print results
48− for_ (Array.zip inputs results) \(Tuple input result) -> do
48+ Om.fromAff $ for_ (Array.zip inputs results) \(Tuple input result) -> do
4949 log $ "fib(" <> show input <> ") = " <> show result.result
5050
51− log ""
52− log "Cleaning up..."
53− Pool.terminate pool
51+ Om.fromAff $ log ""
52+ Om.fromAff $ log "Cleaning up..."
53+ terminatePool pool
5454
55− log "Done!"
55+ Om.fromAff $ log "Done!"
5656
5757main :: Effect Unit
58−main = launchAff_ fibDemo
58+main = launchAff_ $ Om.runOm unit { exception: throwError } fibDemo
src/Examples/SharedCounterDemo.purs+30−30
@@ -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