src/vs/platform/terminal/common/requestStore.ts
73 LOC · 71 covered · 2 uncovered · 8 ranges · 5 concepts · 4 introducers · 3 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
requestStore.ts ×4
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { timeout } from '../../../base/common/async.js';
import { CancellationTokenSource } from '../../../base/common/cancellation.js';
import { Emitter } from '../../../base/common/event.js';
import { Disposable, dispose, IDisposable, toDisposable } from '../../../base/common/lifecycle.js';
import { ILogService } from '../../log/common/log.js';
/**
* A helper class to track requests that have replies. Using this it's easy to implement an event
* that accepts a reply.
*/
export class RequestStore<T, RequestArgs> extends Disposable {
private _lastRequestId = 0;
private readonly _timeout: number;
private _pendingRequests: Map<number, (resolved: T) => void> = new Map();
private _pendingRequestDisposables: Map<number, IDisposable[]> = new Map();
private readonly _onCreateRequest = this._register(new Emitter<RequestArgs & { requestId: number }>());
readonly onCreateRequest = this._onCreateRequest.event;
/**
* @param timeout How long in ms to allow requests to go unanswered for, undefined will use the
* default (15 seconds).
*/
constructor(
timeout: number | undefined,
@ILogService private readonly _logService: ILogService
) {
super();
this._timeout = timeout === undefined ? 15000 : timeout;
this._register(toDisposable(() => {
for (const d of this._pendingRequestDisposables.values()) {
}
}
/**
* Creates a request.
* @param args The arguments to pass to the onCreateRequest event.
*/
createRequest(args: RequestArgs): Promise<T> {
const requestId = ++this._lastRequestId;
this._pendingRequests.set(requestId, resolve);
this._onCreateRequest.fire({ requestId, ...args });
const tokenSource = new CancellationTokenSource();
timeout(this._timeout, tokenSource.token).then(() => reject(`Request ${requestId} timed out (${this._timeout}ms)`));
this._pendingRequestDisposables.set(requestId, [toDisposable(() => tokenSource.cancel())]);
});
}
/**
* Accept a reply to a request.
* @param requestId The request ID originating from the onCreateRequest event.
* @param data The reply data.
*/
acceptReply(requestId: number, data: T) {
if (resolveRequest) {
this._pendingRequests.delete(requestId);
dispose(this._pendingRequestDisposables.get(requestId) || []);
this._pendingRequestDisposables.delete(requestId);
resolveRequest(data);
} else {
this._logService.warn(`RequestStore#acceptReply was called without receiving a matching request ${requestId}`);
}