Source file test_proc_bonsai.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
open! Core
open! Import
module Bonsai_lib = Bonsai
open Bonsai_lib
open Proc
open Bonsai.Let_syntax
module Query_response_tracker = Bonsai.Effect.For_testing.Query_response_tracker
let%expect_test "cutoff" =
let var = Bonsai.Var.create 0 in
let value = Bonsai.Var.value var in
let component = return @@ Value.cutoff value ~equal:(fun a b -> a % 2 = b % 2) in
let handle = Handle.create (Result_spec.string (module Int)) component in
Handle.show handle;
[%expect {| 0 |}];
Bonsai.Var.set var 2;
Handle.show handle;
[%expect {| 0 |}];
Bonsai.Var.set var 1;
Handle.show handle;
[%expect {| 1 |}]
;;
let%expect_test "arrow-syntax" =
let component =
let%sub a = Bonsai.const "hi" in
let%sub b = Bonsai.const 5 in
let%arr a = a
and b = b in
sprintf "%s %d" a b
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| hi 5 |}]
;;
let%expect_test "mapn" =
let (_ : unit Computation.t) =
let%mapn.Computation () = Bonsai.const ()
and () = Bonsai.const ()
and () = Bonsai.const () in
()
in
()
;;
let%expect_test "if%sub" =
let component input =
let a = Value.return "hello" in
let b = Value.return "world" in
if%sub input then Bonsai.read a else Bonsai.read b
in
let var = Bonsai.Var.create true in
let handle =
Handle.create (Result_spec.string (module String)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {| hello |}];
Bonsai.Var.set var false;
Handle.show handle;
[%expect {| world |}]
;;
let%expect_test "call component" =
let add_one = Bonsai.pure (fun x -> x + 1) in
let component input =
let%sub a = add_one input in
return a
in
let var = Bonsai.Var.create 1 in
let handle =
Handle.create (Result_spec.sexp (module Int)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {| 2 |}];
Bonsai.Var.set var 2;
Handle.show handle;
[%expect {| 3 |}]
;;
let%expect_test "on_display" =
let component =
let%sub state, set_state = Bonsai.state [%here] (module Int) ~default_model:0 in
let update =
let%map state = state
and set_state = set_state in
set_state (state + 1)
in
let%sub () = Bonsai.Edge.after_display update in
return state
in
let handle = Handle.create (Result_spec.sexp (module Int)) component in
Handle.show handle;
[%expect {| 0 |}];
Handle.show handle;
[%expect {| 1 |}];
Handle.show handle;
[%expect {| 2 |}];
Handle.show handle;
[%expect {| 3 |}]
;;
let%expect_test "on_display for updating a state" =
let component input =
let%sub state, set_state = Bonsai.state_opt [%here] (module Int) in
let%sub update =
match%sub state with
| None ->
return
@@ let%map set_state = set_state
and input = input in
Some (set_state (Some input))
| Some state ->
return
@@ let%map state = state
and set_state = set_state
and input = input in
if Int.equal state input then None else Some (set_state (Some input))
in
let%sub () = Bonsai.Edge.after_display' update in
return (Value.both input state)
in
let var = Bonsai.Var.create 1 in
let handle =
Handle.create
(Result_spec.sexp
(module struct
type t = int * int option [@@deriving sexp_of]
end))
(component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {| (1 ()) |}];
Handle.show handle;
[%expect {| (1 (1)) |}];
Handle.show handle;
[%expect {| (1 (1)) |}];
Bonsai.Var.set var 2;
Handle.show handle;
[%expect {| (2 (1)) |}];
Handle.show handle;
[%expect {| (2 (2)) |}];
Handle.show handle;
[%expect {| (2 (2)) |}]
;;
let%expect_test "path" =
let component =
let%sub () = Bonsai.const () in
let%sub path = Bonsai.Private.path in
return (Value.map path ~f:Bonsai.Private.Path.sexp_of_t)
in
let handle = Handle.create (Result_spec.sexp (module Sexp)) component in
Handle.show handle;
[%expect {| (Subst_from Subst_into Subst_into Subst_from) |}]
;;
let%expect_test "assoc and enum path" =
let component =
Bonsai.assoc
(module Int)
(Value.return (Int.Map.of_alist_exn [ -1, (); 1, () ]))
~f:(fun i _ ->
if%sub i >>| ( > ) 0 then Bonsai.Private.path else Bonsai.Private.path)
in
let handle =
Handle.create
(Result_spec.sexp
(module struct
type t = Bonsai.Private.Path.t Int.Map.t [@@deriving sexp_of]
end))
component
in
Handle.show handle;
[%expect
{|
((-1 (Subst_from (Assoc -1) Subst_into (Switch 0)))
(1 (Subst_from (Assoc 1) Subst_into (Switch 1)))) |}]
;;
let%expect_test "simple-assoc works with paths" =
let component =
Bonsai.assoc
(module String)
(Value.return (String.Map.of_alist_exn [ "hello", (); "world", () ]))
~f:(fun _ _ ->
let%sub a = Bonsai.Private.path in
let%sub b = Bonsai.Private.path in
return (Bonsai.Value.both a b))
in
let handle =
Handle.create
(Result_spec.sexp
(module struct
type t = (Bonsai.Private.Path.t * Bonsai.Private.Path.t) String.Map.t
[@@deriving sexp_of]
end))
component
in
Handle.show handle;
[%expect
{|
((hello
((Subst_from (Assoc hello) Subst_from)
(Subst_from (Assoc hello) Subst_into Subst_from)))
(world
((Subst_from (Assoc world) Subst_from)
(Subst_from (Assoc world) Subst_into Subst_from)))) |}];
print_s Bonsai_lib.Private.(Computation.sexp_of_packed (reveal_computation component));
[%expect {| (Assoc_simpl ((map constant))) |}]
;;
let%expect_test "chain" =
let add_one = Bonsai.pure (fun x -> x + 1) in
let double = Bonsai.pure (fun x -> x * 2) in
let component input =
let%sub a = add_one input in
let%sub b = double a in
return b
in
let var = Bonsai.Var.create 1 in
let handle =
Handle.create (Result_spec.sexp (module Int)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {| 4 |}];
Bonsai.Var.set var 2;
Handle.show handle;
[%expect {| 6 |}]
;;
let%expect_test "chain + both" =
let add_one = Bonsai.pure (fun x -> x + 1) in
let double = Bonsai.pure (fun x -> x * 2) in
let add = Bonsai.pure (fun (x, y) -> x + y) in
let component input =
let%sub a = add_one input in
let%sub b = double a in
let%sub c = add (Value.both a b) in
return c
in
let var = Bonsai.Var.create 1 in
let handle =
Handle.create (Result_spec.sexp (module Int)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {| 6 |}];
Bonsai.Var.set var 2;
Handle.show handle;
[%expect {| 9 |}]
;;
let%expect_test "wrap" =
let component =
Bonsai.wrap
(module Int)
~default_model:0
~apply_action:(fun ~inject:_ ~schedule_event:_ (result, _) model () ->
String.length result + model)
~f:(fun model inject ->
return
@@ let%map model = model
and inject = inject in
Int.to_string model, inject)
in
let handle =
Handle.create
(module struct
type t = string * (unit -> unit Effect.t)
type incoming = unit
let view = Tuple2.get1
let incoming (_, x) () = x ()
end)
component
in
Handle.show handle;
[%expect {| 0 |}];
Handle.do_actions handle [ () ];
Handle.show handle;
[%expect {| 1 |}];
Handle.do_actions handle [ (); (); (); (); (); (); (); (); (); () ];
Handle.show handle;
[%expect {| 12 |}];
Handle.do_actions handle [ () ];
Handle.show handle;
[%expect {| 14 |}]
;;
let%expect_test "match%sub" =
let var : (string, int) Either.t Bonsai.Var.t =
Bonsai.Var.create (Either.First "hello")
in
let component =
match%sub Bonsai.Var.value var with
| First s -> Bonsai.read (Value.map s ~f:(sprintf "%s world"))
| Second i -> Bonsai.read (Value.map i ~f:Int.to_string)
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| hello world |}];
Bonsai.Var.set var (Second 2);
Handle.show handle;
[%expect {| 2 |}]
;;
let%expect_test "match%sub" =
let var : (string, int) Either.t Bonsai.Var.t =
Bonsai.Var.create (Either.First "hello")
in
let component =
match%sub Bonsai.Var.value var with
| First s -> Bonsai.read (Value.map s ~f:(sprintf "%s world"))
| Second i -> Bonsai.read (Value.map i ~f:Int.to_string)
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| hello world |}];
Bonsai.Var.set var (Second 2);
Handle.show handle;
[%expect {| 2 |}]
;;
type thing =
| Loading of string
| Search_results of int
let%expect_test "match%sub repro" =
let open Bonsai.Let_syntax in
let component current_page =
match%sub current_page with
| Loading x ->
Bonsai.read
(let%map x = x in
"loading " ^ x)
| Search_results s ->
Bonsai.read
(let%map s = s in
sprintf "search results %d" s)
in
let var = Bonsai.Var.create (Loading "hello") in
let handle =
Handle.create (Result_spec.string (module String)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {| loading hello |}];
Bonsai.Var.set var (Search_results 5);
Handle.show handle;
[%expect {| search results 5 |}]
;;
let%expect_test "if%sub" =
let component input =
let a = Value.return "hello" in
let b = Value.return "world" in
if%sub input then Bonsai.read a else Bonsai.read b
in
let var = Bonsai.Var.create true in
let handle =
Handle.create (Result_spec.string (module String)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {| hello |}];
Bonsai.Var.set var false;
Handle.show handle;
[%expect {| world |}]
;;
let%expect_test "let%sub patterns" =
let component =
let%sub a, _b = Bonsai.const ("hello world", 5) in
return a
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| hello world |}]
;;
let%expect_test "let%sub unit rhs optimizaiton" =
let component =
let%sub a = Bonsai.const 5 in
let%sub b = Bonsai.const 6 in
return
(let%map a = a
and b = b in
a + b)
in
print_s Bonsai_lib.Private.(Computation.sexp_of_packed (reveal_computation component));
[%expect
{|
(Subst_stateless_from (
(from (Return constant))
(via lib/bonsai/src/proc.ml:29:46)
(into (
Subst_stateless_from (
(from (Return constant))
(via lib/bonsai/src/proc.ml:29:46)
(into (
Return (
map (
t (
both
(t1 (named lib/bonsai/src/proc.ml:29:46))
(t2 (named lib/bonsai/src/proc.ml:29:46)))))))
(here None))))
(here None))) |}]
;;
let%expect_test "assoc simplifies its inner computation, if possible" =
let value = Value.return String.Map.empty in
let component =
Bonsai.assoc
(module String)
value
~f:(fun key data -> Bonsai.read (Value.both key data))
in
print_s Bonsai_lib.Private.(Computation.sexp_of_packed (reveal_computation component));
[%expect {|
(Assoc_simpl ((map constant))) |}]
;;
let%expect_test "assoc with sub simplifies its inner computation, if possible" =
let value = Bonsai.Value.return String.Map.empty in
let component =
Bonsai.assoc
(module String)
value
~f:(fun key data ->
let%sub key = Bonsai.read key in
Bonsai.read (Bonsai.Value.both key data))
in
print_s Bonsai_lib.Private.(Computation.sexp_of_packed (reveal_computation component));
[%expect {|
(Assoc_simpl ((map constant))) |}]
;;
let%expect_test "assoc with sub simplifies its inner computation, if possible" =
let value = Bonsai.Value.return String.Map.empty in
let component =
Bonsai.assoc
(module String)
value
~f:(fun key data ->
let%sub key = Bonsai.read key in
Bonsai.read (Bonsai.Value.both key data))
in
print_s Bonsai_lib.Private.(Computation.sexp_of_packed (reveal_computation component));
[%expect {|
(Assoc_simpl ((map constant))) |}]
;;
let%expect_test "map > lazy" =
let open Bonsai.Let_syntax in
let module M = struct
type t =
{ label : string
; children : t Int.Map.t
}
end
in
let rec f ~t ~depth =
let%sub { M.label; children } = return t in
let%sub children =
Bonsai.assoc
(module Int)
children
~f:(fun _ v ->
let depth =
let%map depth = depth in
depth + 1
in
Bonsai.lazy_ (lazy (f ~t:v ~depth)))
in
return
@@ let%map label = label
and children = children
and depth = depth in
[%message label (depth : int) (children : Sexp.t Int.Map.t)]
in
let t_var = Bonsai.Var.create { M.label = "hi"; children = Int.Map.empty } in
let t_value = Bonsai.Var.value t_var in
let handle =
Handle.create
(Result_spec.sexp (module Sexp))
(f ~t:t_value ~depth:(Bonsai.Value.return 0))
in
[%expect {| |}];
Handle.show handle;
[%expect {| (hi (depth 0) (children ())) |}];
Bonsai.Var.set
t_var
{ M.label = "hi"
; children = Int.Map.singleton 0 { M.label = "hello"; children = Int.Map.empty }
};
Handle.show handle;
[%expect {| (hi (depth 0) (children ((0 (hello (depth 1) (children ())))))) |}]
;;
let%expect_test "dynamic action sent to non-existent assoc element" =
let var = Bonsai.Var.create (Int.Map.of_alist_exn [ 1, (); 2, () ]) in
let component =
Bonsai.assoc
(module Int)
(Bonsai.Var.value var)
~f:(fun _key _data ->
Bonsai.state_machine1
[%here]
(module Int)
(module Int)
~default_model:0
~apply_action:(fun ~inject:_ ~schedule_event:_ () _model new_model -> new_model)
(Value.return ()))
in
let handle =
Handle.create
(module struct
type t = (int * (int -> unit Effect.t)) Int.Map.t
type incoming = Nothing.t
let incoming _ = Nothing.unreachable_code
let view (map : t) =
map
|> Map.to_alist
|> List.map ~f:(fun (i, (s, _)) -> i, s)
|> [%sexp_of: (int * int) list]
|> Sexp.to_string_hum
;;
end)
component
in
Handle.show handle;
[%expect {| ((1 0) (2 0)) |}];
let result = Handle.result handle in
let set_two what =
let _, set = Map.find_exn result 2 in
Ui_effect.Expert.handle (set what)
in
set_two 3;
Handle.show handle;
[%expect {| ((1 0) (2 3)) |}];
Bonsai.Var.set var (Int.Map.of_alist_exn [ 1, () ]);
Handle.show handle;
[%expect {| ((1 0)) |}];
set_two 4;
Handle.show handle;
[%expect
{|
("an action inside of Bonsai.assoc as been dropped because the computation is no longer active"
(key 2) (action 4))
((1 0)) |}];
Bonsai.Var.set var (Int.Map.of_alist_exn [ 1, (); 2, () ]);
Handle.show handle;
[%expect {| ((1 0) (2 3)) |}]
;;
let%test_module "inactive delivery" =
(module struct
let rec censor_sexp = function
| Sexp.List l ->
(match List.filter_map l ~f:censor_sexp with
| [] -> None
| [ x ] -> Some x
| all -> Some (Sexp.List all))
| Sexp.Atom s ->
if String.is_prefix s ~prefix:"lib/bonsai" then None else Some (Atom s)
;;
let print_computation computation =
computation
|> Bonsai_lib.Private.reveal_computation
|> Bonsai_lib.Private.Computation.sexp_of_packed
|> censor_sexp
|> Option.value ~default:(Sexp.List [])
|> print_s
;;
let test_delivery_to_inactive_component computation =
print_computation computation;
let var = Bonsai.Var.create (Int.Map.of_alist_exn [ 1, (); 2, () ]) in
let component =
Bonsai.assoc
(module Int)
(Bonsai.Var.value var)
~f:(fun _key _data -> computation)
in
let handle =
Handle.create
(module struct
type t = (int * (int -> unit Effect.t)) Int.Map.t
type incoming = Nothing.t
let incoming _ = Nothing.unreachable_code
let view (map : t) =
map
|> Map.to_alist
|> List.map ~f:(fun (i, (s, _)) -> i, s)
|> [%sexp_of: (int * int) list]
|> Sexp.to_string_hum
;;
end)
component
in
Handle.show handle;
let result = Handle.result handle in
let set_two what =
let _, set = Map.find_exn result 2 in
Ui_effect.Expert.handle (set what)
in
set_two 3;
Handle.show handle;
Bonsai.Var.set var (Int.Map.of_alist_exn [ 1, () ]);
Handle.show handle;
set_two 4;
Handle.show handle;
Bonsai.Var.set var (Int.Map.of_alist_exn [ 1, (); 2, () ]);
Handle.show handle
;;
let%expect_test "dynamic action inactive-delivery" =
test_delivery_to_inactive_component
(Bonsai.state_machine1
[%here]
(module Int)
(module Int)
~default_model:0
~apply_action:(fun ~inject:_ ~schedule_event:_ () _model new_model ->
new_model)
(Value.return ()));
[%expect
{|
Leaf
((1 0) (2 0))
((1 0) (2 3))
((1 0))
("an action inside of Bonsai.assoc as been dropped because the computation is no longer active"
(key 2) (action 4))
((1 0))
((1 0) (2 3)) |}]
;;
let%expect_test "static action inactive-delivery" =
test_delivery_to_inactive_component
(Bonsai.state [%here] (module Int) ~default_model:0);
[%expect
{|
Leaf0
((1 0) (2 0))
((1 0) (2 3))
((1 0))
((1 0))
((1 0) (2 4)) |}]
;;
let%expect_test "static inside of a lazy" =
test_delivery_to_inactive_component
(Bonsai.lazy_ (lazy (Bonsai.state [%here] (module Int) ~default_model:0)));
[%expect
{|
Lazy
((1 0) (2 0))
((1 0) (2 3))
((1 0))
((1 0))
((1 0) (2 4)) |}]
;;
let%expect_test "static inside of a wrap" =
test_delivery_to_inactive_component
(Bonsai.wrap
(module Unit)
~default_model:()
~apply_action:(fun ~inject:_ ~schedule_event:_ _ () () -> ())
~f:(fun _model _inject -> Bonsai.state [%here] (module Int) ~default_model:0));
[%expect
{|
(Wrap Leaf0)
((1 0) (2 0))
((1 0) (2 3))
((1 0))
((1 0))
((1 0) (2 4)) |}]
;;
let%expect_test "static inside of a match%sub" =
test_delivery_to_inactive_component
(match%sub Value.return () with
| () -> Bonsai.state [%here] (module Int) ~default_model:0);
[%expect
{|
(Subst_stateless_from (
(from (Return constant))
via
(into (
Subst_stateless_from (
(from (Return (map (t named))))
via
(into Leaf0)
(here None))))
(here None)))
((1 0) (2 0))
((1 0) (2 3))
((1 0))
((1 0))
((1 0) (2 4)) |}]
;;
let%expect_test "static inside of a with_model_resetter" =
test_delivery_to_inactive_component
(let%sub r, _reset =
Bonsai.with_model_resetter (Bonsai.state [%here] (module Int) ~default_model:0)
in
return r);
[%expect
{|
(Subst_stateless_into (
(from (With_model_resetter Leaf0))
via
(into (
Subst_stateless_from (
(from (Return (map (t named))))
via
(into (
Subst_stateless_from (
(from (Return (map (t named))))
via
(into (Return named))
(here None))))
(here None))))
(here None)))
((1 0) (2 0))
((1 0) (2 3))
((1 0))
((1 0))
((1 0) (2 4)) |}]
;;
let%expect_test "resetting while inactive" =
let which_branch = Bonsai.Var.create true in
let component =
if%sub Bonsai.Var.value which_branch
then
Bonsai.with_model_resetter (Bonsai.state [%here] (module Int) ~default_model:0)
else Bonsai.const ((-1, fun _ -> Effect.Ignore), Effect.Ignore)
in
let handle =
Handle.create
(module struct
type t = (int * (int -> unit Effect.t)) * unit Effect.t
type incoming = Nothing.t
let incoming _ = Nothing.unreachable_code
let view ((i, _), _) = Int.to_string i
end)
component
in
Handle.show handle;
let (_, set_value), reset = Handle.result handle in
let set_value i = Ui_effect.Expert.handle (set_value i) in
let reset () = Ui_effect.Expert.handle reset in
set_value 3;
Handle.show handle;
Bonsai.Var.set which_branch false;
Handle.show handle;
set_value 4;
Handle.show handle;
Bonsai.Var.set which_branch true;
Handle.show handle;
[%expect {|
0
3
-1
-1
4 |}];
Bonsai.Var.set which_branch false;
Handle.show handle;
[%expect {| -1 |}];
reset ();
Bonsai.Var.set which_branch true;
Handle.show handle;
[%expect {| 0 |}]
;;
end)
;;
let%test_module "testing Bonsai internals" =
(module struct
[@@@alert "-rampantly_nondeterministic"]
let%expect_test "remove unused models in assoc" =
let var = Bonsai.Var.create Int.Map.empty in
let module State_with_setter = struct
type t =
{ state : string
; set_state : string -> unit Effect.t
}
end
in
let module Action = struct
type t = Set of string
end
in
let component =
Bonsai.assoc
(module Int)
(Bonsai.Var.value var)
~f:(fun _key _data ->
let%sub v = Bonsai.state [%here] (module String) ~default_model:"hello" in
return
@@ let%map state, set_state = v in
{ State_with_setter.state; set_state })
in
let handle =
Handle.create
(module struct
type t = State_with_setter.t Int.Map.t
type incoming = int * Action.t
let incoming (map : t) (id, action) =
let t = Map.find_exn map id in
match (action : Action.t) with
| Set value -> t.set_state value
;;
let view (map : t) =
map
|> Map.to_alist
|> List.map ~f:(fun (i, { state; set_state = _ }) -> i, state)
|> [%sexp_of: (int * string) list]
|> Sexp.to_string_hum
;;
end)
component
in
Handle.show_model handle;
[%expect {|
() |}];
Bonsai.Var.set var (Int.Map.of_alist_exn [ 1, (); 2, () ]);
Handle.show_model handle;
[%expect {|
() |}];
Handle.do_actions handle [ 1, Set "test" ];
Handle.show_model handle;
[%expect {| ((1 test)) |}];
Handle.do_actions handle [ 1, Set "hello" ];
Handle.show_model handle;
[%expect {|
() |}]
;;
end)
;;
let%expect_test "multiple maps respect cutoff" =
let component input =
input
|> Value.map ~f:(fun (_ : int) -> ())
|> Value.map ~f:(fun () -> print_endline "triggered")
|> return
in
let var = Bonsai.Var.create 1 in
let handle =
Handle.create (Result_spec.sexp (module Unit)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {|
triggered
() |}];
Bonsai.Var.set var 2;
Handle.show handle;
[%expect {| () |}]
;;
let%expect_test "let syntax is collapsed upon eval" =
let computation =
let%arr () = Value.return ()
and () = Value.return ()
and () = Value.return ()
and () = Value.return ()
and () = Value.return ()
and () = Value.return ()
and () = Value.return () in
()
in
let packed =
let open Bonsai.Private in
let (T { t = computation; model; apply_static = _; static_action; dynamic_action }) =
reveal_computation computation
in
let T = Type_equal.Id.same_witness_exn Meta.Action.nothing static_action in
let T = Type_equal.Id.same_witness_exn Meta.Action.nothing dynamic_action in
let snapshot =
eval
~environment:Environment.empty
~path:Path.empty
~clock:Ui_incr.clock
~inject_dynamic:Nothing.unreachable_code
~inject_static:Nothing.unreachable_code
~model:(Ui_incr.return model.default)
computation
in
Snapshot.result snapshot |> Ui_incr.pack
in
let filename = Stdlib.Filename.temp_file "incr" "out" in
Ui_incr.Packed.save_dot_to_file filename [ packed ];
let dot_contents = In_channel.read_all filename in
require
[%here]
~if_false_then_print_s:(lazy [%message "No Map7 node found"])
(String.is_substring dot_contents ~substring:"Map7");
[%expect {| |}]
;;
let%test_unit "constant prop doesn't happen" =
let (_ : int Computation.t) =
match%sub Value.return (First 1) with
| First x -> Bonsai.read x
| Second x -> Bonsai.read x
in
()
;;
let%expect_test "ignored result of assoc" =
let var = Bonsai.Var.create (Int.Map.of_alist_exn [ 1, (); 2, () ]) in
let component =
let%sub _ =
Bonsai.assoc
(module Int)
(Bonsai.Var.value var)
~f:(fun _key data ->
let%sub _ = Bonsai.const () in
Bonsai.read data)
in
Bonsai.const ()
in
let handle = Handle.create (Result_spec.sexp (module Unit)) component in
Handle.show handle;
[%expect {|
() |}];
Bonsai.Var.set var (Int.Map.of_alist_exn []);
Expect_test_helpers_core.require_does_not_raise [%here] (fun () -> Handle.show handle);
[%expect {| () |}]
;;
let%expect_test "on_display for updating a state (using on_change)" =
let callback =
Value.return (fun prev cur ->
Ui_effect.print_s [%message "change!" (prev : int option) (cur : int)])
in
let component input = Bonsai.Edge.on_change' [%here] (module Int) ~callback input in
let var = Bonsai.Var.create 1 in
let handle =
Handle.create
(Result_spec.sexp
(module struct
type t = unit
let sexp_of_t () = Sexp.Atom "rendering..."
end))
(component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {|
rendering...
(change! (prev ()) (cur 1)) |}];
Handle.show handle;
[%expect {| rendering... |}];
Handle.show handle;
[%expect {| rendering... |}];
Bonsai.Var.set var 2;
Handle.show handle;
[%expect {|
rendering...
(change! (prev (1)) (cur 2)) |}];
Handle.show handle;
[%expect {| rendering... |}];
Handle.show handle;
[%expect {| rendering... |}]
;;
let%expect_test "actor" =
let print_int_effect = printf "%d\n" |> Bonsai.Effect.of_sync_fun in
let component =
let%sub _, effect =
Bonsai.actor0
[%here]
(module Int)
(module Unit)
~default_model:0
~recv:(fun ~schedule_event:_ v () -> v + 1, v)
in
return
@@ let%map effect = effect in
let%bind.Bonsai.Effect i = effect () in
print_int_effect i
in
let handle =
Handle.create
(module struct
type t = unit Effect.t
type incoming = unit
let view _ = ""
let incoming t () = t
end)
component
in
Handle.do_actions handle [ () ];
Handle.show handle;
[%expect {| 0 |}];
Handle.do_actions handle [ (); (); () ];
Handle.show handle;
[%expect {|
1
2
3 |}]
;;
let%expect_test "lifecycle" =
let effect action on =
Ui_effect.print_s [%message (action : string) (on : string)] |> Value.return
in
let component input =
let rendered = Bonsai.const "" in
if%sub input
then (
let%sub () =
Bonsai.Edge.lifecycle
~on_activate:(effect "activate" "a")
~on_deactivate:(effect "deactivate" "a")
~after_display:(effect "after-display" "a")
()
in
rendered)
else (
let%sub () =
Bonsai.Edge.lifecycle
~on_activate:(effect "activate" "b")
~on_deactivate:(effect "deactivate" "b")
~after_display:(effect "after-display" "b")
()
in
rendered)
in
let var = Bonsai.Var.create true in
let handle =
Handle.create (Result_spec.string (module String)) (component (Bonsai.Var.value var))
in
Handle.show handle;
[%expect {|
((action activate) (on a))
((action after-display) (on a)) |}];
Bonsai.Var.set var false;
Handle.show handle;
[%expect
{|
((action deactivate) (on a))
((action activate) (on b))
((action after-display) (on b)) |}];
Bonsai.Var.set var true;
Handle.show handle;
[%expect
{|
((action deactivate) (on b))
((action activate) (on a))
((action after-display) (on a)) |}]
;;
let%expect_test "Clock.every" =
let print_hi = (fun () -> print_endline "hi") |> Bonsai.Effect.of_sync_fun in
let component =
let%sub () =
Bonsai.Clock.every [%here] (Time_ns.Span.of_sec 3.0) (Value.return (print_hi ()))
in
Bonsai.const ()
in
let handle = Handle.create (Result_spec.sexp (module Unit)) component in
let move_forward_and_show () =
Handle.advance_clock_by handle (Time_ns.Span.of_sec 1.0);
Handle.show handle
in
Handle.show handle;
[%expect {|
()
hi |}];
move_forward_and_show ();
[%expect {| () |}];
move_forward_and_show ();
[%expect {| () |}];
move_forward_and_show ();
[%expect {|
()
hi |}]
;;
let edge_poll_shared ~get_expect_output =
let effect_tracker = Query_response_tracker.create () in
let effect = Bonsai.Effect.For_testing.of_query_response_tracker effect_tracker in
let var = Bonsai.Var.create "hello" in
let component =
Bonsai.Edge.Poll.effect_on_change
[%here]
(module String)
(module String)
Bonsai.Edge.Poll.Starting.empty
(Bonsai.Var.value var)
~effect:(Value.return effect)
in
let handle =
Handle.create
(Result_spec.sexp
(module struct
type t = string option [@@deriving sexp]
end))
component
in
let trigger_display () =
Handle.show handle;
let pending = Query_response_tracker.queries_pending_response effect_tracker in
let output = Sexp.of_string (get_expect_output ()) in
print_s [%message (pending : string list) (output : Sexp.t)]
in
var, effect_tracker, trigger_display
;;
let%expect_test "Edge.poll in order" =
let get_expect_output () = [%expect.output] in
let var, effect_tracker, trigger_display = edge_poll_shared ~get_expect_output in
trigger_display ();
[%expect {|
((pending ())
(output ())) |}];
trigger_display ();
[%expect {|
((pending (hello)) (output ())) |}];
Bonsai.Var.set var "world";
trigger_display ();
[%expect {|
((pending (hello)) (output ())) |}];
trigger_display ();
[%expect {|
((pending (world hello)) (output ())) |}];
Query_response_tracker.maybe_respond effect_tracker ~f:(fun s ->
Respond (String.uppercase s));
trigger_display ();
[%expect {| ((pending ()) (output (WORLD))) |}]
;;
let%expect_test "Edge.poll out of order" =
let get_expect_output () = [%expect.output] in
let var, effect_tracker, trigger_display = edge_poll_shared ~get_expect_output in
trigger_display ();
[%expect {|
((pending ())
(output ())) |}];
trigger_display ();
[%expect {|
((pending (hello)) (output ())) |}];
Bonsai.Var.set var "world";
trigger_display ();
[%expect {|
((pending (hello)) (output ())) |}];
trigger_display ();
[%expect {|
((pending (world hello)) (output ())) |}];
Query_response_tracker.maybe_respond effect_tracker ~f:(function
| "world" as s -> Respond (String.uppercase s)
| _ -> No_response_yet);
trigger_display ();
[%expect {|
((pending (hello))
(output (WORLD))) |}];
Query_response_tracker.maybe_respond effect_tracker ~f:(function
| "hello" as s -> Respond (String.uppercase s)
| _ -> No_response_yet);
trigger_display ();
[%expect {|
((pending ()) (output (WORLD))) |}]
;;
let%expect_test "Clock.now" =
let clock = Ui_incr.Clock.create ~start:Time_ns.epoch () in
let component = Bonsai.Clock.now in
let handle =
Handle.create ~clock (Result_spec.sexp (module Time_ns.Alternate_sexp)) component
in
Handle.show handle;
[%expect {| "1970-01-01 00:00:00Z" |}];
Ui_incr.Clock.advance_clock_by clock (Time_ns.Span.of_sec 0.5);
Handle.show handle;
[%expect {| "1970-01-01 00:00:00.5Z" |}];
Ui_incr.Clock.advance_clock_by clock (Time_ns.Span.of_sec 0.7);
Handle.show handle;
[%expect {| "1970-01-01 00:00:01.2Z" |}]
;;
let%expect_test "Clock.now" =
let clock = Ui_incr.Clock.create ~start:Time_ns.epoch () in
let component =
let%sub get_time = Bonsai.Clock.get_current_time in
Bonsai.Edge.after_display
(let%map get_time = get_time in
let%bind.Effect now = get_time in
Effect.print_s [%sexp (now : Time_ns.Alternate_sexp.t)])
in
let handle = Handle.create ~clock (Result_spec.sexp (module Unit)) component in
Handle.recompute_view handle;
[%expect {| "1970-01-01 00:00:00Z" |}];
Ui_incr.Clock.advance_clock_by clock (Time_ns.Span.of_sec 0.5);
Handle.recompute_view handle;
[%expect {| "1970-01-01 00:00:00.5Z" |}];
Ui_incr.Clock.advance_clock_by clock (Time_ns.Span.of_sec 0.7);
Handle.recompute_view handle;
[%expect {| "1970-01-01 00:00:01.2Z" |}]
;;
let%expect_test "Clock.approx_now" =
let clock = Ui_incr.Clock.create ~start:Time_ns.epoch () in
let component = Bonsai.Clock.approx_now ~tick_every:(Time_ns.Span.of_sec 1.0) in
let handle =
Handle.create ~clock (Result_spec.sexp (module Time_ns.Alternate_sexp)) component
in
Handle.show handle;
[%expect {| "1970-01-01 00:00:00Z" |}];
Ui_incr.Clock.advance_clock_by clock (Time_ns.Span.of_sec 0.5);
Handle.show handle;
[%expect {| "1970-01-01 00:00:00Z" |}];
Ui_incr.Clock.advance_clock_by clock (Time_ns.Span.of_sec 0.7);
Handle.show handle;
[%expect {| "1970-01-01 00:00:01.2Z" |}]
;;
let chain_computation =
let%sub a = Bonsai.const "x" in
let%sub b, set_b = Bonsai.state [%here] (module String) ~default_model:" " in
let%sub c, set_c = Bonsai.state [%here] (module String) ~default_model:" " in
let%sub d, set_d = Bonsai.state [%here] (module String) ~default_model:" " in
let%sub () = Bonsai.Edge.on_change [%here] (module String) a ~callback:set_b in
let%sub () = Bonsai.Edge.on_change [%here] (module String) b ~callback:set_c in
let%sub () = Bonsai.Edge.on_change [%here] (module String) c ~callback:set_d in
return (Value.map4 a b c d ~f:(sprintf "a:%s b:%s c:%s d:%s"))
;;
let%expect_test "chained on_change" =
let handle = Handle.create (Result_spec.string (module String)) chain_computation in
Handle.show handle;
[%expect {| a:x b: c: d: |}];
Handle.show handle;
[%expect {| a:x b:x c: d: |}];
Handle.show handle;
[%expect {| a:x b:x c:x d: |}];
Handle.show handle;
[%expect {| a:x b:x c:x d:x |}];
Handle.show handle;
[%expect {| a:x b:x c:x d:x |}]
;;
let%expect_test "chained on_change with recompute_view_until_stable" =
let handle = Handle.create (Result_spec.string (module String)) chain_computation in
Handle.recompute_view_until_stable handle;
Handle.show handle;
[%expect {| a:x b:x c:x d:x |}]
;;
let%expect_test "infinite chain!" =
let computation =
let%sub state, set_state = Bonsai.state [%here] (module Int) ~default_model:0 in
let callback =
let%map set_state = set_state in
fun new_state -> set_state (new_state + 1)
in
let%sub () = Bonsai.Edge.on_change [%here] (module Int) state ~callback in
Bonsai.const ()
in
let handle = Handle.create (Result_spec.string (module Unit)) computation in
Expect_test_helpers_base.require_does_raise [%here] (fun () ->
Handle.recompute_view_until_stable handle);
[%expect {| (Failure "view not stable after 100 recomputations") |}]
;;
let%expect_test "computation.all_map" =
let component =
let%map.Computation map =
[ 1, Bonsai.const "a"; 2, Bonsai.const "b" ]
|> Int.Map.of_alist_exn
|> Computation.all_map
in
[%sexp_of: string Int.Map.t] map
in
let handle = Handle.create (Result_spec.string (module Sexp)) component in
Handle.show handle;
[%expect {| ((1 a)(2 b)) |}]
;;
let%expect_test "dynamic lookup" =
let id = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no" () in
let component =
Bonsai.Dynamic_scope.set
id
(Value.return "hello")
~inside:(Bonsai.Dynamic_scope.lookup id)
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| hello |}]
;;
let%expect_test "dynamic lookup fails" =
let id = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no" () in
let component = Bonsai.Dynamic_scope.lookup id in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| no |}]
;;
let%expect_test "eval inside one, use inside another" =
let id = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no" () in
let component =
let%sub a =
Bonsai.Dynamic_scope.set
id
(Value.return "hello")
~inside:(Bonsai.Dynamic_scope.lookup id)
in
let%sub b =
Bonsai.Dynamic_scope.set id (Value.return "world") ~inside:(Bonsai.read a)
in
return b
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| hello |}]
;;
let%expect_test "sub outside, use inside" =
let id = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no" () in
let component =
let%sub find = Bonsai.Dynamic_scope.lookup id in
Bonsai.Dynamic_scope.set id (Value.return "hello") ~inside:(return find)
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| no |}]
;;
let%expect_test "use resetter" =
let id = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no" () in
let component =
Bonsai.Dynamic_scope.set' id (Value.return "hello") ~f:(fun { revert } ->
revert (Bonsai.Dynamic_scope.lookup id))
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| no |}]
;;
let%expect_test "nested resetter" =
let id = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no" () in
let component =
Bonsai.Dynamic_scope.set
id
(Value.return "hello")
~inside:
(Bonsai.Dynamic_scope.set' id (Value.return "world") ~f:(fun { revert } ->
revert (Bonsai.Dynamic_scope.lookup id)))
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| hello |}]
;;
let%expect_test "resetter only impacts the id you target" =
let id_a = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no-a" () in
let id_b = Bonsai.Dynamic_scope.create ~name:"my-id" ~fallback:"no-b" () in
let component =
Bonsai.Dynamic_scope.set' id_a (Value.return "hello") ~f:(fun { revert } ->
Bonsai.Dynamic_scope.set
id_b
(Value.return "world")
~inside:
(revert
@@ let%sub a = Bonsai.Dynamic_scope.lookup id_a in
let%sub b = Bonsai.Dynamic_scope.lookup id_b in
return (Value.map2 a b ~f:(fun a b -> a ^ " " ^ b))))
in
let handle = Handle.create (Result_spec.string (module String)) component in
Handle.show handle;
[%expect {| no-a world |}]
;;
module M = struct
type t =
{ a : string
; b : int
}
[@@deriving sexp_of, fields]
end
let%expect_test "derived value" =
let id =
Bonsai.Dynamic_scope.create
~sexp_of:M.sexp_of_t
~name:"my-id"
~fallback:{ a = "hi"; b = 5 }
()
in
let a = Bonsai.Dynamic_scope.derived id ~get:M.a ~set:(Field.fset M.Fields.a) in
let component =
Bonsai.Dynamic_scope.set
a
(Value.return "hello")
~inside:(Bonsai.Dynamic_scope.lookup id)
in
let handle = Handle.create (Result_spec.sexp (module M)) component in
Handle.show handle;
[%expect {| ((a hello) (b 5)) |}]
;;
let%expect_test "derived value revert" =
let id =
Bonsai.Dynamic_scope.create
~sexp_of:M.sexp_of_t
~name:"my-id"
~fallback:{ a = "hi"; b = 5 }
()
in
let a = Bonsai.Dynamic_scope.derived id ~get:M.a ~set:(Field.fset M.Fields.a) in
let component =
Bonsai.Dynamic_scope.set' a (Value.return "hello") ~f:(fun { revert } ->
revert (Bonsai.Dynamic_scope.lookup id))
in
let handle = Handle.create (Result_spec.sexp (module M)) component in
Handle.show handle;
[%expect {| ((a hi) (b 5)) |}]
;;
let%expect_test "derived value nested revert inner" =
let id =
Bonsai.Dynamic_scope.create
~sexp_of:M.sexp_of_t
~name:"my-id"
~fallback:{ a = "hi"; b = 5 }
()
in
let a = Bonsai.Dynamic_scope.derived id ~get:M.a ~set:(Field.fset M.Fields.a) in
let component =
Bonsai.Dynamic_scope.set
a
(Value.return "hello")
~inside:
(Bonsai.Dynamic_scope.set' a (Value.return "world") ~f:(fun { revert } ->
revert (Bonsai.Dynamic_scope.lookup id)))
in
let handle = Handle.create (Result_spec.sexp (module M)) component in
Handle.show handle;
[%expect {| ((a hello) (b 5)) |}]
;;
let%expect_test "derived value nested revert outer" =
let id =
Bonsai.Dynamic_scope.create
~sexp_of:M.sexp_of_t
~name:"my-id"
~fallback:{ a = "hi"; b = 5 }
()
in
let a = Bonsai.Dynamic_scope.derived id ~get:M.a ~set:(Field.fset M.Fields.a) in
let b = Bonsai.Dynamic_scope.derived id ~get:M.b ~set:(Field.fset M.Fields.b) in
let component =
Bonsai.Dynamic_scope.set' a (Value.return "hello") ~f:(fun { revert } ->
Bonsai.Dynamic_scope.set
b
(Value.return 1000)
~inside:(revert (Bonsai.Dynamic_scope.lookup id)))
in
let handle = Handle.create (Result_spec.sexp (module M)) component in
Handle.show handle;
[%expect {| ((a hi) (b 1000)) |}]
;;
let%expect_test "exactly once" =
let component =
Bonsai_extra.exactly_once
[%here]
(Bonsai.Value.return (Ui_effect.print_s [%message "hello!"]))
in
let handle = Handle.create (Result_spec.sexp (module Unit)) component in
Handle.show handle;
[%expect {|
()
hello! |}];
Handle.show handle;
[%expect {| () |}]
;;
let%expect_test "exactly once with value" =
let component =
Bonsai_extra.exactly_once_with_value
[%here]
(module String)
(Bonsai.Value.return
(let%bind.Ui_effect () = Ui_effect.print_s [%message "hello!"] in
Ui_effect.return "done"))
in
let handle =
Handle.create
(Result_spec.sexp
(module struct
type t = string option [@@deriving sexp, equal]
end))
component
in
Handle.show handle;
[%expect {|
()
hello! |}];
Handle.show handle;
[%expect {| (done) |}]
;;
let%expect_test "yoink" =
let component =
let%sub state, set_state = Bonsai.state [%here] (module Int) ~default_model:0 in
let%sub get_state = Bonsai_extra.yoink state in
Bonsai_extra.exactly_once
[%here]
(let%map get_state = get_state
and set_state = set_state in
let%bind.Bonsai.Effect () = set_state 1 in
let%bind.Bonsai.Effect s = get_state in
Ui_effect.print_s [%message (s : int)])
in
let handle = Handle.create (Result_spec.sexp (module Unit)) component in
Handle.show handle;
[%expect {| () |}];
Handle.show handle;
[%expect {|
(s 1)
() |}]
;;
let%expect_test "freeze" =
let var = Bonsai.Var.create "hello" in
let component = Bonsai_extra.freeze [%here] (module String) (Bonsai.Var.value var) in
let handle = Handle.create (Result_spec.sexp (module String)) component in
Handle.show handle;
[%expect {| hello |}];
Bonsai.Var.set var "world";
Handle.show handle;
[%expect {| hello |}]
;;
let%expect_test "effect-lazy" =
let message = Bonsai.Var.create "hello" in
let on = Bonsai.Var.create true in
let component =
let%sub on_deactivate =
let%arr message = Bonsai.Var.value message in
let a =
print_endline "computing a...";
Effect.print_s [%sexp "a", (message : string)]
in
let b =
Effect.lazy_
(lazy
(print_endline "computing b...";
Effect.print_s [%sexp "b", (message : string)]))
in
Effect.Many [ a; b ]
in
if%sub Bonsai.Var.value on
then Bonsai.Edge.lifecycle ~on_deactivate ()
else Bonsai.const ()
in
let handle = Handle.create (Result_spec.sexp (module Unit)) component in
Handle.show handle;
Bonsai.Var.set message "there";
Handle.show handle;
Bonsai.Var.set message "world";
Handle.show handle;
[%expect
{|
computing a...
()
computing a...
()
computing a...
() |}];
Bonsai.Var.set on false;
Handle.show handle;
[%expect {|
()
(a world)
computing b...
(b world) |}]
;;
let%expect_test "id_gen" =
let module Id = Bonsai_extra.Id_gen (Int) () in
let component =
let%sub next = Id.component [%here] in
Bonsai.Edge.after_display
(let%map next = next in
let%bind.Bonsai.Effect id = next in
Ui_effect.print_s [%sexp (id : Id.t)])
in
let handle = Handle.create (Result_spec.sexp (module Unit)) component in
Handle.recompute_view handle;
Handle.recompute_view handle;
Handle.recompute_view handle;
Handle.recompute_view handle;
Handle.recompute_view handle;
[%expect {|
0
1
2
3 |}]
;;
let%expect_test "state_machine_dynamic_model" =
let component =
Bonsai_extra.state_machine0_dynamic_model
[%here]
(module String)
(module String)
~model:
(`Computed
(Bonsai.Value.return (function
| None -> "not set "
| Some s -> sprintf "set %s" s)))
~apply_action:(fun ~inject:_ ~schedule_event:_ _model action -> action)
in
let handle =
Handle.create
(module struct
type t = string * (string -> unit Effect.t)
type incoming = string
let view (s, _) = s
let incoming (_, s) = s
end)
component
in
Handle.show handle;
[%expect {| not set |}];
Handle.do_actions handle [ "here" ];
Handle.show handle;
[%expect {| set here |}]
;;
let%expect_test "portal" =
let var = Bonsai.Var.create (Sexp.Atom "hello") in
let component =
Bonsai_extra.with_inject_fixed_point (fun inject ->
let%sub () =
Bonsai.Edge.on_change
[%here]
(module Sexp)
(Bonsai.Var.value var)
~callback:inject
in
Bonsai.const ((), Ui_effect.print_s))
in
let handle = Handle.create (Result_spec.sexp (module Unit)) component in
Handle.recompute_view_until_stable handle;
[%expect {| hello |}];
Bonsai.Var.set var (Sexp.Atom "world");
Handle.recompute_view_until_stable handle;
[%expect {| world |}]
;;
let%expect_test "portal 2" =
let component =
Bonsai_extra.with_inject_fixed_point (fun inject_fix ->
let%sub state1, inject1 =
Bonsai.state_machine1
[%here]
(module Int)
(module Int)
~default_model:0
~apply_action:(fun ~inject:_ ~schedule_event inject model action ->
schedule_event (inject (model + action));
action)
inject_fix
in
let%sub (), inject2 =
Bonsai.state_machine1
[%here]
(module Unit)
(module Int)
~default_model:()
~apply_action:(fun ~inject:_ ~schedule_event state1 _model action ->
schedule_event (Ui_effect.print_s [%message (state1 : int) (action : int)]);
())
state1
in
return @@ Bonsai.Value.both inject1 inject2)
in
let handle =
Handle.create
(module struct
type t = int -> unit Effect.t
type incoming = int
let view _ = ""
let incoming = Fn.id
end)
component
in
Handle.show handle;
[%expect {||}];
Handle.do_actions handle [ 1 ];
Handle.flush handle;
[%expect {| ((state1 1) (action 1)) |}];
Handle.do_actions handle [ 5 ];
Handle.flush handle;
[%expect {| ((state1 5) (action 6)) |}];
Handle.do_actions handle [ 10 ];
Handle.flush handle;
[%expect {| ((state1 10) (action 15)) |}]
;;
let%expect_test "pipe" =
let component =
let%sub push_and_pop = Bonsai_extra.pipe [%here] (module String) in
return
@@ let%map push, pop = push_and_pop in
let pop s =
let%bind.Bonsai.Effect a = pop in
Ui_effect.print_s [%sexp "pop", (s : string), (a : string)]
in
push, pop
in
let handle =
Handle.create
(module struct
type t = (string -> unit Effect.t) * (string -> unit Effect.t)
type incoming =
[ `Push of string
| `Pop of string
]
let view _ = ""
let incoming (push, pop) = function
| `Push s -> push s
| `Pop s -> pop s
;;
end)
component
in
Handle.do_actions handle [ `Push "hello"; `Pop "a" ];
Handle.flush handle;
[%expect {| (pop a hello) |}];
Handle.do_actions handle [ `Push "world" ];
Handle.flush handle;
[%expect {| |}];
Handle.do_actions handle [ `Pop "b" ];
Handle.flush handle;
[%expect {| (pop b world) |}];
Handle.do_actions handle [ `Pop "c" ];
Handle.flush handle;
[%expect {| |}];
Handle.do_actions handle [ `Push "foo" ];
Handle.flush handle;
[%expect {| (pop c foo) |}];
Handle.do_actions
handle
[ `Push "hello"; `Push "world"; `Push "foo"; `Pop "a"; `Pop "b"; `Pop "c" ];
Handle.flush handle;
[%expect {|
(pop a hello)
(pop b world)
(pop c foo) |}]
;;
let%expect_test "multi-thunk" =
let module Id = Core.Unique_id.Int () in
let id =
Bonsai_extra.thunk (fun () ->
print_endline "pulling id!";
Id.create ())
in
let component =
let%map.Computation a = id
and b = id in
sprintf "%s %s" (Id.to_string a) (Id.to_string b)
in
let handle = Handle.create (Result_spec.sexp (module String)) component in
Handle.show handle;
[%expect {|
pulling id!
pulling id!
"1 0" |}]
;;
let%expect_test "scope_model" =
let var = Bonsai.Var.create true in
let component =
Bonsai_extra.scope_model
(module Bool)
~on:(Bonsai.Var.value var)
(Bonsai.state [%here] (module String) ~default_model:"default")
in
let handle =
Handle.create
(module struct
type t = string * (string -> unit Effect.t)
type incoming = string
let view (s, _) = s
let incoming (_, s) = s
end)
component
in
Handle.show handle;
[%expect {| default |}];
Handle.do_actions handle [ "a" ];
Handle.show handle;
[%expect {| a |}];
Bonsai.Var.set var false;
Handle.show handle;
[%expect {| default |}];
Handle.do_actions handle [ "b" ];
Handle.show handle;
[%expect {| b |}];
Bonsai.Var.set var true;
Handle.show handle;
[%expect {| a |}]
;;
let%expect_test "thunk-storage" =
let module Id = Core.Unique_id.Int () in
let var = Bonsai.Var.create true in
let id =
Bonsai_extra.thunk (fun () ->
print_endline "pulling id!";
Id.create ())
in
let component =
if%sub Bonsai.Var.value var
then (
let%map.Computation id = id in
Id.to_string id)
else Bonsai.const ""
in
let handle = Handle.create (Result_spec.sexp (module String)) component in
Handle.show handle;
[%expect {|
pulling id!
0 |}];
Bonsai.Var.set var false;
Handle.show handle;
[%expect {| "" |}];
Bonsai.Var.set var true;
Handle.show handle;
[%expect {| 0 |}]
;;
let%test_module "mirror" =
(module struct
let prepare_test ~store ~interactive =
let store = Bonsai.Var.create store in
let interactive = Bonsai.Var.create interactive in
let store_set =
(fun value ->
printf "store set to \"%s\"" value;
Bonsai.Var.set store value)
|> Ui_effect.of_sync_fun
in
let interactive_set =
(fun value ->
printf "interactive set to \"%s\"" value;
Bonsai.Var.set interactive value)
|> Ui_effect.of_sync_fun
in
let component =
let%sub () =
Bonsai_extra.mirror
[%here]
(module String)
~store_set:(Bonsai.Value.return store_set)
~interactive_set:(Bonsai.Value.return interactive_set)
~store_value:(Bonsai.Var.value store)
~interactive_value:(Bonsai.Var.value interactive)
in
return
(let%map store = Bonsai.Var.value store
and interactive = Bonsai.Var.value interactive in
sprintf "store: %s, interactive: %s" store interactive)
in
let handle = Handle.create (Result_spec.string (module String)) component in
handle, store, interactive
;;
let%expect_test "starts stable" =
let handle, _store, _interactive = prepare_test ~store:"a" ~interactive:"a" in
Handle.show handle;
[%expect {| store: a, interactive: a |}]
;;
let%expect_test "starts unstable" =
let handle, _store, _interactive = prepare_test ~store:"a" ~interactive:"b" in
Handle.show handle;
[%expect {|
store: a, interactive: b
interactive set to "a" |}];
Handle.show handle;
[%expect {| store: a, interactive: a |}]
;;
let%expect_test "starts stable and then interactive changes" =
let handle, _store, interactive = prepare_test ~store:"a" ~interactive:"a" in
Handle.show handle;
[%expect {| store: a, interactive: a |}];
Bonsai.Var.set interactive "b";
Handle.show handle;
[%expect {|
store: a, interactive: b
store set to "b" |}];
Handle.show handle;
[%expect {| store: b, interactive: b |}]
;;
let%expect_test "starts stable and then store changes" =
let handle, store, _interactive = prepare_test ~store:"a" ~interactive:"a" in
Handle.show handle;
[%expect {| store: a, interactive: a |}];
Bonsai.Var.set store "b";
Handle.show handle;
[%expect {|
store: b, interactive: a
interactive set to "b" |}];
Handle.show handle;
[%expect {| store: b, interactive: b |}]
;;
let%expect_test "starts stable and then both change at the same time" =
let handle, store, interactive = prepare_test ~store:"a" ~interactive:"a" in
Handle.show handle;
[%expect {| store: a, interactive: a |}];
Bonsai.Var.set store "b";
Bonsai.Var.set interactive "c";
Handle.show handle;
[%expect {|
store: b, interactive: c
store set to "c" |}];
Handle.show handle;
[%expect {| store: c, interactive: c |}]
;;
end)
;;