-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit provides an external API that can be used by other extensions to communicate with the base trace extension. The current API provides a way to retrieve the active experiment as well as fire events and register callbacks. This is the initial API and will be further expanded in future commits. Fixes #140 Signed-off-by: Neel Gondalia <[email protected]>
- Loading branch information
Showing
8 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/*************************************************************************************** | ||
* Copyright (c) 2023 BlackBerry Limited and others. | ||
* | ||
* Licensed under the MIT license. See LICENSE file in the project root for details. | ||
***************************************************************************************/ | ||
import { Experiment } from 'tsp-typescript-client/lib/models/experiment'; | ||
import { TraceViewerPanel } from '../trace-viewer-panel/trace-viewer-webview-panel'; | ||
import * as vscode from 'vscode'; | ||
import { traceExtensionWebviewManager } from '../extension'; | ||
|
||
export interface ExternalAPI { | ||
getActiveExperiment(): Experiment | undefined, | ||
getActiveWebviewPanels(): { [key: string]: TraceViewerPanel | undefined; }, | ||
getActiveWebviews(): vscode.WebviewView[], | ||
onWebviewCreated(listener: (data: vscode.WebviewView) => void): void, | ||
onWebviewPanelCreated(listener: (data: vscode.WebviewPanel) => void): void, | ||
} | ||
|
||
export const traceExtensionAPI: ExternalAPI = { | ||
/** | ||
* Retrieves the currently active experiment | ||
* | ||
* @returns Experiment if one is currently active, otherwise undefined | ||
*/ | ||
getActiveExperiment(): Experiment | undefined { | ||
return TraceViewerPanel.getCurrentExperiment(); | ||
}, | ||
|
||
/** | ||
* Retrieves active trace panels | ||
* | ||
* @returns Key value pairs where the value is of TraceViewerPanel type if panel with that key is active, otherwise undefined | ||
*/ | ||
getActiveWebviewPanels(): { [key: string]: TraceViewerPanel | undefined; } { | ||
return TraceViewerPanel.activePanels; | ||
}, | ||
|
||
/** | ||
* Retrieves active webviews | ||
* | ||
* @returns List of active webviews | ||
*/ | ||
getActiveWebviews(): vscode.WebviewView[] { | ||
return traceExtensionWebviewManager.getAllActiveWebviews(); | ||
}, | ||
|
||
/** | ||
* Registers an event listener for onWebviewCreated event | ||
* | ||
* @param listener event listener | ||
*/ | ||
onWebviewCreated(listener: (data: vscode.WebviewView) => void): void { | ||
traceExtensionWebviewManager.onWebviewCreated(listener); | ||
}, | ||
|
||
/** | ||
* Registers an event listener for onWebviewPanelCreated event | ||
* | ||
* @param listener event listener | ||
*/ | ||
onWebviewPanelCreated(listener: (data: vscode.WebviewPanel) => void): void { | ||
traceExtensionWebviewManager.onWebviewPanelCreated(listener); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
vscode-trace-extension/src/utils/trace-extension-webview-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/*************************************************************************************** | ||
* Copyright (c) 2023 BlackBerry Limited and others. | ||
* | ||
* Licensed under the MIT license. See LICENSE file in the project root for details. | ||
***************************************************************************************/ | ||
import * as vscode from 'vscode'; | ||
|
||
/** | ||
* Manages webview panels and webviews creation events | ||
*/ | ||
export class TraceExtensionWebviewManager { | ||
private webviews: vscode.WebviewView[] = []; | ||
private webviewCreated: vscode.EventEmitter<vscode.WebviewView> = new vscode.EventEmitter(); | ||
private webviewPanelCreated: vscode.EventEmitter<vscode.WebviewPanel> = new vscode.EventEmitter(); | ||
private isManagerDisposed = false; | ||
|
||
getAllActiveWebviews(): vscode.WebviewView[] { | ||
if (!this.isDisposed()) { | ||
return this.webviews; | ||
} | ||
return []; | ||
} | ||
|
||
fireWebviewCreated(_webview: vscode.WebviewView): void { | ||
if (!this.isDisposed()) { | ||
this.addWebview(_webview); | ||
this.webviewCreated.fire(_webview); | ||
} | ||
} | ||
|
||
fireWebviewPanelCreated(_webviewPanel: vscode.WebviewPanel): void { | ||
if (!this.isDisposed()) { | ||
this.webviewPanelCreated.fire(_webviewPanel); | ||
} | ||
} | ||
|
||
onWebviewCreated(listener: (data: vscode.WebviewView) => unknown): void { | ||
if (!this.isDisposed()) { | ||
this.webviewCreated.event(listener); | ||
} | ||
} | ||
|
||
onWebviewPanelCreated(listener: (data: vscode.WebviewPanel) => unknown): void { | ||
if (!this.isDisposed()) { | ||
this.webviewPanelCreated.event(listener); | ||
} | ||
} | ||
|
||
dispose(): void { | ||
if (!this.isDisposed()) { | ||
this.webviews = []; | ||
this.webviewCreated.dispose(); | ||
this.webviewPanelCreated.dispose(); | ||
this.isManagerDisposed = true; | ||
} | ||
} | ||
|
||
isDisposed(): boolean { | ||
return this.isManagerDisposed; | ||
} | ||
|
||
private addWebview(webview: vscode.WebviewView): void { | ||
// Remove it from the array when the webview disposes | ||
webview.onDidDispose(() => { | ||
this.removeWebview(webview); | ||
}); | ||
this.webviews.push(webview); | ||
} | ||
|
||
private removeWebview(_webview: vscode.WebviewView): void { | ||
this.webviews.filter(webview => webview === _webview).forEach(webview => { | ||
const index = this.webviews.indexOf(webview); | ||
if (index !== -1) { | ||
this.webviews.splice(index, 1); | ||
} | ||
}); | ||
} | ||
} |