Have terminal service clean up instances using event

This commit is contained in:
Daniel Imms 2016-09-15 12:26:14 -07:00
parent 5094cf079e
commit 2e02b52deb
4 changed files with 13 additions and 16 deletions

View file

@ -26,7 +26,7 @@ export class MainThreadTerminalService extends MainThreadTerminalServiceShape {
super(); super();
this._proxy = threadService.get(ExtHostContext.ExtHostTerminalService); this._proxy = threadService.get(ExtHostContext.ExtHostTerminalService);
this._toDispose = []; this._toDispose = [];
this._toDispose.push(terminalService.onInstanceClosed((terminalInstance) => this._onTerminalClosed(terminalInstance))); this._toDispose.push(terminalService.onInstanceDisposed((terminalInstance) => this._onTerminalDisposed(terminalInstance)));
} }
public dispose(): void { public dispose(): void {
@ -65,7 +65,7 @@ export class MainThreadTerminalService extends MainThreadTerminalServiceShape {
} }
} }
private _onTerminalClosed(terminalInstance: ITerminalInstance): void { private _onTerminalDisposed(terminalInstance: ITerminalInstance): void {
this._proxy.$acceptTerminalClosed(terminalInstance.id); this._proxy.$acceptTerminalClosed(terminalInstance.id);
} }
} }

View file

@ -58,7 +58,7 @@ export interface ITerminalService {
activeTerminalInstanceIndex: number; activeTerminalInstanceIndex: number;
configHelper: TerminalConfigHelper; configHelper: TerminalConfigHelper;
onActiveInstanceChanged: Event<string>; onActiveInstanceChanged: Event<string>;
onInstanceClosed: Event<ITerminalInstance>; onInstanceDisposed: Event<ITerminalInstance>;
onInstancesChanged: Event<string>; onInstancesChanged: Event<string>;
onInstanceTitleChanged: Event<string>; onInstanceTitleChanged: Event<string>;
terminalInstances: ITerminalInstance[]; terminalInstances: ITerminalInstance[];

View file

@ -31,11 +31,11 @@ export class TerminalInstance implements ITerminalInstance {
private _id: number; private _id: number;
private _title: string; private _title: string;
private _onClosed: Emitter<TerminalInstance>; private _onDisposed: Emitter<TerminalInstance>;
private _onTitleChanged: Emitter<string>; private _onTitleChanged: Emitter<string>;
public get id(): number { return this._id; } public get id(): number { return this._id; }
public get title(): string { return this._title; } public get title(): string { return this._title; }
public get onClosed(): Event<TerminalInstance> { return this._onClosed.event; } public get onClosed(): Event<TerminalInstance> { return this._onDisposed.event; }
public get onTitleChanged(): Event<string> { return this._onTitleChanged.event; } public get onTitleChanged(): Event<string> { return this._onTitleChanged.event; }
private isExiting: boolean = false; private isExiting: boolean = false;
@ -49,7 +49,6 @@ export class TerminalInstance implements ITerminalInstance {
public constructor( public constructor(
private terminalFocusContextKey: IContextKey<boolean>, private terminalFocusContextKey: IContextKey<boolean>,
private onExitCallback: (TerminalInstance) => void,
private configHelper: TerminalConfigHelper, private configHelper: TerminalConfigHelper,
private container: HTMLElement, private container: HTMLElement,
private workspace: IWorkspace, private workspace: IWorkspace,
@ -60,7 +59,7 @@ export class TerminalInstance implements ITerminalInstance {
) { ) {
this._id = TerminalInstance.ID_COUNTER++; this._id = TerminalInstance.ID_COUNTER++;
this._onTitleChanged = new Emitter<string>(); this._onTitleChanged = new Emitter<string>();
this._onClosed = new Emitter<TerminalInstance>(); this._onDisposed = new Emitter<TerminalInstance>();
this.createProcess(workspace, name, shell); this.createProcess(workspace, name, shell);
if (container) { if (container) {
@ -170,10 +169,8 @@ export class TerminalInstance implements ITerminalInstance {
} }
this.process = null; this.process = null;
} }
this._onClosed.fire(this); this._onDisposed.fire(this);
this.toDispose = lifecycle.dispose(this.toDispose); this.toDispose = lifecycle.dispose(this.toDispose);
// TODO: Move exit callback to listen onClose event
this.onExitCallback(this);
} }
public focus(force?: boolean): void { public focus(force?: boolean): void {

View file

@ -23,14 +23,14 @@ export class TerminalService implements ITerminalService {
private _activeTerminalInstanceIndex: number = 0; private _activeTerminalInstanceIndex: number = 0;
private _configHelper: TerminalConfigHelper; private _configHelper: TerminalConfigHelper;
private _onActiveInstanceChanged: Emitter<string>; private _onActiveInstanceChanged: Emitter<string>;
private _onInstanceClosed: Emitter<ITerminalInstance>; private _onInstanceDisposed: Emitter<ITerminalInstance>;
private _onInstanceTitleChanged: Emitter<string>; private _onInstanceTitleChanged: Emitter<string>;
private _onInstancesChanged: Emitter<string>; private _onInstancesChanged: Emitter<string>;
private _terminalInstances: ITerminalInstance[] = []; private _terminalInstances: ITerminalInstance[] = [];
public get activeTerminalInstanceIndex(): number { return this._activeTerminalInstanceIndex; } public get activeTerminalInstanceIndex(): number { return this._activeTerminalInstanceIndex; }
public get configHelper(): TerminalConfigHelper { return this._configHelper; } public get configHelper(): TerminalConfigHelper { return this._configHelper; }
public get onActiveInstanceChanged(): Event<string> { return this._onActiveInstanceChanged.event; } public get onActiveInstanceChanged(): Event<string> { return this._onActiveInstanceChanged.event; }
public get onInstanceClosed(): Event<ITerminalInstance> { return this._onInstanceClosed.event; } public get onInstanceDisposed(): Event<ITerminalInstance> { return this._onInstanceDisposed.event; }
public get onInstanceTitleChanged(): Event<string> { return this._onInstanceTitleChanged.event; } public get onInstanceTitleChanged(): Event<string> { return this._onInstanceTitleChanged.event; }
public get onInstancesChanged(): Event<string> { return this._onInstancesChanged.event; } public get onInstancesChanged(): Event<string> { return this._onInstancesChanged.event; }
public get terminalInstances(): ITerminalInstance[] { return this._terminalInstances; } public get terminalInstances(): ITerminalInstance[] { return this._terminalInstances; }
@ -47,11 +47,12 @@ export class TerminalService implements ITerminalService {
@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService @IWorkspaceContextService private workspaceContextService: IWorkspaceContextService
) { ) {
this._onActiveInstanceChanged = new Emitter<string>(); this._onActiveInstanceChanged = new Emitter<string>();
this._onInstanceClosed = new Emitter<ITerminalInstance>(); this._onInstanceDisposed = new Emitter<ITerminalInstance>();
this._onInstancesChanged = new Emitter<string>(); this._onInstancesChanged = new Emitter<string>();
this._onInstanceTitleChanged = new Emitter<string>(); this._onInstanceTitleChanged = new Emitter<string>();
this.terminalFocusContextKey = KEYBINDING_CONTEXT_TERMINAL_FOCUS.bindTo(this.contextKeyService); this.terminalFocusContextKey = KEYBINDING_CONTEXT_TERMINAL_FOCUS.bindTo(this.contextKeyService);
this._configHelper = <TerminalConfigHelper>this.instantiationService.createInstance(TerminalConfigHelper, platform.platform); this._configHelper = <TerminalConfigHelper>this.instantiationService.createInstance(TerminalConfigHelper, platform.platform);
this.onInstanceDisposed((terminalInstance) => { this.removeInstance(terminalInstance); });
} }
public createInstance(name?: string, shellPath?: string, shellArgs?: string[]): ITerminalInstance { public createInstance(name?: string, shellPath?: string, shellArgs?: string[]): ITerminalInstance {
@ -61,14 +62,13 @@ export class TerminalService implements ITerminalService {
}; };
let terminalInstance = <TerminalInstance>this.instantiationService.createInstance(TerminalInstance, let terminalInstance = <TerminalInstance>this.instantiationService.createInstance(TerminalInstance,
this.terminalFocusContextKey, this.terminalFocusContextKey,
this.onTerminalInstanceDispose.bind(this),
this._configHelper, this._configHelper,
this.terminalContainer, this.terminalContainer,
this.workspaceContextService.getWorkspace(), this.workspaceContextService.getWorkspace(),
name, name,
shell); shell);
terminalInstance.addDisposable(terminalInstance.onTitleChanged(this._onInstanceTitleChanged.fire, this._onInstanceTitleChanged)); terminalInstance.addDisposable(terminalInstance.onTitleChanged(this._onInstanceTitleChanged.fire, this._onInstanceTitleChanged));
terminalInstance.addDisposable(terminalInstance.onClosed(this._onInstanceClosed.fire, this._onInstanceClosed)); terminalInstance.addDisposable(terminalInstance.onClosed(this._onInstanceDisposed.fire, this._onInstanceDisposed));
this.terminalInstances.push(terminalInstance); this.terminalInstances.push(terminalInstance);
if (this.terminalInstances.length === 1) { if (this.terminalInstances.length === 1) {
// It's the first instance so it should be made active automatically // It's the first instance so it should be made active automatically
@ -82,7 +82,7 @@ export class TerminalService implements ITerminalService {
return this._terminalInstances.map((instance, index) => `${index + 1}: ${instance.title}`); return this._terminalInstances.map((instance, index) => `${index + 1}: ${instance.title}`);
} }
private onTerminalInstanceDispose(terminalInstance: TerminalInstance): void { private removeInstance(terminalInstance: ITerminalInstance): void {
let index = this.terminalInstances.indexOf(terminalInstance); let index = this.terminalInstances.indexOf(terminalInstance);
let wasActiveInstance = terminalInstance === this.getActiveInstance(); let wasActiveInstance = terminalInstance === this.getActiveInstance();
if (index !== -1) { if (index !== -1) {