Aff. Worker. Link
parentLink :: forall dn up. Aff (Link dn up)
Construct bi-directional channels for a worker to be able to communicate with the parent thread.
module GreetWorker where import Prelude import Effect.Worker (Worker) import Effect.Aff (liftAff) import Effect.Aff.Worker (parentChannels) import Effect.Aff.Worker.Link as Link import Control.Monad.Rec.Class (forever) data GreetWorker = Worker String String main :: Effect Unit main = liftAff $ forever do link <- parentLink :: Aff (Link.Link String String) -- wait for parent to send us a name name <- Link.recv link -- send "hello, name!" to parent thread let msg = "Hello, " <> name <> "!" _ <- Link.send msg link mempty
workerLink :: forall dn up. Worker dn up -> Aff (Link dn up)
Construct a communication link for a Worker, allowing 2-way communication with a worker thread.
module Main where import Prelude import Effect.Console (log) import Effect.Aff (liftAff) import Effect.Aff.Worker.Link (workerLink) import Effect.Aff.Link as Link import Control.Monad.Rec.Class (forever) import GreetWorker (GreetWorker) foreign import spawnGreetWorker :: Effect GreetWorker main :: Effect Unit main = liftAff $ do worker <- spawnGreetWorker link <- workerLink worker Link.send "Harry" link greeting <- Link.recv link liftEffect $ log greeting mempty