package bonsai

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

Source file test_handle_compute_lazy.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
51
52
53
open! Core
open! Import
open Bonsai_test
open Bonsai.For_open

let handle () =
  let var = Bonsai.Var.create 0 in
  let handle =
    Handle.create
      (module struct
        type t = int
        type incoming = unit

        let view i =
          print_endline "computing the view!";
          Int.to_string i
        ;;

        let incoming _i () = Effect.Ignore
      end)
      (Bonsai.read (Bonsai.Var.value var))
  in
  handle, var
;;

let%expect_test "Handle.show forces the view" =
  let handle, var = handle () in
  Handle.show handle;
  [%expect {|
    computing the view!
    0
    |}];
  Bonsai.Var.set var 1;
  Handle.show handle;
  [%expect {|
    computing the view!
    1
    |}]
;;

let%expect_test "Handle.recompute_view should _not_ force the view" =
  let handle, var = handle () in
  Handle.recompute_view handle;
  [%expect {| |}];
  Bonsai.Var.set var 1;
  Handle.recompute_view handle;
  [%expect {| |}];
  Handle.show handle;
  [%expect {|
    computing the view!
    1
    |}]
;;