haste-compiler-0.5.5.0: Haskell To ECMAScript compiler

Safe HaskellNone
LanguageHaskell98

Haste.Concurrent

Description

Concurrency for Haste. Includes MVars, forking, Ajax and more.

Synopsis

Documentation

data MVar a

data CIO a

Concurrent IO monad. The normal IO monad does not have concurrency capabilities with Haste. This monad is basically IO plus concurrency.

class ToConcurrent a where

Embed concurrent computations into non-concurrent ones.

Associated Types

type Async a

Methods

async :: Async a -> a

Instances

class Monad m => MonadConc m where

Any monad which supports concurrency.

Methods

liftConc :: CIO a -> m a

fork :: m () -> m ()

Instances

forkIO :: CIO () -> CIO ()

Spawn a new thread.

forkMany :: [CIO ()] -> CIO ()

Spawn several threads at once.

newMVar :: MonadIO m => a -> m (MVar a)

Create a new MVar with an initial value.

newEmptyMVar :: MonadIO m => m (MVar a)

Create a new empty MVar.

takeMVar :: MVar a -> CIO a

Read an MVar. Blocks if the MVar is empty. Only the first writer in the write queue, if any, is woken.

putMVar :: MVar a -> a -> CIO ()

Write an MVar. Blocks if the MVar is already full. Only the first reader in the read queue, if any, is woken.

withMVarIO :: MVar a -> (a -> IO b) -> CIO b

Perform an IO action over an MVar.

peekMVar :: MonadIO m => MVar a -> m (Maybe a)

Peek at the value inside a given MVar, if any, without removing it.

modifyMVarIO :: MVar a -> (a -> IO (a, b)) -> CIO b

Perform an IO action over an MVar, then write the MVar back.

readMVar :: MVar a -> CIO a

Read an MVar then put it back. As Javascript is single threaded, this function is atomic. If this ever changes, this function will only be atomic as long as no other thread attempts to write to the MVar.

concurrent :: CIO () -> IO ()

Run a concurrent computation. Two different concurrent computations may share MVars; if this is the case, then a call to concurrent may return before all the threads it spawned finish executing.

liftIO :: MonadIO m => forall a. IO a -> m a

Lift a computation from the IO monad.

data Recv

data Send

type Inbox = MBox Recv

data MBox t a

An MBox is a read/write-only MVar, depending on its first type parameter. Used to communicate with server processes.

receive :: MonadConc m => Inbox a -> m a

Block until a message arrives in a mailbox, then return it.

spawn :: MonadConc m => (Inbox a -> m ()) -> m (Outbox a)

Creates a generic process and returns a MBox which may be used to pass messages to it. While it is possible for a process created using spawn to transmit its inbox to someone else, this is a very bad idea; don't do it.

statefully :: MonadConc m => st -> (st -> evt -> m (Maybe st)) -> m (Outbox evt)

Creates a generic stateful process. This process is a function taking a state and an event argument, returning an updated state or Nothing. statefully creates a MBox that is used to pass events to the process. Whenever a value is written to this MBox, that value is passed to the process function together with the function's current state. If the process function returns Nothing, the process terminates. If it returns a new state, the process again blocks on the event MBox, and will use the new state to any future calls to the server function.

(!) :: MonadConc m => Outbox a -> a -> m ()

Write a value to a MBox. Named after the Erlang message sending operator, as both are intended for passing messages to processes. This operation does not block until the message is delivered, but returns immediately.

(<!) :: MonadConc m => Outbox a -> m a -> m ()

Perform a Client computation, then write its return value to the given pipe. Mnemonic: the operator is a combination of <- and !. Just like (!), this operation is non-blocking.

wait :: Int -> CIO ()

Wait for n milliseconds.