Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/asyncIterableUtils.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
export namespace AsyncIterUtils {
7
8
export async function* map<T0, T1>(iterable: AsyncIterable<T0>, mapItem: (item: T0) => T1): AsyncIterable<T1> {
9
for await (const item of iterable) {
10
yield mapItem(item);
11
}
12
}
13
14
export async function* mapWithReturn<T0, R0, N, T1, R1 = R0>(
15
iterable: AsyncIterable<T0, R0, N>,
16
mapItem: (item: T0) => T1,
17
mapReturn: (ret: R0) => R1,
18
): AsyncGenerator<T1, R1, N> {
19
const iter = iterable[Symbol.asyncIterator]();
20
let v: IteratorResult<T0, R0>;
21
22
while (!((v = await iter.next()).done)) {
23
yield mapItem(v.value);
24
}
25
26
return mapReturn(v.value);
27
}
28
29
export async function* filter<T>(iterable: AsyncIterable<T>, filterItem: (item: T) => boolean): AsyncIterable<T> {
30
for await (const item of iterable) {
31
if (filterItem(item)) {
32
yield item;
33
}
34
}
35
}
36
37
export async function toArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {
38
const arr: T[] = [];
39
for await (const item of iterable) {
40
arr.push(item);
41
}
42
return arr;
43
}
44
45
export async function* fromArray<T>(arr: T[]): AsyncIterable<T> {
46
for (const item of arr) {
47
yield item;
48
}
49
}
50
51
export async function* fromArrayWithReturn<T, R>(arr: T[], returnValue: R): AsyncGenerator<T, R> {
52
for (const item of arr) {
53
yield item;
54
}
55
return returnValue;
56
}
57
58
export async function toArrayWithReturn<T, R>(iterable: AsyncIterable<T, R>): Promise<[T[], ret: R]> {
59
const iter = iterable[Symbol.asyncIterator]();
60
const arr: T[] = [];
61
let v: IteratorResult<T, R>;
62
63
while (!((v = await iter.next()).done)) {
64
arr.push(v.value);
65
}
66
67
return [arr, v.value];
68
}
69
70
export async function drainUntilReturn<T, R>(iterable: AsyncIterable<T, R>): Promise<R> {
71
const iter = iterable[Symbol.asyncIterator]();
72
let v: IteratorResult<T, R>;
73
74
do {
75
v = await iter.next();
76
} while (!v.done);
77
78
return v.value;
79
}
80
}
81
82
/**
83
* Namespace for extensions to AsyncIterUtils that are not generally useful enough to be in the main namespace, but are still worth keeping around.
84
*/
85
export namespace AsyncIterUtilsExt {
86
87
export async function* splitLines(stream: AsyncIterable<string>): AsyncIterable<string> {
88
let buffer: string | null = null;
89
90
for await (const chunk of stream) {
91
buffer ??= '';
92
buffer += chunk;
93
94
const parts: string[] = buffer.split(/\r?\n/);
95
buffer = parts.pop() ?? '';
96
97
yield* parts;
98
}
99
100
if (buffer !== null) {
101
yield buffer;
102
}
103
}
104
}
105
106