src/vs/workbench/contrib/debug/common/abstractDebugAdapter.ts
226 LOC · 164 covered · 62 uncovered · 40 ranges · 28 concepts · 5 introducers · 14 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.
/*---------------------------------------------------------------------------------------------
abstractDebugAdapter.ts ×17
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Emitter, Event } from '../../../../base/common/event.js';
import { IDebugAdapter } from './debug.js';
import { timeout } from '../../../../base/common/async.js';
import { localize } from '../../../../nls.js';
/**
* Abstract implementation of the low level API for a debug adapter.
* Missing is how this API communicates with the debug adapter.
*/
export abstract class AbstractDebugAdapter implements IDebugAdapter {
private sequence: number;
private pendingRequests = new Map<number, (e: DebugProtocol.Response) => void>();
private pendingRequestTimers = new Map<number, Timeout>();
private requestCallback: ((request: DebugProtocol.Request) => void) | undefined;
private eventCallback: ((request: DebugProtocol.Event) => void) | undefined;
private messageCallback: ((message: DebugProtocol.ProtocolMessage) => void) | undefined;
private queue: DebugProtocol.ProtocolMessage[] = [];
protected readonly _onError = new Emitter<Error>();
protected readonly _onExit = new Emitter<number | null>();
constructor() {
}
abstract startSession(): Promise<void>;
abstract stopSession(): Promise<void>;
abstract sendMessage(message: DebugProtocol.ProtocolMessage): void;
get onError(): Event<Error> {
return this._onError.event;
}
get onExit(): Event<number | null> {
return this._onExit.event;
}
onMessage(callback: (message: DebugProtocol.ProtocolMessage) => void): void {
if (this.messageCallback) {
this._onError.fire(new Error(`attempt to set more than one 'Message' callback`));
}
this.messageCallback = callback;
}
onEvent(callback: (event: DebugProtocol.Event) => void): void {
this._onError.fire(new Error(`attempt to set more than one 'Event' callback`));
}
}
onRequest(callback: (request: DebugProtocol.Request) => void): void {
if (this.requestCallback) {
this._onError.fire(new Error(`attempt to set more than one 'Request' callback`));
}
this.requestCallback = callback;
}
sendResponse(response: DebugProtocol.Response): void {
if (response.seq > 0) {
this._onError.fire(new Error(`attempt to send more than one response for command ${response.command}`));
} else {
this.internalSend('response', response);
}
}
sendRequest(command: string, args: any, clb: (result: DebugProtocol.Response) => void, timeout?: number): number {
command: command
};
if (args && Object.keys(args).length > 0) {
request.arguments = args;
}
this.internalSend('request', request);
if (typeof timeout === 'number') {
this.pendingRequestTimers.delete(request.seq);
const clb = this.pendingRequests.get(request.seq);
if (clb) {
this.pendingRequests.delete(request.seq);
const err: DebugProtocol.Response = {
type: 'response',
seq: 0,
request_seq: request.seq,
success: false,
command,
message: localize('timeout', "Timeout after {0} ms for '{1}'", timeout, command)
};
clb(err);
}
this.pendingRequestTimers.set(request.seq, timer);
}
// store callback for this request
this.pendingRequests.set(request.seq, clb);
}
return request.seq;
}
acceptMessage(message: DebugProtocol.ProtocolMessage): void {
this.messageCallback(message);
this.queue.push(message);
if (this.queue.length === 1) {
// first item = need to start processing loop
this.processQueue();
}
}
}
/**
* Returns whether we should insert a timeout between processing messageA
* and messageB. Artificially queueing protocol messages guarantees that any
* microtasks for previous message finish before next message is processed.
* This is essential ordering when using promises anywhere along the call path.
*
* For example, take the following, where `chooseAndSendGreeting` returns
* a person name and then emits a greeting event:
*
* ```
* let person: string;
* adapter.onGreeting(() => console.log('hello', person));
* person = await adapter.chooseAndSendGreeting();
* ```
*
* Because the event is dispatched synchronously, it may fire before person
* is assigned if they're processed in the same task. Inserting a task
* boundary avoids this issue.
*/
protected needsTaskBoundaryBetween(messageA: DebugProtocol.ProtocolMessage, messageB: DebugProtocol.ProtocolMessage) {
}
/**
* Reads and dispatches items from the queue until it is empty.
*/
private async processQueue() {
while (this.queue.length) {
if (!message || this.needsTaskBoundaryBetween(this.queue[0], message)) {
await timeout(0);
}
message = this.queue.shift();
if (!message) {
return; // may have been disposed of
}
switch (message.type) {
case 'event':
break;
this.requestCallback?.(<DebugProtocol.Request>message);
break;
const clb = this.pendingRequests.get(response.request_seq);
if (clb) {
this.pendingRequests.delete(response.request_seq);
this.clearPendingRequestTimer(response.request_seq);
clb(response);
}
break;
}
}
}
private internalSend(typ: 'request' | 'response' | 'event', message: DebugProtocol.ProtocolMessage): void {
message.seq = this.sequence++;
this.sendMessage(message);
}
protected async cancelPendingRequests(): Promise<void> {
return Promise.resolve();
}
const pending = new Map<number, (e: DebugProtocol.Response) => void>();
this.pendingRequests.forEach((value, key) => pending.set(key, value));
await timeout(500);
pending.forEach((callback, request_seq) => {
const err: DebugProtocol.Response = {
type: 'response',
seq: 0,
request_seq,
success: false,
command: 'canceled',
message: 'canceled'
};
callback(err);
this.pendingRequests.delete(request_seq);
this.clearPendingRequestTimer(request_seq);
});
private clearPendingRequestTimer(requestSeq: number): void {
this.pendingRequestTimers.delete(requestSeq);
}
getPendingRequestIds(): number[] {
return Array.from(this.pendingRequests.keys());
}
dispose(): void {
clearTimeout(timer);
}
this._onError.dispose();
this._onExit.dispose();
this.queue = [];
}