Module Machine.Observation

Observations interface.

val observe : 'a observation -> ('a -> unit t) -> unit t

observe obs on_observation subscribes to the given observation obs. Every time the observation obs is made a function on_observation is called. The function can perform arbitrary computations in the machine monad, e.g., make its own computations, or access other components via their interfaces.

val subscribe : 'a observation -> ('a -> unit t) -> subscription t

subscribe obs handler creates a cancelable subscription to the observation obs.

Returns a subscription handler that could be used to cancel the subscription.

  • since 2.1.0
val cancel : subscription -> unit t

cancel sub cancels the given subscription.

An observation that was registered under this subscription won't be called anymore.

  • since 2.1.0
val watch : Observation.provider -> (Core_kernel.Sexp.t -> unit t) -> unit t

watch prov data watches for the data provider.

This function is the same as observe except that it uses the provider to access the observation and gets the observation in the sexp-serialized form.

val make : 'a statement -> 'a -> unit t

make observation event make an observation of the given event.

val post : 'a statement -> f:(('a -> unit t) -> unit t) -> unit t

post observation k makes observation if necessary.

The continuation k is a function that is called only when the given statement has subscribers.

Use this function to skip creating an observation if nobody is interested in it. This is useful, when the observation has some cost to construct, so when there no subscribers no machine cycles will be lost.

The function k receives a provide function that could be used to provide observation once it is ready, e.g.,

Observation.post big_thing ~f:(fun provide ->
    some_costly_function1 >>= fun x ->
    some_costly_function2 >>= fun y ->
    some_costly_function3 >>= fun z ->
    provide (x,y,z))

Note: even for observations that are tuples this function is efficient as sometimes the compiler can optimize the closure creation or the closure itself might be smaller than the created observation.

  • since 2.1.0