package fiber
State variables
A "state variable" Svar.t
differs from Ivar.t
in that it can be updated multiple times. It is particularly useful if you'd like to observe the state of a process by subscribing to "interesting" (from your perspective) events.
For example, a build system can have a complex state machine, switching between states like "waiting for source file changes", "building", etc., by calling Svar.write
on the corresponding state variable. If you don't care about most of the states but would like to observe all errors, you can use Svar.wait ~until:is_error
to be woken up when an error happens. Other observers may have different preferences, expressed with different until
predicates. You can also use the non-blocking Svar.read
if you'd like to use polling instead.
val create : 'a -> 'a t
create init
creates a new state variable initialized with init
val read : 'a t -> 'a
read t
returns the current value of t
wait t ~until
waits until t
's value satisfies the predicate until
. If the current value satisfies it, wait
returns immediately, otherwise, the caller is blocked.