Safe Haskell | None |
---|
Wraps Haste.Concurrent to work with Haste.App. Task switching happens whenever a thread is blocked in an MVar, so things like polling an IORef in a loop will starve all other threads.
This will likely be the state of Haste concurrency until Javascript gains decent native concurrency support.
- data MVar a
- data MBox t a
- data Send
- data Recv
- type Inbox = MBox Recv
- type Outbox = MBox Send
- class Monad m => MonadConc m where
- forkMany :: [Client ()] -> Client ()
- newMVar :: a -> Client (MVar a)
- newEmptyMVar :: Client (MVar a)
- takeMVar :: MVar a -> Client a
- putMVar :: MVar a -> a -> Client ()
- peekMVar :: MVar a -> Client (Maybe a)
- readMVar :: MVar a -> Client a
- spawn :: MonadConc m => (Inbox a -> m ()) -> m (Outbox a)
- receive :: MonadConc m => Inbox a -> m a
- statefully :: MonadConc m => st -> (st -> evt -> m (Maybe st)) -> m (Outbox evt)
- (!) :: MonadConc m => Outbox a -> a -> m ()
- (<!) :: MonadConc m => Outbox a -> m a -> m ()
- forever :: Monad m => m a -> m b
Documentation
An MBox is a read/write-only MVar, depending on its first type parameter. Used to communicate with server processes.
newEmptyMVar :: Client (MVar a)Source
Create a new empty MVar.
takeMVar :: MVar a -> Client aSource
Read the value of an MVar. If the MVar is empty, takeMVar
blocks until
a value arrives. takeMVar
empties the MVar.
putMVar :: MVar a -> a -> Client ()Source
Put a value into an MVar. If the MVar is full, putMVar
will block until
the MVar is empty.
peekMVar :: MVar a -> Client (Maybe a)Source
Read an MVar without affecting its contents.
If the MVar is empty, peekMVar
immediately returns Nothing
.
readMVar :: MVar a -> Client aSource
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.
spawn :: MonadConc m => (Inbox a -> m ()) -> m (Outbox a)Source
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.
receive :: MonadConc m => Inbox a -> m aSource
Block until a message arrives in a mailbox, then return it.
statefully :: MonadConc m => st -> (st -> evt -> m (Maybe st)) -> m (Outbox evt)Source
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 ()Source
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.