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 =>
11 curry(sink => {
12 let size = Rebel.Array.size(arr);
13 let state = {index: 0, ended: false, looping: false, pull: false};
14
15 sink(.
16 Start(
17 (. signal) =>
18 switch (signal, state.looping) {
19 | (Pull, false) =>
20 state.pull = true;
21 state.looping = true;
22
23 while (state.pull && !state.ended) {
24 let index = state.index;
25 if (index < size) {
26 let x = Rebel.Array.getUnsafe(arr, index);
27 state.index = index + 1;
28 state.pull = false;
29 sink(. Push(x));
30 } else {
31 state.ended = true;
32 sink(. End);
33 };
34 };
35
36 state.looping = false;
37 | (Pull, true) => state.pull = true
38 | (Close, _) => state.ended = true
39 },
40 ),
41 );
42 });