Skip to content

Commit ee6c9a1

Browse files
authored
Adding cancelable async iterable producer (microsoft#259192)
adding cancelable async iterable producer
1 parent bc94816 commit ee6c9a1

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/vs/base/common/async.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,24 +2168,12 @@ export class AsyncIterableObject<T> implements AsyncIterable<T> {
21682168
}
21692169
}
21702170

2171-
export class CancelableAsyncIterableObject<T> extends AsyncIterableObject<T> {
2172-
constructor(
2173-
private readonly _source: CancellationTokenSource,
2174-
executor: AsyncIterableExecutor<T>
2175-
) {
2176-
super(executor);
2177-
}
21782171

2179-
cancel(): void {
2180-
this._source.cancel();
2181-
}
2182-
}
2183-
2184-
export function createCancelableAsyncIterable<T>(callback: (token: CancellationToken) => AsyncIterable<T>): CancelableAsyncIterableObject<T> {
2172+
export function createCancelableAsyncIterableProducer<T>(callback: (token: CancellationToken) => AsyncIterable<T>): CancelableAsyncIterableProducer<T> {
21852173
const source = new CancellationTokenSource();
21862174
const innerIterable = callback(source.token);
21872175

2188-
return new CancelableAsyncIterableObject<T>(source, async (emitter) => {
2176+
return new CancelableAsyncIterableProducer<T>(source, async (emitter) => {
21892177
const subscription = source.token.onCancellationRequested(() => {
21902178
subscription.dispose();
21912179
source.dispose();
@@ -2492,6 +2480,19 @@ export class AsyncIterableProducer<T> implements AsyncIterable<T> {
24922480
}
24932481
}
24942482

2483+
export class CancelableAsyncIterableProducer<T> extends AsyncIterableProducer<T> {
2484+
constructor(
2485+
private readonly _source: CancellationTokenSource,
2486+
executor: AsyncIterableExecutor<T>
2487+
) {
2488+
super(executor);
2489+
}
2490+
2491+
cancel(): void {
2492+
this._source.cancel();
2493+
}
2494+
}
2495+
24952496
//#endregion
24962497

24972498
export const AsyncReaderEndOfStream = Symbol('AsyncReaderEndOfStream');

src/vs/editor/contrib/hover/browser/hoverOperation.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 { AsyncIterableProducer, CancelableAsyncIterableObject, createCancelableAsyncIterable, RunOnceScheduler } from '../../../../base/common/async.js';
6+
import { AsyncIterableProducer, CancelableAsyncIterableProducer, createCancelableAsyncIterableProducer, 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';
@@ -70,7 +70,7 @@ export class HoverOperation<TArgs, TResult> extends Disposable {
7070
private readonly _loadingMessageScheduler = this._register(new Debouncer((options: TArgs) => this._triggerLoadingMessage(options), 0));
7171

7272
private _state = HoverOperationState.Idle;
73-
private _asyncIterable: CancelableAsyncIterableObject<TResult> | null = null;
73+
private _asyncIterable: CancelableAsyncIterableProducer<TResult> | null = null;
7474
private _asyncIterableDone: boolean = false;
7575
private _result: TResult[] = [];
7676
private _options: TArgs | undefined;
@@ -119,7 +119,7 @@ export class HoverOperation<TArgs, TResult> extends Disposable {
119119

120120
if (this._computer.computeAsync) {
121121
this._asyncIterableDone = false;
122-
this._asyncIterable = createCancelableAsyncIterable(token => this._computer.computeAsync!(options, token));
122+
this._asyncIterable = createCancelableAsyncIterableProducer(token => this._computer.computeAsync!(options, token));
123123

124124
(async () => {
125125
try {

0 commit comments

Comments
 (0)