package bonsai

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

A mutable-state tracker is meant to be used in concert with Vdom.Node.widget or Vdom.Attr.create_hook. Because a widget and hook can exist in multiple places in the dom at the same time, this state-tracker actually tracks a collection of states, which is why read returns a list, and the callback you pass to modify can get called multiple times per invocation.

unsafe_init should be called inside the widget or hooks's init function and is passed some subset of the widget's state. Then, you must store the returned Id.t in the widget's state. If unsafe_init is called, then you _must_ call unsafe_destroy with the returned ID or you risk leaking memory, and making the output of calls to read return stale data.

unsafe_destroy should be called inside the widget or hook's destroy function, and must be passed the same id that was returned from the corresponding create.

modify can be invoked to run some mutating function on the widget's state. It is legal to use this function anywhere that an Effect can be scheduled.

read provides access to the states of each instance managed by this mutable-state-tracker. You can use this function anywhere that an Effect can be scheduled.

module Id : Core.T
type 's t = {
  1. unsafe_init : 's -> Id.t;
  2. unsafe_destroy : Id.t -> unit;
  3. modify : ('s -> unit) -> unit Bonsai.For_open.Effect.t;
  4. read : 'a. ('s -> 'a) -> 'a list Bonsai.For_open.Effect.t;
}
val component : unit -> 's t Bonsai.For_open.Computation.t
OCaml

Innovation. Community. Security.