package bonsai

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

'a Effect.t represents some computation of type 'a that can be performed outside of the typical computational/incremental structure of a Bonsai program. Examples of this computation might be:

  • Calling an RPC and getting the result back
  • Running expensive computation on a web-worker thread
  • Requesting some information from the imperative "Start.Handle"-holding code

If you have a value of type 'a Effect.t, you can schedule it to be run by calling inject and providing a function that will be called when the callback completes.

type 'a t
include Core_kernel.Monad.S with type 'a t := 'a t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t

t >>= f returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t to yield a value v, and then runs the computation returned by f v.

val (>>|) : 'a t -> ('a -> 'b) -> 'b t

t >>| f is t >>= (fun a -> return (f a)).

module Monad_infix : sig ... end
val bind : 'a t -> f:('a -> 'b t) -> 'b t

bind t ~f = t >>= f

val return : 'a -> 'a t

return v returns the (trivial) computation that returns v.

val map : 'a t -> f:('a -> 'b) -> 'b t

map t ~f is t >>| f.

val join : 'a t t -> 'a t

join t is t >>= (fun t' -> t').

val ignore_m : 'a t -> unit t

ignore_m t is map t ~f:(fun _ -> ()). ignore_m used to be called ignore, but we decided that was a bad name, because it shadowed the widely used Caml.ignore. Some monads still do let ignore = ignore_m for historical reasons.

val all : 'a t list -> 'a list t
val all_unit : unit t list -> unit t

Like all, but ensures that every monadic value in the list produces a unit value, all of which are discarded rather than being collected into a list.

module Let_syntax : sig ... end

These are convenient to have in scope when programming with a monad:

val of_event : Virtual_dom.Vdom.Event.t -> unit t

Converts a Vdom.Event.t to an Effect.t. This can be useful if you want to use the monadic interface that is available via Effect.t in order to sequence events.

val of_deferred_fun : ('query -> 'result Async_kernel.Deferred.t) -> ('query -> 'result t) Core_kernel.Staged.t

of_deferred_fun is a way to convert from a deferred-returning function to an effect-returning function. This function is commonly used to wrap RPC calls. Memory is allocated permenantly every time that of_deferred_fun is called, so be sure to re-use the function inside the Staged.t!

val of_sync_fun : ('query -> 'result) -> ('query -> 'result t) Core_kernel.Staged.t

of_sync_fun is similar to of_deferred_fun but with a synchronous function instead of a deferred one. This can be used for functions that are synchronous but side-effecting, or as a mock-function in tests that replace the usages of of_deferred_fun in the actual app.

val never : _ t

An effect that never completes

val inject : 'a t -> on_response:('a -> Virtual_dom.Vdom.Event.t) -> Virtual_dom.Vdom.Event.t

Produces an event which schedules the some computation to be run. When that computation is complete, on_response is called with the value.

val inject_ignoring_response : unit t -> Virtual_dom.Vdom.Event.t

When the type being computed is unit, you are calling this Effect for side-effects that don't yield a value back. inject_ignoring_response frees you from having to pass an on_response callback.

val inject_with_userdata : 'a t -> userdata:'u -> on_response:('a -> 'u -> Virtual_dom.Vdom.Event.t) -> Virtual_dom.Vdom.Event.t

inject_with_userdata is the same as inject, but you have the option of passing in some extra value which will also be available in the on_response callback.

val handle_error : ('ok, 'error) Core_kernel.Result.t t -> f:('error -> Virtual_dom.Vdom.Event.t) -> 'ok t

Transforms a result-returing Effect.t into an Effect.t that doesn't have it's error case, by providing a callback that handles the error condition.

OCaml

Innovation. Community. Security.