1open Wonka_types;
2
3let talkbackPlaceholder = (. _: talkbackT) => ();
4
5let captureTalkback =
6 (
7 source: sourceT('a),
8 sinkWithTalkback: (. signalT('a), (. talkbackT) => unit) => unit,
9 ) => {
10 let talkback = ref(talkbackPlaceholder);
11
12 source((. signal) => {
13 switch (signal) {
14 | Start(x) => talkback := x
15 | _ => ()
16 };
17
18 sinkWithTalkback(. signal, talkback^);
19 });
20};
21
22type trampolineT = {
23 mutable ended: bool,
24 mutable looping: bool,
25 mutable pull: bool,
26};
27
28let makeTrampoline = (sink: sinkT('a), f: (. unit) => option('a)) => {
29 let state: trampolineT = {ended: false, looping: false, pull: false};
30
31 sink(.
32 Start(
33 (. signal) =>
34 switch (signal, state.looping) {
35 | (Pull, false) =>
36 state.pull = true;
37 state.looping = true;
38
39 while (state.pull && !state.ended) {
40 switch (f(.)) {
41 | Some(x) =>
42 state.pull = false;
43 sink(. Push(x));
44 | None =>
45 state.ended = true;
46 sink(. End);
47 };
48 };
49
50 state.looping = false;
51 | (Pull, true) => state.pull = true
52 | (Close, _) => state.ended = true
53 },
54 ),
55 );
56};