Skip to content

Commit f7d70da

Browse files
authored
migrating to AsyncIterableProducer (microsoft#257043)
* migrating to AsyncIterableProducer * adding methods
1 parent 38bf4ce commit f7d70da

File tree

7 files changed

+85
-19
lines changed

7 files changed

+85
-19
lines changed

src/vs/base/common/async.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,72 @@ export class AsyncIterableProducer<T> implements AsyncIterable<T> {
24012401
});
24022402
}
24032403

2404+
public static fromArray<T>(items: T[]): AsyncIterableProducer<T> {
2405+
return new AsyncIterableProducer<T>((writer) => {
2406+
writer.emitMany(items);
2407+
});
2408+
}
2409+
2410+
public static fromPromise<T>(promise: Promise<T[]>): AsyncIterableProducer<T> {
2411+
return new AsyncIterableProducer<T>(async (emitter) => {
2412+
emitter.emitMany(await promise);
2413+
});
2414+
}
2415+
2416+
public static fromPromisesResolveOrder<T>(promises: Promise<T>[]): AsyncIterableProducer<T> {
2417+
return new AsyncIterableProducer<T>(async (emitter) => {
2418+
await Promise.all(promises.map(async (p) => emitter.emitOne(await p)));
2419+
});
2420+
}
2421+
2422+
public static merge<T>(iterables: AsyncIterable<T>[]): AsyncIterableProducer<T> {
2423+
return new AsyncIterableProducer(async (emitter) => {
2424+
await Promise.all(iterables.map(async (iterable) => {
2425+
for await (const item of iterable) {
2426+
emitter.emitOne(item);
2427+
}
2428+
}));
2429+
});
2430+
}
2431+
2432+
public static EMPTY = AsyncIterableProducer.fromArray<any>([]);
2433+
2434+
public static map<T, R>(iterable: AsyncIterable<T>, mapFn: (item: T) => R): AsyncIterableProducer<R> {
2435+
return new AsyncIterableProducer<R>(async (emitter) => {
2436+
for await (const item of iterable) {
2437+
emitter.emitOne(mapFn(item));
2438+
}
2439+
});
2440+
}
2441+
2442+
public map<R>(mapFn: (item: T) => R): AsyncIterableProducer<R> {
2443+
return AsyncIterableProducer.map(this, mapFn);
2444+
}
2445+
2446+
public static coalesce<T>(iterable: AsyncIterable<T | undefined | null>): AsyncIterableProducer<T> {
2447+
return <AsyncIterableProducer<T>>AsyncIterableProducer.filter(iterable, item => !!item);
2448+
}
2449+
2450+
public coalesce(): AsyncIterableProducer<NonNullable<T>> {
2451+
return AsyncIterableProducer.coalesce(this) as AsyncIterableProducer<NonNullable<T>>;
2452+
}
2453+
2454+
public static filter<T>(iterable: AsyncIterable<T>, filterFn: (item: T) => boolean): AsyncIterableProducer<T> {
2455+
return new AsyncIterableProducer<T>(async (emitter) => {
2456+
for await (const item of iterable) {
2457+
if (filterFn(item)) {
2458+
emitter.emitOne(item);
2459+
}
2460+
}
2461+
});
2462+
}
2463+
2464+
public filter<T2 extends T>(filterFn: (item: T) => item is T2): AsyncIterableProducer<T2>;
2465+
public filter(filterFn: (item: T) => boolean): AsyncIterableProducer<T>;
2466+
public filter(filterFn: (item: T) => boolean): AsyncIterableProducer<T> {
2467+
return AsyncIterableProducer.filter(this, filterFn);
2468+
}
2469+
24042470
private _finishOk(): void {
24052471
this._producerConsumer.produceFinal({ ok: true, value: { done: true, value: undefined } });
24062472
}

src/vs/editor/contrib/colorPicker/browser/hoverColorPicker/hoverColorPickerParticipant.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { AsyncIterableObject } from '../../../../../base/common/async.js';
6+
import { AsyncIterableProducer } from '../../../../../base/common/async.js';
77
import { CancellationToken } from '../../../../../base/common/cancellation.js';
88
import { ICodeEditor } from '../../../../browser/editorBrowser.js';
99
import { Range } from '../../../../common/core/range.js';
@@ -65,8 +65,8 @@ export class HoverColorPickerParticipant implements IEditorHoverParticipant<Colo
6565
return [];
6666
}
6767

68-
public computeAsync(anchor: HoverAnchor, lineDecorations: IModelDecoration[], source: HoverStartSource, token: CancellationToken): AsyncIterableObject<ColorHover> {
69-
return AsyncIterableObject.fromPromise(this._computeAsync(anchor, lineDecorations, source));
68+
public computeAsync(anchor: HoverAnchor, lineDecorations: IModelDecoration[], source: HoverStartSource, token: CancellationToken): AsyncIterableProducer<ColorHover> {
69+
return AsyncIterableProducer.fromPromise(this._computeAsync(anchor, lineDecorations, source));
7070
}
7171

7272
private async _computeAsync(_anchor: HoverAnchor, lineDecorations: IModelDecoration[], source: HoverStartSource): Promise<ColorHover[]> {

src/vs/editor/contrib/hover/browser/contentHoverComputer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IActiveCodeEditor, ICodeEditor } from '../../../browser/editorBrowser.j
99
import { IModelDecoration } from '../../../common/model.js';
1010
import { HoverStartSource, IHoverComputer } from './hoverOperation.js';
1111
import { HoverAnchor, HoverAnchorType, IEditorHoverParticipant, IHoverPart } from './hoverTypes.js';
12-
import { AsyncIterableObject } from '../../../../base/common/async.js';
12+
import { AsyncIterableProducer } from '../../../../base/common/async.js';
1313

1414
export interface ContentHoverComputerOptions {
1515
shouldFocus: boolean;
@@ -64,19 +64,19 @@ export class ContentHoverComputer implements IHoverComputer<ContentHoverComputer
6464
});
6565
}
6666

67-
public computeAsync(options: ContentHoverComputerOptions, token: CancellationToken): AsyncIterableObject<IHoverPart> {
67+
public computeAsync(options: ContentHoverComputerOptions, token: CancellationToken): AsyncIterableProducer<IHoverPart> {
6868
const anchor = options.anchor;
6969

7070
if (!this._editor.hasModel() || !anchor) {
71-
return AsyncIterableObject.EMPTY;
71+
return AsyncIterableProducer.EMPTY;
7272
}
7373

7474
const lineDecorations = ContentHoverComputer._getLineDecorations(this._editor, anchor);
7575

76-
return AsyncIterableObject.merge(
76+
return AsyncIterableProducer.merge(
7777
this._participants.map((participant) => {
7878
if (!participant.computeAsync) {
79-
return AsyncIterableObject.EMPTY;
79+
return AsyncIterableProducer.EMPTY;
8080
}
8181
return participant.computeAsync(anchor, lineDecorations, options.source, token);
8282
})

src/vs/editor/contrib/hover/browser/getHover.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { AsyncIterableObject } from '../../../../base/common/async.js';
6+
import { AsyncIterableProducer } from '../../../../base/common/async.js';
77
import { CancellationToken } from '../../../../base/common/cancellation.js';
88
import { onUnexpectedExternalError } from '../../../../base/common/errors.js';
99
import { registerModelAndPositionCommand } from '../../../browser/editorExtensions.js';
@@ -37,7 +37,7 @@ async function executeProvider(provider: HoverProvider, ordinal: number, model:
3737
export function getHoverProviderResultsAsAsyncIterable(registry: LanguageFeatureRegistry<HoverProvider>, model: ITextModel, position: Position, token: CancellationToken, recursive = false): AsyncIterable<HoverProviderResult> {
3838
const providers = registry.ordered(model, recursive);
3939
const promises = providers.map((provider, index) => executeProvider(provider, index, model, position, token));
40-
return AsyncIterableObject.fromPromisesResolveOrder(promises).coalesce();
40+
return AsyncIterableProducer.fromPromisesResolveOrder(promises).coalesce();
4141
}
4242

4343
export async function getHoversPromise(registry: LanguageFeatureRegistry<HoverProvider>, model: ITextModel, position: Position, token: CancellationToken, recursive = false): Promise<Hover[]> {

src/vs/editor/contrib/hover/browser/hoverOperation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { AsyncIterableObject, CancelableAsyncIterableObject, createCancelableAsyncIterable, RunOnceScheduler } from '../../../../base/common/async.js';
6+
import { AsyncIterableProducer, CancelableAsyncIterableObject, createCancelableAsyncIterable, RunOnceScheduler } from '../../../../base/common/async.js';
77
import { CancellationToken } from '../../../../base/common/cancellation.js';
88
import { onUnexpectedError } from '../../../../base/common/errors.js';
99
import { Emitter } from '../../../../base/common/event.js';
@@ -15,7 +15,7 @@ export interface IHoverComputer<TArgs, TResult> {
1515
/**
1616
* This is called after half the hover time
1717
*/
18-
computeAsync?: (args: TArgs, token: CancellationToken) => AsyncIterableObject<TResult>;
18+
computeAsync?: (args: TArgs, token: CancellationToken) => AsyncIterableProducer<TResult>;
1919
/**
2020
* This is called after all the hover time
2121
*/

src/vs/editor/contrib/hover/browser/markdownHoverParticipant.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { IKeybindingService } from '../../../../platform/keybinding/common/keybi
3030
import { ClickAction, HoverPosition, KeyDownAction } from '../../../../base/browser/ui/hover/hoverWidget.js';
3131
import { KeyCode } from '../../../../base/common/keyCodes.js';
3232
import { IHoverService, WorkbenchHoverDelegate } from '../../../../platform/hover/browser/hover.js';
33-
import { AsyncIterableObject } from '../../../../base/common/async.js';
33+
import { AsyncIterableProducer } from '../../../../base/common/async.js';
3434
import { LanguageFeatureRegistry } from '../../../common/languageFeatureRegistry.js';
3535
import { getHoverProviderResultsAsAsyncIterable } from './getHover.js';
3636
import { ICommandService } from '../../../../platform/commands/common/commands.js';
@@ -155,14 +155,14 @@ export class MarkdownHoverParticipant implements IEditorHoverParticipant<Markdow
155155

156156
public computeAsync(anchor: HoverAnchor, lineDecorations: IModelDecoration[], source: HoverStartSource, token: CancellationToken): AsyncIterable<MarkdownHover> {
157157
if (!this._editor.hasModel() || anchor.type !== HoverAnchorType.Range) {
158-
return AsyncIterableObject.EMPTY;
158+
return AsyncIterableProducer.EMPTY;
159159
}
160160

161161
const model = this._editor.getModel();
162162

163163
const hoverProviderRegistry = this._languageFeaturesService.hoverProvider;
164164
if (!hoverProviderRegistry.has(model)) {
165-
return AsyncIterableObject.EMPTY;
165+
return AsyncIterableProducer.EMPTY;
166166
}
167167
return this._getMarkdownHovers(hoverProviderRegistry, model, anchor, token);
168168
}

src/vs/editor/contrib/inlayHints/browser/inlayHintsHover.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { AsyncIterableObject } from '../../../../base/common/async.js';
6+
import { AsyncIterableProducer } from '../../../../base/common/async.js';
77
import { CancellationToken } from '../../../../base/common/cancellation.js';
88
import { IMarkdownString, isEmptyMarkdownString, MarkdownString } from '../../../../base/common/htmlContent.js';
99
import { ICodeEditor, IEditorMouseEvent, MouseTargetType } from '../../../browser/editorBrowser.js';
@@ -77,12 +77,12 @@ export class InlayHintsHover extends MarkdownHoverParticipant implements IEditor
7777
return [];
7878
}
7979

80-
override computeAsync(anchor: HoverAnchor, _lineDecorations: IModelDecoration[], source: HoverStartSource, token: CancellationToken): AsyncIterableObject<MarkdownHover> {
80+
override computeAsync(anchor: HoverAnchor, _lineDecorations: IModelDecoration[], source: HoverStartSource, token: CancellationToken): AsyncIterableProducer<MarkdownHover> {
8181
if (!(anchor instanceof InlayHintsHoverAnchor)) {
82-
return AsyncIterableObject.EMPTY;
82+
return AsyncIterableProducer.EMPTY;
8383
}
8484

85-
return new AsyncIterableObject<MarkdownHover>(async executor => {
85+
return new AsyncIterableProducer<MarkdownHover>(async executor => {
8686

8787
const { part } = anchor;
8888
await part.item.resolve(token);

0 commit comments

Comments
 (0)