Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect webview messages async #4405

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extension/src/experiments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class Experiments extends BaseRepository<TableData> {
public toggleColumnStatus(path: string) {
const status = this.columns.toggleStatus(path)

this.notifyColumnsChanged()
void this.notifyColumnsChanged()

return status
}
Expand Down Expand Up @@ -605,7 +605,7 @@ export class Experiments extends BaseRepository<TableData> {
this.experiments.getErrors()
)
this.experimentsChanged.fire()
this.notifyColumnsChanged()
void this.notifyColumnsChanged()
}

private notifyColumnsChanged() {
Expand Down
2 changes: 1 addition & 1 deletion extension/src/experiments/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {

type StarredExperiments = Record<string, boolean | undefined>

export type SelectedExperimentWithColor = Experiment & {
type SelectedExperimentWithColor = Experiment & {
displayColor: Color
selected: true
}
Expand Down
98 changes: 68 additions & 30 deletions extension/src/experiments/webview/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ export class WebviewMessages {
this.update = update
}

public sendWebviewMessage() {
public async sendWebviewMessage() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identical blocks of code found in 2 locations. Consider refactoring.

const webview = this.getWebview()
void webview?.show(this.getWebviewData())
if (!webview) {
return
}
const data = await this.getWebviewData()
return webview.show(data)
}

public handleMessageFromWebview(message: MessageFromWebview) {
Expand Down Expand Up @@ -264,46 +268,80 @@ export class WebviewMessages {

private toggleShowOnlyChanged() {
this.columns.toggleShowOnlyChanged()
this.sendWebviewMessage()
sendTelemetryEvent(
EventName.VIEWS_EXPERIMENTS_TABLE_REFRESH,
undefined,
undefined
)
return Promise.all([
this.sendWebviewMessage(),
sendTelemetryEvent(
EventName.VIEWS_EXPERIMENTS_TABLE_REFRESH,
undefined,
undefined
)
])
}

private getWebviewData(): TableData {
const filters = this.experiments.getFilters()
const rows = this.experiments.getRowData()
const selectedColumns = this.columns.getSelected()

const showOnlyChanged = this.columns.getShowOnlyChanged()
private async getWebviewData(): Promise<TableData> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getWebviewData has 62 lines of code (exceeds 40 allowed). Consider refactoring.

const [
changes,
cliError,
columnOrder,
columnWidths,
selectedColumns,
filters,
hasBranchesToSelect,
hasCheckpoints,
hasColumns,
hasConfig,
hasMoreCommits,
hasRunningWorkspaceExperiment,
isShowingMoreCommits,
rows,
selectedBranches,
selectedForPlotsCount,
showOnlyChanged,
sorts
] = await Promise.all([
this.columns.getChanges(),
this.experiments.getCliError() || null,
this.columns.getColumnOrder(),
this.columns.getColumnWidths(),
this.columns.getSelected(),
this.experiments.getFilters(),
this.experiments.getAvailableBranchesToShow().length > 0,
this.experiments.hasCheckpoints(),
this.columns.hasNonDefaultColumns(),
this.pipeline.hasPipeline(),
this.experiments.getHasMoreCommits(),
this.experiments.hasRunningWorkspaceExperiment(),
this.experiments.getIsShowingMoreCommits(),
this.experiments.getRowData(),
this.experiments.getSelectedBranches(),
this.experiments.getSelectedRevisions().length,
this.columns.getShowOnlyChanged(),
this.experiments.getSorts()
])

const columns = showOnlyChanged
? collectColumnsWithChangedValues(selectedColumns, rows, filters)
: selectedColumns

return {
changes: this.columns.getChanges(),
cliError: this.experiments.getCliError() || null,
columnOrder: this.columns.getColumnOrder(),
columnWidths: this.columns.getColumnWidths(),
changes,
cliError,
columnOrder,
columnWidths,
columns,
filters: filters.map(({ path }) => path),
hasBranchesToSelect:
this.experiments.getAvailableBranchesToShow().length > 0,
hasCheckpoints: this.experiments.hasCheckpoints(),
hasColumns: this.columns.hasNonDefaultColumns(),
hasConfig: this.pipeline.hasPipeline(),
hasMoreCommits: this.experiments.getHasMoreCommits(),
hasRunningWorkspaceExperiment:
this.experiments.hasRunningWorkspaceExperiment(),
isShowingMoreCommits: this.experiments.getIsShowingMoreCommits(),
hasBranchesToSelect,
hasCheckpoints,
hasColumns,
hasConfig,
hasMoreCommits,
hasRunningWorkspaceExperiment,
isShowingMoreCommits,
rows,
selectedBranches: this.experiments.getSelectedBranches(),
selectedForPlotsCount: this.experiments.getSelectedRevisions().length,
selectedBranches,
selectedForPlotsCount,
showOnlyChanged,
sorts: this.experiments.getSorts()
sorts
}
}

Expand Down
51 changes: 39 additions & 12 deletions extension/src/plots/webview/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,12 @@ export class WebviewMessages {
}

public async sendWebviewMessage() {
const selectedRevisions = this.plots.getSelectedRevisionDetails()

await this.getWebview()?.show({
cliError: this.errors.getCliError()?.error || null,
comparison: this.getComparisonPlots(),
custom: this.getCustomPlots(),
hasPlots: !!this.paths.hasPaths(),
hasUnselectedPlots: this.paths.getHasUnselectedPlots(),
sectionCollapsed: this.plots.getSectionCollapsed(),
selectedRevisions,
template: this.getTemplatePlots(selectedRevisions)
})
const webview = this.getWebview()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[F] although this function is duplicated it is not worth the hassle that a base class would bring

if (!webview) {
return
}
const data = await this.getWebviewData()
return webview.show(data)
}

public handleMessageFromWebview(message: MessageFromWebview) {
Expand Down Expand Up @@ -140,6 +134,39 @@ export class WebviewMessages {
}
}

private async getWebviewData(): Promise<TPlotsData> {
const selectedRevisions = this.plots.getSelectedRevisionDetails()

const [
cliError,
comparison,
custom,
hasPlots,
hasUnselectedPlots,
sectionCollapsed,
template
] = await Promise.all([
this.errors.getCliError()?.error || null,
this.getComparisonPlots(),
this.getCustomPlots(),
!!this.paths.hasPaths(),
this.paths.getHasUnselectedPlots(),
this.plots.getSectionCollapsed(),
this.getTemplatePlots(selectedRevisions)
])

return {
cliError,
comparison,
custom,
hasPlots,
hasUnselectedPlots,
sectionCollapsed,
selectedRevisions,
template
}
}

private setPlotSize(
section: PlotsSection,
nbItemsPerRow: number,
Expand Down
Loading