lvar-0.1.0.0: TMVar that can be listened to
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.LVar

Description

LVar is like Control.Concurrent.STM.TMVar but with a capability for listening to its changes.

Synopsis

Types

data LVar a Source #

Creating a LVar

new :: forall a m. MonadIO m => a -> m (LVar a) Source #

Create a new LVar with the given initial value

empty :: MonadIO m => m (LVar a) Source #

Like new, but there is no initial value. A get will block until an initial value is set using set or modify

Modifying a LVar

get :: MonadIO m => LVar a -> m a Source #

Get the value of the LVar

set :: MonadIO m => LVar a -> a -> m () Source #

Set the LVar value; active listeners are automatically notifed.

modify :: MonadIO m => LVar a -> (a -> a) -> m () Source #

Modify the LVar value; active listeners are automatically notified.

Listening to a LVar

addListener :: MonadIO m => LVar a -> m ListenerId Source #

Create a listener for changes to the LVar, as they are set by set or modify from this time onwards.

You must call listenNext to get the next updated value (or current value if there is one).

Returns a ListenerId that can be used to stop listening later (via removeListener)

listenNext :: MonadIO m => LVar a -> ListenerId -> m a Source #

Listen for the next value update (since the last listenNext or addListener). Unless the LVar was empty when addListener was invoked, the first invocation of listenNext will return the current value even if there wasn't an update. Therefore, the *first* call to listenNext will *always* return immediately, unless the LVar is empty.

Call this in a loop to listen on a series of updates.

Throws ListenerDead if called with a ListenerId that got already removed by removeListener.

removeListener :: MonadIO m => LVar a -> ListenerId -> m () Source #

Stop listening to the LVar