Imandrakit.Observer
Observer, to subscribe to some events.
The observer is thread-safe. However, it is recommended that subscribe
callbacks either run in a very short time, or queue heavier work on some background Executor_sig.S
.
val create : unit -> 'a t
val emit : 'a t -> 'a -> unit
val emit_lazy : 'a t -> (unit -> 'a) -> unit
emit_lazy obs f
emits the result of f()
, but only calls f()
if there's at least one observer to be notified.
Subscribe to the events. The callback can raise Unsubscribe
to remove itself.
val subscribe' : 'a t -> ('a -> unit) -> unit
Subscribe to the events. The callback can raise Unsubscribe
to remove itself.
val n_subscribers : _ t -> int
Current number of subscribers
val has_subscribers : _ t -> bool
Is there any subscriber?
val with_subscribe : 'a t -> ('a -> unit) -> (unit -> 'ret) -> 'ret
with_subscribe obs cb f
subscribes cb
to obs
, calls f()
, and when f()
is done or has raised, it unsubscribes cb
.