Skip to content

Commit 6ae9783

Browse files
Show progress during context switch (#1969) (#1979)
* Show progress during context switch * Address review feedback * move the cache clearing logic to useDockerContext * Apply suggestions from code review Co-authored-by: Brandon Waterloo [MSFT] <36966225+bwateratmsft@users.noreply.github.com> * remove unnecessary await Co-authored-by: Brandon Waterloo [MSFT] <36966225+bwateratmsft@users.noreply.github.com> Co-authored-by: Brandon Waterloo [MSFT] <36966225+bwateratmsft@users.noreply.github.com>
1 parent bf079be commit 6ae9783

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/commands/context/useDockerContext.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IActionContext } from 'vscode-azureextensionui';
88
import { ext } from '../../extensionVariables';
99
import { localize } from '../../localize';
1010
import { ContextTreeItem } from '../../tree/contexts/ContextTreeItem';
11+
import { LocalRootTreeItemBase } from '../../tree/LocalRootTreeItemBase';
1112

1213
export async function useDockerContext(actionContext: IActionContext, node?: ContextTreeItem): Promise<void> {
1314
let invokedFromCommandPalette = false;
@@ -19,7 +20,20 @@ export async function useDockerContext(actionContext: IActionContext, node?: Con
1920
invokedFromCommandPalette = true;
2021
}
2122

22-
await node.use(actionContext);
23+
try {
24+
LocalRootTreeItemBase.autoRefreshViews = false;
25+
await node.runWithTemporaryDescription(
26+
localize('vscode-docker.tree.context.switching', 'Switching...'),
27+
async () => {
28+
await node.use(actionContext);
29+
// The refresh logic will use the cached contexts retrieved by polling (auto refresh logic)
30+
// Since the context is changed, clear the cached contexts, otherwise the old context will be shown as active
31+
ext.contextsRoot.clearPollingCache();
32+
});
33+
} finally {
34+
LocalRootTreeItemBase.autoRefreshViews = true;
35+
}
36+
2337
await ext.contextsRoot.refresh();
2438

2539
if (invokedFromCommandPalette) {

src/tree/LocalRootTreeItemBase.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export abstract class LocalRootTreeItemBase<TItem extends ILocalItem, TProperty
5454
public abstract getItems(): Promise<TItem[] | undefined>;
5555
public abstract getPropertyValue(item: TItem, property: TProperty): string;
5656

57+
public static autoRefreshViews: boolean = true;
58+
5759
public groupBySetting: TProperty | CommonGroupBy;
5860
public sortBySetting: CommonSortBy;
5961
public labelSetting: TProperty;
@@ -72,6 +74,10 @@ export abstract class LocalRootTreeItemBase<TItem extends ILocalItem, TProperty
7274
return workspace.getConfiguration(`${configPrefix}.${this.treePrefix}`);
7375
}
7476

77+
private get autoRefreshEnabled(): boolean {
78+
return window.state.focused && LocalRootTreeItemBase.autoRefreshViews;
79+
}
80+
7581
protected getRefreshInterval(): number {
7682
const configOptions: WorkspaceConfiguration = workspace.getConfiguration('docker');
7783
return configOptions.get<number>('explorerRefreshInterval', 2000)
@@ -89,8 +95,12 @@ export abstract class LocalRootTreeItemBase<TItem extends ILocalItem, TProperty
8995
const refreshInterval: number = this.getRefreshInterval();
9096
intervalId = setInterval(
9197
async () => {
92-
if (window.state.focused && await this.hasChanged()) {
93-
await this.refresh();
98+
if (this.autoRefreshEnabled && await this.hasChanged()) {
99+
// Auto refresh could be disabled while invoking the hasChanged()
100+
// So check again before starting the refresh.
101+
if (this.autoRefreshEnabled) {
102+
await this.refresh();
103+
}
94104
}
95105
},
96106
refreshInterval);
@@ -118,13 +128,17 @@ export abstract class LocalRootTreeItemBase<TItem extends ILocalItem, TProperty
118128
})];
119129
}
120130

131+
public clearPollingCache(): void {
132+
this._itemsFromPolling = undefined;
133+
}
134+
121135
public async loadMoreChildrenImpl(_clearCache: boolean, context: IActionContext): Promise<AzExtTreeItem[]> {
122136
try {
123137
// eslint-disable-next-line @typescript-eslint/no-floating-promises
124138
ext.activityMeasurementService.recordActivity('overallnoedit');
125139

126140
this._currentItems = this._itemsFromPolling || await this.getSortedItems();
127-
this._itemsFromPolling = undefined;
141+
this.clearPollingCache();
128142
this.failedToConnect = false;
129143
this._currentDockerStatus = 'Running';
130144
} catch (error) {
@@ -345,7 +359,7 @@ export abstract class LocalRootTreeItemBase<TItem extends ILocalItem, TProperty
345359
this._itemsFromPolling = await this.getSortedItems();
346360
pollingDockerStatus = 'Running';
347361
} catch (error) {
348-
this._itemsFromPolling = undefined;
362+
this.clearPollingCache();
349363
pollingDockerStatus = await dockerInstallStatusProvider.isDockerInstalled() ? 'Installed' : 'NotInstalled';
350364
isDockerStatusChanged = pollingDockerStatus !== this._currentDockerStatus;
351365
}

src/tree/contexts/ContextTreeItem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class ContextTreeItem extends AzExtTreeItem {
5252
}
5353

5454
public async deleteTreeItemImpl(context: IActionContext): Promise<void> {
55-
await dockerContextManager.remove(this.name);
55+
return dockerContextManager.remove(this.name);
5656
}
5757

5858
public async inspect(context: IActionContext): Promise<string> {

0 commit comments

Comments
 (0)