Skip to content

Commit

Permalink
Clean up debug session lifecycle (#10333)
Browse files Browse the repository at this point in the history
Clean up debug session lifecycle

Signed-off-by: Thomas Mäder <[email protected]>
  • Loading branch information
tsmaeder authored Nov 26, 2021
1 parent 4cedd3f commit 99a56a3
Show file tree
Hide file tree
Showing 32 changed files with 547 additions and 735 deletions.
8 changes: 5 additions & 3 deletions packages/console/src/browser/console-session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ export class ConsoleSessionManager implements Disposable {
delete(id: string): void {
const session = this.sessions.get(id);
if (this.sessions.delete(id) && session) {
this.sessionDeletedEmitter.fire(session);
if (this.sessions.size === 0) {
this.selectedSessionChangedEmitter.fire(undefined);
if (this.selectedSession === session) {
// select a new sessions or undefined if none are left
this.selectedSession = this.sessions.values().next().value;
}
session.dispose();
this.sessionDeletedEmitter.fire(session);
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/console/src/browser/console-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ export class ConsoleWidget extends BaseWidget implements StatefulWidget {

this.session = this.sessionManager.selectedSession;
this.toDispose.push(this.sessionManager.onDidChangeSelectedSession(session => {
// Don't delete the last session by overriding it with 'undefined'
if (session) {
this.session = session;
}
this.session = session;
}));

this.updateFont();
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/common/promise-util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/********************************************************************************
* Copyright (C) 2021 Red Hat and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import * as assert from 'assert';
import { waitForEvent } from './promise-util';
import { Emitter } from './event';

describe('promise-util', () => {
it('should time out', async () => {
const emitter = new Emitter<string>();
try {
await waitForEvent(emitter.event, 1000);
assert.fail('did not time out');
} catch (e) {
// OK
}
});

describe('promise-util', () => {
it('should get event', async () => {
const emitter = new Emitter<string>();
setTimeout(() => {
emitter.fire('abcd');
}, 500);
assert.strictEqual(await waitForEvent(emitter.event, 1000), 'abcd');
});
});

});
21 changes: 20 additions & 1 deletion packages/core/src/common/promise-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { CancellationToken, cancelled } from './cancellation';
import { Disposable } from './disposable';
import { Event } from './event';
import { CancellationToken, CancellationError, cancelled } from './cancellation';

/**
* Simple implementation of the deferred pattern.
Expand Down Expand Up @@ -90,3 +92,20 @@ export function delay<T>(ms: number): (value: T) => Promise<T> {
export async function wait(ms: number): Promise<void> {
await delay(ms)(undefined);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function waitForEvent<T>(event: Event<T>, ms: number, thisArg?: any, disposables?: Disposable[]): Promise<T> {
return new Promise<T>((resolve, reject) => {
const registration = setTimeout(() => {
listener.dispose();
reject(new CancellationError());
}, ms);

const listener = event((evt: T) => {
clearTimeout(registration);
listener.dispose();
resolve(evt);
}, thisArg, disposables);

});
}
4 changes: 2 additions & 2 deletions packages/debug/src/browser/console/debug-console-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ export class DebugConsoleSession extends ConsoleSession {
this.completionKinds.set('file', monaco.languages.CompletionItemKind.File);
this.completionKinds.set('reference', monaco.languages.CompletionItemKind.Reference);
this.completionKinds.set('customcolor', monaco.languages.CompletionItemKind.Color);
monaco.languages.registerCompletionItemProvider({
this.toDispose.push(monaco.languages.registerCompletionItemProvider({
scheme: DebugConsoleSession.uri.scheme,
hasAccessToAllModels: true
}, {
triggerCharacters: ['.'],
provideCompletionItems: (model, position) => this.completions(model, position),
});
}));
}

getElements(): IterableIterator<ConsoleItem> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,11 @@ export class DebugFrontendApplicationContribution extends AbstractViewContributi
execute: (config?: DebugSessionOptions) => this.start(true, config)
});
registry.registerCommand(DebugCommands.STOP, {
execute: () => this.manager.terminateSessions(),
execute: () => this.manager.terminateSession(),
isEnabled: () => this.manager.state !== DebugState.Inactive
});
registry.registerCommand(DebugCommands.RESTART, {
execute: () => this.manager.restartSessions(),
execute: () => this.manager.restartSession(),
isEnabled: () => this.manager.state !== DebugState.Inactive
});

Expand Down Expand Up @@ -722,12 +722,12 @@ export class DebugFrontendApplicationContribution extends AbstractViewContributi
});

registry.registerCommand(DebugSessionContextCommands.STOP, {
execute: () => this.selectedSession && this.selectedSession.terminate(),
execute: () => this.selectedSession && this.manager.terminateSession(this.selectedSession),
isEnabled: () => !!this.selectedSession && this.selectedSession.state !== DebugState.Inactive,
isVisible: () => !this.selectedThread
});
registry.registerCommand(DebugSessionContextCommands.RESTART, {
execute: () => this.selectedSession && this.manager.restart(this.selectedSession),
execute: () => this.selectedSession && this.manager.restartSession(this.selectedSession),
isEnabled: () => !!this.selectedSession && this.selectedSession.state !== DebugState.Inactive,
isVisible: () => !this.selectedThread
});
Expand Down
Loading

0 comments on commit 99a56a3

Please sign in to comment.