1open Wonka_types;
2
3type fromArrayState('a) = {
4 mutable index: int,
5 mutable ended: bool,
6 mutable looping: bool,
7 mutable pull: bool
8};
9
10let fromArray = arr => curry(sink => {
11 let size = Rebel.Array.size(arr);
12 let state = {
13 index: 0,
14 ended: false,
15 looping: false,
16 pull: false
17 };
18
19 sink(.Start((.signal) => {
20 switch (signal, state.looping) {
21 | (Pull, false) => {
22 state.pull = true;
23 state.looping = true;
24
25 while (state.pull && !state.ended) {
26 let index = state.index;
27 if (index < size) {
28 let x = Rebel.Array.getUnsafe(arr, index);
29 state.index = index + 1;
30 state.pull = false;
31 sink(.Push(x));
32 } else {
33 state.ended = true;
34 sink(.End);
35 }
36 };
37
38 state.looping = false;
39 }
40 | (Pull, true) => state.pull = true
41 | (Close, _) => state.ended = true
42 }
43 }));
44});