Callbacks now function

This commit is contained in:
Gabriel DeBacker
2019-01-17 09:29:47 -08:00
parent 8fe69fe73f
commit 54b9bba47e
6 changed files with 102 additions and 52 deletions

View File

@@ -916,7 +916,7 @@ export interface ShellLaunchConfigDto {
export interface ExtHostTerminalServiceShape {
$acceptTerminalClosed(id: number): void;
$acceptTerminalOpened(id: number, name: string): void;
$acceptTerminalOpened(id: number, name: string, isRendererOnly: boolean): void;
$acceptActiveTerminalChanged(id: number | null): void;
$acceptTerminalProcessId(id: number, processId: number): void;
$acceptTerminalProcessData(id: number, data: string): void;

View File

@@ -28,9 +28,10 @@ import {
import { ExtHostVariableResolverService } from 'vs/workbench/api/node/extHostDebugService';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/node/extHostDocumentsAndEditors';
import { ExtHostConfiguration } from 'vs/workbench/api/node/extHostConfiguration';
import { ExtHostTerminalService, ExtHostTerminal } from 'vs/workbench/api/node/extHostTerminalService';
import { ExtHostTerminalService, ExtHostTerminalRenderer } from 'vs/workbench/api/node/extHostTerminalService';
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import { IDisposable } from 'vs/base/common/lifecycle';
namespace TaskDefinitionDTO {
export function from(value: vscode.TaskDefinition): TaskDefinitionDTO {
@@ -332,40 +333,76 @@ interface HandlerData {
extension: IExtensionDescription;
}
class ExtensionCallbackExecutionData {
class ExtensionCallbackExecutionData implements IDisposable {
private _cancellationSource?: CancellationTokenSource;
private _onDidOpenRendererTerminal?: IDisposable;
private _terminalId?: number;
constructor(private readonly callbackData: vscode.ExtensionCallbackExecution, private readonly terminalService: ExtHostTerminalService) {
}
public terminateCallback(): void {
public dispose(): void {
if (this._cancellationSource) {
return undefined;
this._cancellationSource.cancel();
this._cancellationSource.dispose();
}
this._cancellationSource.cancel();
this._cancellationSource.dispose();
this._cancellationSource = undefined;
if (this._onDidOpenRendererTerminal) {
this._onDidOpenRendererTerminal.dispose();
}
}
public startCallback(terminalId: number): Thenable<void> {
private onDidOpenRenderTerminal(terminal: vscode.TerminalRenderer): Thenable<void> {
// If we have already started the extension task callback, then
// do not start it again.
// It is completely valid for multiple terminals to be opened
// before the one for our task.
if (this._cancellationSource) {
return undefined;
}
let foundTerminal: ExtHostTerminal | undefined;
for (let terminal of this.terminalService.terminals) {
if (terminal instanceof ExtHostTerminalRenderer && terminal._id === this._terminalId) {
// Stop listening (if we are) for more terminals
// to be created.
if (this._onDidOpenRendererTerminal) {
this._onDidOpenRendererTerminal.dispose();
this._onDidOpenRendererTerminal = undefined;
}
if (!(terminal instanceof ExtHostTerminalRenderer)) {
throw new Error('Expected a terminal renderer');
}
this._cancellationSource = new CancellationTokenSource();
return this.callbackData.callback(terminal, this._cancellationSource.token).then(() => {
});
}
return undefined;
}
public startCallback(terminalId: number): void {
this._terminalId = terminalId;
// In order to start the task, we need to wait for the extension host
// to know about the new terminal.
// The order in which the events make it to the extension host (currently)
// is "task created" followed by "terminal opened".
// So, we need to wait for that event.
// However, this loop below ensures that if the order of those events
// ever changes, this code continues to function.
// Check to see if the extension host already knows about this terminal.
for (let terminal of this.terminalService.rendererTerminals) {
if (terminal._id === terminalId) {
foundTerminal = terminal;
break;
this.onDidOpenRenderTerminal(terminal);
return;
}
}
if (!foundTerminal) {
throw new Error('Should have a terminal by this point.');
}
this._cancellationSource = new CancellationTokenSource();
return this.callbackData.callback(undefined, this._cancellationSource.token);
// If we get here, then the terminal is unknown to the extension host. Let's wait
// for it to be created and the start our extension callback.
this._onDidOpenRendererTerminal = this.terminalService.onDidOpenRendererTerminal(this.onDidOpenRenderTerminal.bind(this));
}
}
@@ -646,7 +683,7 @@ export class ExtHostTask implements ExtHostTaskShape {
private terminateExtensionCallbackExecution(execution: TaskExecutionDTO): void {
const extensionCallback: ExtensionCallbackExecutionData | undefined = this._activeExtensionCallbacks.get(execution.id);
if (extensionCallback) {
extensionCallback.terminateCallback();
extensionCallback.dispose();
this._activeExtensionCallbacks.delete(execution.id);
}
}

View File

@@ -208,13 +208,16 @@ export class ExtHostTerminalRenderer extends BaseExtHostTerminal implements vsco
constructor(
proxy: MainThreadTerminalServiceShape,
private _name: string,
private _terminal: ExtHostTerminal
private _terminal: ExtHostTerminal,
id?: number
) {
super(proxy);
this._proxy.$createTerminalRenderer(this._name).then(id => {
this._runQueuedRequests(id);
(<any>this._terminal)._runQueuedRequests(id);
});
super(proxy, id);
if (!id) {
this._proxy.$createTerminalRenderer(this._name).then(id => {
this._runQueuedRequests(id);
(<any>this._terminal)._runQueuedRequests(id);
});
}
}
public write(data: string): void {
@@ -245,11 +248,14 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
public get activeTerminal(): ExtHostTerminal { return this._activeTerminal; }
public get terminals(): ExtHostTerminal[] { return this._terminals; }
public get rendererTerminals(): ExtHostTerminalRenderer[] { return this._terminalRenderers; }
private readonly _onDidCloseTerminal: Emitter<vscode.Terminal> = new Emitter<vscode.Terminal>();
public get onDidCloseTerminal(): Event<vscode.Terminal> { return this._onDidCloseTerminal && this._onDidCloseTerminal.event; }
private readonly _onDidOpenTerminal: Emitter<vscode.Terminal> = new Emitter<vscode.Terminal>();
public get onDidOpenTerminal(): Event<vscode.Terminal> { return this._onDidOpenTerminal && this._onDidOpenTerminal.event; }
private readonly _onDidOpenRendererTerminal: Emitter<vscode.TerminalRenderer> = new Emitter<vscode.TerminalRenderer>();
public get onDidOpenRendererTerminal(): Event<vscode.TerminalRenderer> { return this._onDidOpenRendererTerminal && this._onDidOpenRendererTerminal.event; }
private readonly _onDidChangeActiveTerminal: Emitter<vscode.Terminal | undefined> = new Emitter<vscode.Terminal | undefined>();
public get onDidChangeActiveTerminal(): Event<vscode.Terminal | undefined> { return this._onDidChangeActiveTerminal && this._onDidChangeActiveTerminal.event; }
@@ -342,17 +348,39 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
this._onDidCloseTerminal.fire(terminal);
}
public $acceptTerminalOpened(id: number, name: string): void {
public $acceptTerminalOpened(id: number, name: string, isRendererOnly: boolean): void {
// If this is a terminal created by one of the public createrTerminal* APIs
// then @acceptTerminalOpened was called from the main thread task
// to indicate that the terminal is ready for use.
// In those cases, we don't need to create the extension host objects
// as they alrady exist, but, we do still need to fire the events
// to our consumers.
const index = this._getTerminalObjectIndexById(this._terminals, id);
if (index !== null) {
// The terminal has already been created (via createTerminal*), only fire the event
this._onDidOpenTerminal.fire(this.terminals[index]);
}
let renderer = this._getTerminalRendererById(id);
if (renderer) {
// The terminal has already been created (via createTerminal*), only fire the event
this._onDidOpenRendererTerminal.fire(renderer);
}
if (renderer || index) {
return;
}
const renderer = this._getTerminalRendererById(id);
// The extension host did not know about this terminal, so create extension host
// objects to represent them.
const terminal = new ExtHostTerminal(this._proxy, name, id, renderer ? RENDERER_NO_PROCESS_ID : undefined);
this._terminals.push(terminal);
this._onDidOpenTerminal.fire(terminal);
if (isRendererOnly) {
renderer = new ExtHostTerminalRenderer(this._proxy, name, terminal, id);
this.rendererTerminals.push(renderer);
this._onDidOpenRendererTerminal.fire(renderer);
}
}
public $acceptTerminalProcessId(id: number, processId: number): void {

View File

@@ -1622,9 +1622,9 @@ export enum TaskScope {
}
export class ExtensionCallbackExecution implements vscode.ExtensionCallbackExecution {
private _callback: (args: vscode.ExtensionCallbackExecutionArgs, cancellationToken: vscode.CancellationToken) => Thenable<void>;
private _callback: (args: vscode.TerminalRenderer, cancellationToken: vscode.CancellationToken) => Thenable<void>;
constructor(callback: (args: vscode.ExtensionCallbackExecutionArgs, cancellationToken: vscode.CancellationToken) => Thenable<void>) {
constructor(callback: (args: vscode.TerminalRenderer, cancellationToken: vscode.CancellationToken) => Thenable<void>) {
this._callback = callback;
}
@@ -1637,11 +1637,11 @@ export class ExtensionCallbackExecution implements vscode.ExtensionCallbackExecu
return hash.digest('hex');
}
public set callback(value: (args: vscode.ExtensionCallbackExecutionArgs, cancellationToken: vscode.CancellationToken) => Thenable<void>) {
public set callback(value: (args: vscode.TerminalRenderer, cancellationToken: vscode.CancellationToken) => Thenable<void>) {
this._callback = value;
}
public get callback(): (args: vscode.ExtensionCallbackExecutionArgs, cancellationToken: vscode.CancellationToken) => Thenable<void> {
public get callback(): (args: vscode.TerminalRenderer, cancellationToken: vscode.CancellationToken) => Thenable<void> {
return this._callback;
}
}