-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathDelegatedYield.ts
More file actions
34 lines (33 loc) · 1.13 KB
/
DelegatedYield.ts
File metadata and controls
34 lines (33 loc) · 1.13 KB
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
import { GeneratorIterator } from "./GeneratorIterator";
export function __TS__DelegatedYield<T>(this: void, iterable: string | GeneratorIterator | Iterable<T> | readonly T[]) {
if (typeof iterable === "string") {
for (const index of $range(0, iterable.length - 1)) {
coroutine.yield(iterable[index]);
}
} else if ("____coroutine" in iterable) {
const co = iterable.____coroutine;
while (true) {
const [status, value] = coroutine.resume(co);
if (!status) throw value;
if (coroutine.status(co) === "dead") {
return value;
} else {
coroutine.yield(value);
}
}
} else if (iterable[Symbol.iterator]) {
const iterator = iterable[Symbol.iterator]();
while (true) {
const result = iterator.next();
if (result.done) {
return result.value;
} else {
coroutine.yield(result.value);
}
}
} else {
for (const value of iterable as readonly T[]) {
coroutine.yield(value);
}
}
}