-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathAsyncIterator.resi
188 lines (144 loc) · 4.08 KB
/
AsyncIterator.resi
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
/***
Bindings to async iterators, a way to do async iteration in JavaScript.
See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) on MDN.*/
/**
The type representing an async iterator.
*/
type t<'a>
type value<'a> = {
/**
Whether there are more values to iterate on before the iterator is done.
*/
done: bool,
/**
The value of this iteration, if any.
*/
value: option<'a>,
}
/**
`make(nextFn)`
Creates an async iterator from a function that returns the next value of the iterator.
## Examples
- A simple example, creating an async iterator that returns 1, 2, 3:
```rescript
let context = ref(0)
let asyncIterator = AsyncIterator.make(async () => {
let currentValue = context.contents
// Increment current value
context := currentValue + 1
{
AsyncIterator.value: Some(currentValue),
done: currentValue >= 3
}
})
// This will log 1, 2, 3
let main = async () => await asyncIterator->AsyncIterator.forEach(value =>
switch value {
| Some(value) => Console.log(value)
| None => ()
}
)
main()->ignore
```
*/
let make: (unit => promise<value<'value>>) => t<'value>
/**
`value(value)`
Shorthand for creating a value object with the provided value, and the `done` property set to false.
## Examples
```rescript
let context = ref(0)
let asyncIterator = AsyncIterator.make(async () => {
let currentValue = context.contents
// Increment current value
context := currentValue + 1
if currentValue >= 3 {
AsyncIterator.done()
} else {
AsyncIterator.value(currentValue)
}
})
```
*/
let value: 'value => value<'value>
/**
`done(~finalValue=?)`
Shorthand for creating a value object with the `done` property set to true, and the provided value as the final value, if any.
## Examples
```rescript
let context = ref(0)
let asyncIterator = AsyncIterator.make(async () => {
let currentValue = context.contents
// Increment current value
context := currentValue + 1
if currentValue >= 3 {
AsyncIterator.done()
} else {
AsyncIterator.value(currentValue)
}
})
```
*/
let done: (~finalValue: 'value=?) => value<'value>
/**
`next(asyncIterator)`
Returns the next value of the iterator, if any.
See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) on MDN.
## Examples
- A simple example, getting the next value:
```rescript
let asyncIterator: AsyncIterator.t<(string, string)> = %raw(`
(() => {
var map1 = new Map();
map1.set('first', '1');
map1.set('second', '2');
var iterator1 = map1[Symbol.iterator]();
return iterator1;
})()
`)
let processMyAsyncIterator = async () => {
// ReScript doesn't have `for ... of` loops, but it's easy to mimic using a while loop.
let break = ref(false)
while !break.contents {
// Await the next iterator value
let {value, done} = await asyncIterator->AsyncIterator.next
// Exit the while loop if the iterator says it's done
break := done
if done {
value
->Option.isNone
->assertEqual(true)
}
}
}
processMyAsyncIterator()->ignore
```
*/
@send
external next: t<'a> => promise<value<'a>> = "next"
/**
`forEach(iterator, fn)` consumes all values in the async iterator and runs the callback `fn` for each value.
See [iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) on MDN.
## Examples
```rescript
// Let's pretend we get an async iterator returning ints from somewhere.
let asyncIterator: AsyncIterator.t<(string, string)> = %raw(`
(() => {
var map1 = new Map();
map1.set('first', '1');
map1.set('second', '2');
var iterator1 = map1[Symbol.iterator]();
return iterator1;
})()
`)
let main = async () =>
await asyncIterator->AsyncIterator.forEach(v => {
switch v {
| Some(("second", value)) => assertEqual(value, "2")
| _ => ()
}
})
main()->ignore
```
*/
let forEach: (t<'a>, option<'a> => unit) => promise<unit>