Skip to content

Commit

Permalink
use built-in queue(), pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Dec 11, 2019
1 parent 04ebcec commit bacbfd2
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions src/vs/workbench/contrib/debug/common/abstractDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Emitter, Event } from 'vs/base/common/event';
import { IDebugAdapter } from 'vs/workbench/contrib/debug/common/debug';
import { timeout } from 'vs/base/common/async';
import { timeout, Queue } from 'vs/base/common/async';

/**
* Abstract implementation of the low level API for a debug adapter.
Expand All @@ -18,7 +18,7 @@ export abstract class AbstractDebugAdapter implements IDebugAdapter {
private requestCallback: ((request: DebugProtocol.Request) => void) | undefined;
private eventCallback: ((request: DebugProtocol.Event) => void) | undefined;
private messageCallback: ((message: DebugProtocol.ProtocolMessage) => void) | undefined;
private readonly queue: DebugProtocol.ProtocolMessage[] = [];
private readonly queue = new Queue();
protected readonly _onError = new Emitter<Error>();
protected readonly _onExit = new Emitter<number | null>();

Expand Down Expand Up @@ -109,41 +109,33 @@ export abstract class AbstractDebugAdapter implements IDebugAdapter {
this.messageCallback(message);
}
else {
// Artificially queueing protocol messages guarantees that any microtasks for
// previous message finish before next message is processed. This is essential
// to guarantee ordering when using promises anywhere along the call path.
this.queue.push(message);
if (this.queue.length === 1) {
setTimeout(() => this.processQueue(), 0);
}
}
}

private processQueue(): void {
const message = this.queue!.shift()!;
switch (message.type) {
case 'event':
if (this.eventCallback) {
this.eventCallback(<DebugProtocol.Event>message);
}
break;
case 'request':
if (this.requestCallback) {
this.requestCallback(<DebugProtocol.Request>message);
this.queue.queue(() => {
switch (message.type) {
case 'event':
if (this.eventCallback) {
this.eventCallback(<DebugProtocol.Event>message);
}
break;
case 'request':
if (this.requestCallback) {
this.requestCallback(<DebugProtocol.Request>message);
}
break;
case 'response':
const response = <DebugProtocol.Response>message;
const clb = this.pendingRequests.get(response.request_seq);
if (clb) {
this.pendingRequests.delete(response.request_seq);
clb(response);
}
break;
}
break;
case 'response':
const response = <DebugProtocol.Response>message;
const clb = this.pendingRequests.get(response.request_seq);
if (clb) {
this.pendingRequests.delete(response.request_seq);
clb(response);
}
break;
}

if (this.queue.length) {
setTimeout(() => this.processQueue(), 0);
// Artificially queueing protocol messages guarantees that any microtasks for
// previous message finish before next message is processed. This is essential
// to guarantee ordering when using promises anywhere along the call path.
return timeout(0);
});
}
}

Expand Down Expand Up @@ -180,6 +172,6 @@ export abstract class AbstractDebugAdapter implements IDebugAdapter {
}

dispose(): void {
// noop
this.queue.dispose();
}
}

0 comments on commit bacbfd2

Please sign in to comment.