package bonsai

  1. Overview
  2. Docs

Bonsai can be used with any Incremental-style UI framework. The parameters for the Bonsai component functor are an instance of Incremental (used to re-evaluate the UI only when the UI model has changed) and an opaque Event.t type (which is used to schedule actions).

The recommended use of this functor is to bind the name Bonsai to its invocation. For example, Bonsai_web's import.ml has:

module Incr = Incr_dom.Incr
module Vdom = Virtual_dom.Vdom
module Bonsai = Bonsai.Make (Incr) (Vdom.Event)

Bonsai_web re-exports the contents of its Import module, which allows users to refer to the module Bonsai to construct components.

Parameters

Signature

type 'custom mismatch_behavior = [
  1. | `Ignore
  2. | `Raise
  3. | `Warn
  4. | `Custom of 'custom
]

Default is `Ignore.

type ('extra, 'new_model) on_action_mismatch = ('extra -> 'new_model) mismatch_behavior
type ('input, 'model, 'result) t

The component type (('model, 'result) Bonsai.t) can be thought of as a function from 'model to 'result, but where the 'result can schedule events of the component's "action type". These actions are used to produce a new 'model which in turn causes the 'result to be recomputed. Instances of the 'result type can contain callbacks which schedule actions when interacted with by user (via button click, text input, etc). These actions are handled by the component's apply_action function, which yields a new model to be displayed.

type ('input, 'model, 'result) nonexpert_t := ('input, 'model, 'result) t

Component Constructors

val const : 'result -> (_, _, 'result) t

Returns a component with no action or model, only a constant result.

val pure : f:('input -> 'result) -> ('input, _, 'result) t

A pure function with no model from 'input to 'result

val pure_from_model : f:('model -> 'result) -> (_, 'model, 'result) t

Like pure, but gets the input from the 'model.

val compose : ('i1, 'model, 'r1) t -> ('r1, 'model, 'r2) t -> ('i1, 'model, 'r2) t

compose a b joins a and b together such that the result of a is used as the input of b.

module Infix : sig ... end

For composing components which share the same model. For example, applying an action in one component changes the shared model, which is reflected in the results of the other component.

include Core_kernel.Applicative.S3 with type ('r, 'i, 'm) t := ('i, 'm, 'r) t
val return : 'a -> (_, _, 'a) t
val map : ('d, 'e, 'a) t -> f:('a -> 'b) -> ('d, 'e, 'b) t
val both : ('d, 'e, 'a) t -> ('d, 'e, 'b) t -> ('d, 'e, 'a * 'b) t
val (<*>) : ('d, 'e, 'a -> 'b) t -> ('d, 'e, 'a) t -> ('d, 'e, 'b) t
val (<*) : ('d, 'e, 'a) t -> ('d, 'e, unit) t -> ('d, 'e, 'a) t
val (*>) : ('d, 'e, unit) t -> ('d, 'e, 'a) t -> ('d, 'e, 'a) t
val (>>|) : ('d, 'e, 'a) t -> ('a -> 'b) -> ('d, 'e, 'b) t
val apply : ('d, 'e, 'a -> 'b) t -> ('d, 'e, 'a) t -> ('d, 'e, 'b) t
val map2 : ('d, 'e, 'a) t -> ('d, 'e, 'b) t -> f:('a -> 'b -> 'c) -> ('d, 'e, 'c) t
val map3 : ('d, 'e, 'a) t -> ('d, 'e, 'b) t -> ('d, 'e, 'c) t -> f:('a -> 'b -> 'c -> 'result) -> ('d, 'e, 'result) t
val all : ('d, 'e, 'a) t list -> ('d, 'e, 'a list) t
val all_unit : ('d, 'e, unit) t list -> ('d, 'e, unit) t
module Applicative_infix : sig ... end
val map_input : ('i2, 'model, 'result) t -> f:('i1 -> 'i2) -> ('i1, 'model, 'result) t

Transforms the input of a component. The signature of f is reversed from most other map functions.

include Core_kernel.Applicative.Let_syntax3 with type ('r, 'i, 'm) t := ('i, 'm, 'r) t
module Open_on_rhs_intf : sig ... end
module Let_syntax : sig ... end
module type S = sig ... end

Many modules have the same shape, they declare the model, action, and result of the component, and then define apply_action and view over those types.

type ('input, 'model, 'action, 'result) component_s = (module S with type Action.t = 'action and type Input.t = 'input and type Model.t = 'model and type Result.t = 'result)
module M (Component : S) : sig ... end
val of_module : ('input, 'model, 'action, 'result) component_s -> ('input, 'model, 'result) t

Creates a component from a Component.S first class module.

module Incremental : sig ... end
module Expert : sig ... end

Combinators

module Project : sig ... end
module Map : sig ... end
module Option : sig ... end
module Either : sig ... end
module List_deprecated : sig ... end

Projecting over lists in Bonsai is fraught with issues. 1. Incremental can't be optimized for linked-list data structures. 2. Using list-index as the method for routing actions to components is basically never what you actually want, causing mis-delivery of events every time that the list changes.

OCaml

Innovation. Community. Security.