-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
wonka_helpers.re
56 lines (47 loc) · 1.21 KB
/
wonka_helpers.re
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
open Wonka_types;
let talkbackPlaceholder = (. _: talkbackT) => ();
let captureTalkback =
(
source: sourceT('a),
sinkWithTalkback: (. signalT('a), (. talkbackT) => unit) => unit,
) => {
let talkback = ref(talkbackPlaceholder);
source((. signal) => {
switch (signal) {
| Start(x) => talkback := x
| _ => ()
};
sinkWithTalkback(. signal, talkback^);
});
};
type trampolineT = {
mutable ended: bool,
mutable looping: bool,
mutable pull: bool,
};
let makeTrampoline = (sink: sinkT('a), f: (. unit) => option('a)) => {
let state: trampolineT = {ended: false, looping: false, pull: false};
sink(.
Start(
(. signal) =>
switch (signal, state.looping) {
| (Pull, false) =>
state.pull = true;
state.looping = true;
while (state.pull && !state.ended) {
switch (f(.)) {
| Some(x) =>
state.pull = false;
sink(. Push(x));
| None =>
state.ended = true;
sink(. End);
};
};
state.looping = false;
| (Pull, true) => state.pull = true
| (Close, _) => state.ended = true
},
),
);
};