package bonsai

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

Source file test_value_stability.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
open! Core
open! Import
open Bonsai.For_open
open Bonsai.Let_syntax
open Proc

(* A big focus of the tests in this file is about making sure that there are no
   "in-transit" frames - frames during which the result has some intermediate
   value because lifecycle events haven't yet run. To accomplish this goal, all
   the tests have been written in a [Common] functor, which accepts a module
   that specifies what [Handle.show] should do. We supply three different
   answers to that question:

   - it should do what it normally does.
   - it should recompute_view an extra time prior to calling Handle.show.
   - it should recompute_view_until_stable prior to calling Handle.show.

   We do this to ensure that all the functions being tested behave the same no
   matter which one of those options is chosen. *)

let advance_by_sec handle seconds =
  Handle.advance_clock_by handle (Time_ns.Span.of_sec seconds)
;;

let%test_module "Bonsai_extra.with_last_modified_time" =
  (module struct
    module Common (M : sig
        val with_last_modified_time
          :  equal:('a -> 'a -> bool)
          -> 'a Value.t
          -> ('a * Time_ns.t) Computation.t

        val show_handle : ('a, 'b) Handle.t -> unit
      end) =
    struct
      let show = M.show_handle

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let v = Bonsai.Var.value v' in
        let c = M.with_last_modified_time ~equal:Int.equal v in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = int * Time_ns.Alternate_sexp.t [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| (1 "1970-01-01 00:00:00Z") |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| (1 "1970-01-01 00:00:00Z") |}];
        Bonsai.Var.set v' 2;
        show handle;
        [%expect {| (2 "1970-01-01 00:00:01Z") |}];
        Bonsai.Var.set v' 3;
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| (3 "1970-01-01 00:00:02Z") |}];
        show handle;
        [%expect {| (3 "1970-01-01 00:00:02Z") |}]
      ;;

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let on' = Bonsai.Var.create true in
        let v = Bonsai.Var.value v' in
        let on = Bonsai.Var.value on' in
        let c =
          match%sub on with
          | true ->
            let%sub x = M.with_last_modified_time ~equal:Int.equal v in
            let%arr x = x in
            Some x
          | false -> Bonsai.const None
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = (int * Time_ns.Alternate_sexp.t) option [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| ((1 "1970-01-01 00:00:00Z")) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| ((1 "1970-01-01 00:00:00Z")) |}];
        Bonsai.Var.set on' false;
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set on' true;
        show handle;
        [%expect {| ((1 "1970-01-01 00:00:01Z")) |}];
        Bonsai.Var.set on' false;
        show handle;
        [%expect {| () |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set on' true;
        show handle;
        [%expect {| ((1 "1970-01-01 00:00:02Z")) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| ((1 "1970-01-01 00:00:02Z")) |}];
        Bonsai.Var.set v' 2;
        show handle;
        [%expect {| ((2 "1970-01-01 00:00:03Z")) |}]
      ;;
    end

    module _ = Common (struct
        let with_last_modified_time = Bonsai_extra.with_last_modified_time
        let show_handle = Handle.show
      end)

    module _ = Common (struct
        let with_last_modified_time = Bonsai_extra.with_last_modified_time

        let show_handle handle =
          Handle.recompute_view handle;
          Handle.show handle
        ;;
      end)

    module _ = Common (struct
        let with_last_modified_time = Bonsai_extra.with_last_modified_time

        let show_handle handle =
          Handle.recompute_view_until_stable handle;
          Handle.show handle
        ;;
      end)
  end)
;;

let%test_module "Bonsai_extra.is_stable" =
  (module struct
    module Common (M : sig
        val is_stable
          :  equal:('a -> 'a -> bool)
          -> 'a Value.t
          -> time_to_stable:Time_ns.Span.t
          -> bool Computation.t

        val show_handle : ('a, 'b) Handle.t -> unit
      end) =
    struct
      let show = M.show_handle

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let v = Bonsai.Var.value v' in
        let c =
          let%sub is_stable =
            M.is_stable ~equal:Int.equal v ~time_to_stable:(Time_ns.Span.of_sec 1.0)
          in
          return (Value.both v is_stable)
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = int * bool [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| (1 false) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| (1 true) |}];
        Bonsai.Var.set v' 2;
        show handle;
        [%expect {| (2 false) |}];
        advance_by_sec handle 0.5;
        show handle;
        [%expect {| (2 false) |}];
        Bonsai.Var.set v' 3;
        show handle;
        [%expect {| (3 false) |}];
        advance_by_sec handle 0.5;
        show handle;
        [%expect {| (3 false) |}];
        advance_by_sec handle 0.5;
        show handle;
        [%expect {| (3 true) |}];
        Bonsai.Var.set v' 4;
        show handle;
        [%expect {| (4 false) |}];
        advance_by_sec handle 1.0;
        Bonsai.Var.set v' 5;
        show handle;
        [%expect {| (5 false) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| (5 true) |}];
        advance_by_sec handle 0.5;
        Bonsai.Var.set v' 4;
        show handle;
        [%expect {| (4 false) |}];
        advance_by_sec handle 0.5;
        Bonsai.Var.set v' 5;
        show handle;
        [%expect {| (5 false) |}]
      ;;

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let on' = Bonsai.Var.create true in
        let v = Bonsai.Var.value v' in
        let on = Bonsai.Var.value on' in
        let c =
          match%sub on with
          | true ->
            let%sub x =
              M.is_stable ~equal:Int.equal v ~time_to_stable:(Time_ns.Span.of_sec 1.0)
            in
            let%arr x = x
            and v = v in
            Some (x, v)
          | false -> Bonsai.const None
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = (bool * int) option [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| ((false 1)) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| ((true 1)) |}];
        Bonsai.Var.set on' false;
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set on' true;
        show handle;
        [%expect {| ((false 1)) |}]
      ;;

      let%expect_test {|zero values for the timespan should be permitted (but issue a warning) and always return false |}
        =
        let v' = Bonsai.Var.create 1 in
        let on' = Bonsai.Var.create true in
        let v = Bonsai.Var.value v' in
        let on = Bonsai.Var.value on' in
        let c =
          match%sub on with
          | true ->
            let%sub x =
              M.is_stable ~equal:Int.equal v ~time_to_stable:(Time_ns.Span.of_sec 0.0)
            in
            let%arr x = x
            and v = v in
            Some (x, v)
          | false -> Bonsai.const None
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = (bool * int) option [@@deriving sexp]
               end))
            c
        in
        [%expect {| "Bonsai_extra.is_stable: [time_to_stable] should be positive" |}];
        show handle;
        [%expect {| ((false 1)) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| ((false 1)) |}];
        Bonsai.Var.set on' false;
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set on' true;
        show handle;
        [%expect {| ((false 1)) |}]
      ;;

      let%expect_test {|negative values for the timespan should be permitted (but issue a warning) and always return false |}
        =
        let v' = Bonsai.Var.create 1 in
        let v = Bonsai.Var.value v' in
        let c =
          M.is_stable ~equal:Int.equal v ~time_to_stable:(Time_ns.Span.of_sec (-1.0))
        in
        [%expect {| "Bonsai_extra.is_stable: [time_to_stable] should be positive" |}];
        let handle = Handle.create (Result_spec.sexp (module Bool)) c in
        show handle;
        [%expect {| false |}]
      ;;
    end

    module _ = Common (struct
        let is_stable = Bonsai_extra.is_stable
        let show_handle = Handle.show
      end)

    module _ = Common (struct
        let is_stable = Bonsai_extra.is_stable

        let show_handle handle =
          Handle.recompute_view handle;
          Handle.show handle
        ;;
      end)

    module _ = Common (struct
        let is_stable = Bonsai_extra.is_stable

        let show_handle handle =
          Handle.recompute_view_until_stable handle;
          Handle.show handle
        ;;
      end)
  end)
;;

let%test_module "Bonsai.most_recent_value_satisfying" =
  (module struct
    module Common (M : sig
        val most_recent_value_satisfying
          :  (module Bonsai.Model with type t = 'a)
          -> 'a Value.t
          -> condition:('a -> bool)
          -> 'a option Computation.t

        val show_handle : ('a, 'b) Handle.t -> unit
      end) =
    struct
      let show = M.show_handle

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let v = Bonsai.Var.value v' in
        let c =
          M.most_recent_value_satisfying (module Int) v ~condition:(fun x -> x % 2 = 0)
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = int option [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set v' 2;
        show handle;
        [%expect {| (2) |}];
        Bonsai.Var.set v' 3;
        show handle;
        [%expect {| (2) |}];
        Bonsai.Var.set v' 4;
        show handle;
        [%expect {| (4) |}]
      ;;

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let on' = Bonsai.Var.create true in
        let v = Bonsai.Var.value v' in
        let on = Bonsai.Var.value on' in
        let c =
          match%sub on with
          | true ->
            let%sub x =
              M.most_recent_value_satisfying
                (module Int)
                v
                ~condition:(fun x -> x % 2 = 0)
            in
            let%arr x = x in
            Some x
          | false -> Bonsai.const None
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = int option option [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| (()) |}];
        Bonsai.Var.set v' 2;
        show handle;
        [%expect {| ((2)) |}];
        Bonsai.Var.set on' false;
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set v' 3;
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set on' true;
        show handle;
        [%expect {| ((2)) |}]
      ;;
    end

    module _ = Common (struct
        let most_recent_value_satisfying = Bonsai.most_recent_value_satisfying
        let show_handle = Handle.show
      end)

    module _ = Common (struct
        let most_recent_value_satisfying = Bonsai.most_recent_value_satisfying

        let show_handle handle =
          Handle.recompute_view handle;
          Handle.show handle
        ;;
      end)

    module _ = Common (struct
        let most_recent_value_satisfying = Bonsai.most_recent_value_satisfying

        let show_handle handle =
          Handle.recompute_view_until_stable handle;
          Handle.show handle
        ;;
      end)
  end)
;;

let%test_module "Bonsai_extra.value_stability" =
  (module struct
    let alternate_value_stability_implementation
          (type a)
          (module M : Bonsai.Model with type t = a)
          input
          ~time_to_stable
      =
      let%sub input =
        (* apply cutoff as an optimistic performance improvement *)
        Bonsai.Incr.value_cutoff input ~equal:M.equal
      in
      let module T = struct
        module Model = struct
          type stability =
            | Inactive of { previously_stable : M.t option }
            | Unstable of
                { previously_stable : M.t option
                ; unstable_value : M.t
                }
            | Stable of M.t
          [@@deriving sexp, equal]

          let set_value new_value = function
            | Inactive { previously_stable } ->
              Unstable { previously_stable; unstable_value = new_value }
            | Stable stable ->
              Unstable { previously_stable = Some stable; unstable_value = new_value }
            | Unstable { previously_stable; unstable_value = _ } ->
              Unstable { previously_stable; unstable_value = new_value }
          ;;

          type t =
            { stability : stability
            ; time_to_next_stable : Time_ns.Alternate_sexp.t option
            }
          [@@deriving sexp, equal]

          let default =
            { stability = Inactive { previously_stable = None }
            ; time_to_next_stable = None
            }
          ;;
        end

        module Action = struct
          type t =
            | Deactivate
            | Bounce of M.t * Time_ns.Alternate_sexp.t
            | Set_stable of M.t * Time_ns.Alternate_sexp.t
          [@@deriving sexp_of]
        end
      end
      in
      let open T in
      let%sub { stability; time_to_next_stable }, inject =
        Bonsai.state_machine0
          (module Model)
          (module Action)
          ~default_model:Model.default
          ~apply_action:(fun ~inject:_ ~schedule_event:_ model action ->
            match action, model with
            | Deactivate, { stability; _ } ->
              let stability =
                match stability with
                | Inactive _ -> stability
                | Unstable { previously_stable; _ } -> Inactive { previously_stable }
                | Stable stable -> Inactive { previously_stable = Some stable }
              in
              (* Deactivating this component will automatically cause the value to be
                 considered unstable.  This is because we have no way to tell what is
                 happening to the value when this component is inactive, and I consider
                 it safer to assume instability than to assume stability. *)
              { stability; time_to_next_stable = None }
            | Bounce (new_value, now), { stability; _ } ->
              (* Bouncing will cause the value to become unstable, and set the
                 time-to-next-stable to the provided value. *)
              let stability = Model.set_value new_value stability in
              let time_to_next_stable = Some (Time_ns.add now time_to_stable) in
              { stability; time_to_next_stable }
            | Set_stable (stable, now), { stability; time_to_next_stable } ->
              (* Sets the value which is considered to be stable and resets
                 the time until next stability. *)
              (match stability with
               | Inactive { previously_stable } ->
                 { stability = Unstable { previously_stable; unstable_value = stable }
                 ; time_to_next_stable = Some (Time_ns.add now time_to_stable)
                 }
               | Stable previously_stable ->
                 if M.equal previously_stable stable
                 then { stability = Stable stable; time_to_next_stable = None }
                 else
                   { stability =
                       Unstable
                         { unstable_value = stable
                         ; previously_stable = Some previously_stable
                         }
                   ; time_to_next_stable = Some (Time_ns.add now time_to_stable)
                   }
               | Unstable { unstable_value; previously_stable } ->
                 let candidate_time_to_next_stable = Time_ns.add now time_to_stable in
                 (match M.equal unstable_value stable, time_to_next_stable with
                  | true, Some time_to_next_stable
                    when Time_ns.( >= ) now time_to_next_stable ->
                    { stability = Stable stable; time_to_next_stable = None }
                  | _ ->
                    { stability = Unstable { unstable_value = stable; previously_stable }
                    ; time_to_next_stable = Some candidate_time_to_next_stable
                    })))
      in
      let%sub get_current_time = Bonsai.Clock.get_current_time in
      let%sub bounce =
        (* [bounce] is an effect which, when scheduled, will bounce the
           state-machine and set the time-until-stable to the current wallclock
           time plus the provided offset *)
        let%arr get_current_time = get_current_time
        and inject = inject
        and input = input in
        let%bind.Effect now = get_current_time in
        inject (Bounce (input, now))
      in
      let%sub () =
        (* the input value changing triggers a bounce *)
        let%sub callback =
          let%arr bounce = bounce in
          fun _ -> bounce
        in
        Bonsai.Edge.on_change (module M) input ~callback
      in
      let%sub () =
        let%sub on_deactivate =
          let%arr inject = inject in
          inject Deactivate
        in
        (* activating the component bounces it to reset the timer *)
        Bonsai.Edge.lifecycle ~on_deactivate ~on_activate:bounce ()
      in
      let%sub () =
        match%sub time_to_next_stable with
        | None -> Bonsai.const ()
        | Some next_stable ->
          let%sub callback =
            let%arr inject = inject
            and input = input
            and get_current_time = get_current_time
            and bounce = bounce in
            fun (prev : Bonsai.Clock.Before_or_after.t option)
              (cur : Bonsai.Clock.Before_or_after.t) ->
              match prev, cur with
              | Some Before, After ->
                let%bind.Effect now = get_current_time in
                inject (Set_stable (input, now))
              | None, After ->
                print_s [%message "BUG" [%here] "clock moves straight to 'after'"];
                bounce
              | _ -> Effect.Ignore
          in
          let%sub before_or_after = Bonsai.Clock.at next_stable in
          Bonsai.Edge.on_change'
            (module Bonsai.Clock.Before_or_after)
            before_or_after
            ~callback
      in
      let%arr stability = stability
      and input = input in
      match stability with
      | Stable input' when M.equal input' input -> Bonsai_extra.Stability.Stable input
      | Stable previously_stable ->
        (* Even if the state-machine claims that the value is stable, we can still witness
           instability one frame before the lifecycle events run. *)
        Unstable { previously_stable = Some previously_stable; unstable_value = input }
      | Unstable { previously_stable; unstable_value = _ } ->
        Unstable { previously_stable; unstable_value = input }
      | Inactive { previously_stable } ->
        Unstable { previously_stable; unstable_value = input }
    ;;

    module Common (M : sig
        val value_stability
          :  (module Bonsai.Model with type t = 'a)
          -> 'a Value.t
          -> time_to_stable:Time_ns.Span.t
          -> 'a Bonsai_extra.Stability.t Computation.t

        val show_handle : ('a, 'b) Handle.t -> unit
      end) =
    struct
      let show = M.show_handle

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let v = Bonsai.Var.value v' in
        let c =
          M.value_stability (module Int) v ~time_to_stable:(Time_ns.Span.of_sec 1.0)
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = int Bonsai_extra.Stability.t [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| (Unstable (previously_stable ()) (unstable_value 1)) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| (Stable 1) |}];
        Bonsai.Var.set v' 2;
        show handle;
        [%expect {| (Unstable (previously_stable (1)) (unstable_value 2)) |}];
        advance_by_sec handle 0.5;
        show handle;
        [%expect {| (Unstable (previously_stable (1)) (unstable_value 2)) |}];
        Bonsai.Var.set v' 3;
        show handle;
        [%expect {| (Unstable (previously_stable (1)) (unstable_value 3)) |}];
        advance_by_sec handle 0.5;
        show handle;
        [%expect {| (Unstable (previously_stable (1)) (unstable_value 3)) |}];
        advance_by_sec handle 0.5;
        show handle;
        [%expect {| (Stable 3) |}];
        Bonsai.Var.set v' 4;
        show handle;
        [%expect {| (Unstable (previously_stable (3)) (unstable_value 4)) |}];
        advance_by_sec handle 1.0;
        Bonsai.Var.set v' 5;
        show handle;
        [%expect {| (Unstable (previously_stable (3)) (unstable_value 5)) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| (Stable 5) |}];
        advance_by_sec handle 0.5;
        Bonsai.Var.set v' 4;
        show handle;
        [%expect {| (Unstable (previously_stable (5)) (unstable_value 4)) |}];
        advance_by_sec handle 0.5;
        Bonsai.Var.set v' 5;
        show handle;
        [%expect {| (Unstable (previously_stable (5)) (unstable_value 5)) |}]
      ;;

      let%expect_test _ =
        let v' = Bonsai.Var.create 1 in
        let on' = Bonsai.Var.create true in
        let v = Bonsai.Var.value v' in
        let on = Bonsai.Var.value on' in
        let c =
          match%sub on with
          | true ->
            let%sub x =
              M.value_stability (module Int) v ~time_to_stable:(Time_ns.Span.of_sec 1.0)
            in
            let%arr x = x in
            Some x
          | false -> Bonsai.const None
        in
        let handle =
          Handle.create
            (Result_spec.sexp
               (module struct
                 type t = int Bonsai_extra.Stability.t option [@@deriving sexp]
               end))
            c
        in
        show handle;
        [%expect {| ((Unstable (previously_stable ()) (unstable_value 1))) |}];
        advance_by_sec handle 1.0;
        show handle;
        [%expect {| ((Stable 1)) |}];
        Bonsai.Var.set on' false;
        show handle;
        [%expect {| () |}];
        Bonsai.Var.set on' true;
        show handle;
        [%expect {| ((Unstable (previously_stable (1)) (unstable_value 1))) |}]
      ;;
    end

    module _ = Common (struct
        (* The function reference below is an implementation that exists purely
           as a sanity check for the real implementation. If two vastly
           different implemenations always yield the same result, that's an
           encouraging sign. Sadly, this implementation relies on having a
           frame between certain real-world events, so we only run the tests
           with [recompute_view_until_stable] being called before Handle.show.
           (This downside is one reason why this is not the real
           implementation.) *)

        let value_stability = alternate_value_stability_implementation

        let show_handle handle =
          Handle.recompute_view_until_stable handle;
          Handle.show handle
        ;;
      end)

    module _ = Common (struct
        let value_stability = Bonsai_extra.value_stability
        let show_handle = Handle.show
      end)

    module _ = Common (struct
        let value_stability = Bonsai_extra.value_stability

        let show_handle handle =
          Handle.recompute_view handle;
          Handle.show handle
        ;;
      end)

    module _ = Common (struct
        let value_stability = Bonsai_extra.value_stability

        let show_handle handle =
          Handle.recompute_view_until_stable handle;
          Handle.show handle
        ;;
      end)
  end)
;;
OCaml

Innovation. Community. Security.