Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
A transactional signal type. Similar to a broadcast channel, but with better memory characteristics when you only care about the latest value.
Allows multiple consumers to detect the latest value of a signal, and to be notified when the signal changes.
Synopsis
- newSignalIO :: MonadIO m => Maybe a -> m (Signal a)
- writeSignal :: Signal a -> a -> STM ()
- writeSignalIO :: MonadIO m => Signal a -> a -> m ()
- subscribe :: MonadIO m => Signal a -> m (STM a)
- data Signal a
Documentation
newSignalIO :: MonadIO m => Maybe a -> m (Signal a) Source #
Create a new signal with an optional initial value.
writeSignal :: Signal a -> a -> STM () Source #
Update the value of a signal, notifying all subscribers (even if the value didn't change)
writeSignalIO :: MonadIO m => Signal a -> a -> m () Source #
Update the value of a signal, notifying all subscribers (even if the value didn't change)
subscribe :: MonadIO m => Signal a -> m (STM a) Source #
Subscribe to a signal, returning an STM action which will read the latest NEW value, after successfully reading a new value, subsequent reads will retry until there's a new value written to the signal.
Each independent reader should have its own subscription.
>>>
signal <- newSignalIO (Just "initial")
>>>
subscriber1 <- subscribe signal
>>>
subscriber2 <- subscribe signal
>>>
-- Should return the initial value
>>>
atomically (optional subscriber1)
>>>
-- Should retry, since the signal hasn't changed.
>>>
atomically (optional subscriber1)
>>>
writeSignalIO signal "new value"
>>>
-- Each subscriber should return the newest value
>>>
("sub1",) <$> atomically (optional subscriber1)
>>>
("sub2",) <$> atomically (optional subscriber2)
>>>
-- Both should now retry
>>>
("sub1",) <$> atomically (optional subscriber1)
>>>
("sub2",) <$> atomically (optional subscriber2)
Just "initial" Nothing ("sub1",Just "new value") ("sub2",Just "new value") ("sub1",Nothing) ("sub2",Nothing)