transient-0.1.0.8: A monad for extensible effects and primitives for unrestricted composability of applications

Safe HaskellNone
LanguageHaskell2010

Transient.EVars

Synopsis

Documentation

newtype EVars Source

Constructors

EVars (IORef (Map Int [EventF])) 

data EVar a Source

Constructors

EVar Int (IORef (Maybe a)) 

newEVar :: TransientIO (EVar a) Source

creates an EVar.

Evars are event vars. writeEVar trigger the execution of all the continuations associated to the readEVar of this variable (the code that is after them) as stack: the most recent reads are executed first.

It is like the publish-subscribe pattern but without inversion of control, since a readEVar can be inserted at any place in the Transient flow.

EVars are created upstream and can be used to communicate two sub-threads of the monad. Following the Transient philosophy they do not block his own thread if used with alternative operators, unlike the IORefs and TVars. And unlike STM vars, that are composable, they wait for their respective events, while TVars execute the whole expression when any variable is modified.

The execution continues after the writeEVar when all subscribers have been executed.

see https://www.fpcomplete.com/user/agocorona/publish-subscribe-variables-transient-effects-v

readEVar :: EVar a -> TransIO a Source

read the EVar. It only succeed when the EVar is being updated The continuation gets registered to be executed whenever the variable is updated. if readEVar is in any kind of loop, since each continuation is different, this will register again the continuation. The effect is that the continuation will be executed multiple times To avoid multiple registrations, use unsubscribe

writeEVar :: EVar t -> t -> TransIO () Source

update the EVar and execute all readEVar blocks with last in - first out priority

unsubscribe :: EVar t -> TransIO () Source

unsuscribe the last readEVar executed for this EVar