package bonsai

  1. Overview
  2. Docs
A library for building dynamic webapps, using Js_of_ocaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=c78c4476ee6b856846e2d0941e5965009d5e1b853e564b2b1bee61202f0b1ebb

doc/src/bonsai/input.ml.html

Source file input.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
open! Core
open! Import

(* This type represents a combination of static and dynamic data. When only one
   is present, the representation is straightforward, but when both are
   present, we use the [Join] constructor. [Join] contains the dynamic portion
   as a single incremental node (which probably contains a tuple-tree built up
   with several [both] nodes), and the static portion is contained implicitly
   within the function, call it [f]. [f] rearranges the ['a] by inserting all
   the pieces of static data into their appropriates places in the tuple tree. *)
type 'input t =
  | Dynamic : 'input Incr.t -> 'input t
  | Static : 'input -> 'input t
  | Join : 'a Incr.t * ('a -> 'b) -> 'b t

let dynamic input = Dynamic input
let static = Static ()

let map t ~f =
  match t with
  | Dynamic input -> Dynamic (Incr.map input ~f)
  | Static input -> Static (f input)
  | Join (input, g) -> Join (input, fun x -> f (g x))
;;

let iter_incremental t ~f =
  match t with
  | Dynamic incr -> f (Incr.pack incr)
  | Static _ -> ()
  | Join (incr, _) -> f (Incr.pack incr)
;;

let to_incremental = function
  | Dynamic input -> input
  | Static input -> Incr.return input
  | Join (incr, f) -> Incr.map incr ~f
;;

let merge a b =
  match a, b with
  | Dynamic a, Dynamic b -> Dynamic (Incr.both a b)
  | Dynamic a, Static b -> Join (a, fun a -> a, b)
  | Static a, Dynamic b -> Join (b, fun b -> a, b)
  | Static a, Static b -> Static (a, b)
  | Dynamic a, Join (b, f) -> Join (Incr.both a b, fun (a, b) -> a, f b)
  | Static a, Join (b, f) -> Join (b, fun b -> a, f b)
  | Join (a, f), Static b -> Join (a, fun a -> f a, b)
  | Join (a, f), Dynamic b -> Join (Incr.both a b, fun (a, b) -> f a, b)
  | Join (a, f), Join (b, g) -> Join (Incr.both a b, fun (a, b) -> f a, g b)
;;