Package

purescript-web-workers

Repository
gbagan/purescript-web-workers
License
MIT
Uploaded by
gbagan
Published on
2022-05-22

Type definitions and low level interface implementations for the W3C Web Workers API.

Install

spago install web-workers

Documentation

Published on Pursuit

Example

code for the worker

module Test.Worker where

import Prelude
import Data.Either (Either(..))
import Effect (Effect)
import Control.Monad.Except (runExcept)
import Foreign (readInt)
import Web.Worker.GlobalScope (postMessage, onMessage)
import Web.Worker.MessageEvent (data_)
import Effect.Console (log)

main :: Effect Unit
main = do
  onMessage \ev -> do
    case runExcept $ readInt $ data_ ev of
      Left _ -> log "Sent data is not an integer"
      Right n -> postMessage $ n * n

main code

module Main where

import Prelude
import Data.Either (Either(..))
import Control.Monad.Except (runExcept)
import Foreign (readInt)
import Effect (Effect)
import Effect.Console (log)
import Web.Worker.Worker (defaultWorkerOptions, new, postMessage, onMessage)
import Web.Worker.MessageEvent (data_)

main :: Effect Unit
main = do
  worker <- new "./worker.js" defaultWorkerOptions
  worker
    # onMessage \ev -> case runExcept $ readInt $ data_ ev of
        Left _ -> log "Received data is not an integer"
        Right n -> log $ "Received integer from worker: " <> show n
  worker # postMessage 4
  worker # postMessage 5