Skip to content

Commit

Permalink
Add method to set server status in TraceServerStatusService
Browse files Browse the repository at this point in the history
This method set the status bar and the serverUp context that is used
to decide whether the trace explorer or the welcome view should
be rendered.

This method doesn't check the backend to get status in comparison to
checkAndUpdateServerStatus. It's useful when it's known that the
server is down or up (e.g. after start notification). In this case, it
is not necessary to check the backend again.

Don't set the status in the method isTraceServerUp() because it should
not have side-effects that are not expected.

Initialize and refresh flags traceViewer.serverUp and
trace-explorer.noExperiments to ensure the correct view is rendered,
i.e. Trace Explorer or Welcome view, when certain event happen (e.g.
server started or stopped).

Fixes #227
Fixes #228

Signed-off-by: Bernd Hufmann <[email protected]>
  • Loading branch information
bhufmann committed Apr 10, 2024
1 parent ab0fa48 commit 86ade94
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
25 changes: 17 additions & 8 deletions vscode-trace-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
await startTraceServerIfAvailable(file.fsPath);
if (await isTraceServerUp()) {
fileOpenHandler(context, file);
vscode.commands.executeCommand('setContext', 'trace-explorer.noExperiments', false);
vscode.commands.executeCommand('trace-explorer.refreshContext');
}
})
);
Expand Down Expand Up @@ -181,6 +181,7 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
await startTraceServerIfAvailable(traceUri.fsPath);
if (await isTraceServerUp()) {
fileOpenHandler(context, traceUri);
serverStatusService.updateServerStatus(true);
vscode.commands.executeCommand('setContext', 'trace-explorer.noExperiments', false);
}
})
Expand All @@ -193,24 +194,28 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
);

context.subscriptions.push(
vscode.commands.registerCommand('serverStatus.started', () => {
serverStatusService.checkAndUpdateServerStatus();
vscode.commands.registerCommand('serverStatus.started', async () => {
await serverStatusService.updateServerStatus(true);
if (tracesProvider) {
// Trigger webview refresh
tracesProvider.postMessagetoWebview(VSCODE_MESSAGES.TRACE_SERVER_STARTED, undefined);
}
// Refresh so that either trace explorer or welcome page is rendered
updateNoExperimentsContext();
})
);

context.subscriptions.push(
vscode.commands.registerCommand('serverStatus.stopped', () => {
serverStatusService.checkAndUpdateServerStatus();
vscode.commands.registerCommand('serverStatus.stopped', async () => {
await serverStatusService.updateServerStatus(false);
})
);

context.subscriptions.push(
vscode.commands.registerCommand('trace-explorer.refreshContext', async () => {
// Refresh so that either trace explorer or welcome page is rendered
const isUp = await isTraceServerUp();
vscode.commands.executeCommand('setContext', 'traceViewer.serverUp', isUp);
await serverStatusService.updateServerStatus(isUp);
if (isUp) {
await updateNoExperimentsContext();
}
Expand All @@ -219,9 +224,13 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {

vscode.commands.executeCommand('setContext', 'traceViewer.markerSetsPresent', false);
vscode.commands.executeCommand('setContext', 'traceViewer.markerCategoriesPresent', false);
vscode.commands.executeCommand('setContext', 'trace-explorer.noExperiments', true);

// Initialize noExperiments/serverUp in a way so that trace explorer webviews are initialized
vscode.commands.executeCommand('setContext', 'trace-explorer.noExperiments', false);
vscode.commands.executeCommand('setContext', 'traceViewer.serverUp', true);

// Refresh to trigger rendering trace explorer or welcome page
vscode.commands.executeCommand('trace-explorer.refreshContext');
serverStatusService.checkAndUpdateServerStatus();
return traceExtensionAPI;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class TraceExplorerAvailableViewsProvider extends AbstractTraceExplorerPr
const data: any = message.data;
switch (command) {
case VSCODE_MESSAGES.CONNECTION_STATUS:
if (data && data.status) {
if (data?.status) {
const status: boolean = JSON.parse(message.data.status);
this._statusService.updateServerStatus(status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class TraceExplorerOpenedTracesViewProvider extends AbstractTraceExplorer
const data: any = message.data;
switch (command) {
case VSCODE_MESSAGES.CONNECTION_STATUS:
if (data && data.status) {
if (data?.status) {
const status: boolean = JSON.parse(message.data.status);
this._statusService.updateServerStatus(status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ export class TraceViewerPanel {
return;
case VSCODE_MESSAGES.CONNECTION_STATUS:
if (message.data?.status && this._statusService) {
this._statusService.checkAndUpdateServerStatus();
const status: boolean = JSON.parse(message.data.status);
this._statusService.updateServerStatus(status);
}
return;
case VSCODE_MESSAGES.SHOW_MARKER_CATEGORIES:
Expand Down
8 changes: 7 additions & 1 deletion vscode-trace-extension/src/utils/trace-server-status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ThemeColor, StatusBarItem } from 'vscode';
import { isTraceServerUp } from './backend-tsp-client-provider';
import * as vscode from 'vscode';

export class TraceServerConnectionStatusService {
private statusBarItem: StatusBarItem;
Expand All @@ -11,9 +12,14 @@ export class TraceServerConnectionStatusService {

public checkAndUpdateServerStatus = async (): Promise<void> => {
const isUp = await isTraceServerUp();
this.render(isUp);
await this.updateServerStatus(isUp);
};

public async updateServerStatus(status: boolean): Promise<void> {
await vscode.commands.executeCommand('setContext', 'traceViewer.serverUp', status);
this.render(status);
}

private render = (status: boolean): void => {
if (status) {
this.statusBarItem.backgroundColor = new ThemeColor('statusBarItem.warningBackground');
Expand Down

0 comments on commit 86ade94

Please sign in to comment.