Skip to content

Commit

Permalink
Merge pull request #2194 from posit-dev/dotnomad/passthrough-progress…
Browse files Browse the repository at this point in the history
…-promise

Passthrough callbacks with new `showProgressPassthrough`
  • Loading branch information
dotNomad authored Aug 28, 2024
2 parents 711e289 + 9962a1d commit c7b2999
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
24 changes: 24 additions & 0 deletions extensions/vscode/src/utils/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,27 @@ export async function showProgress(
console.log(`Progress for "${title}" was displayed for ${duration}ms`);
}
}

export async function showProgressPassthrough<T>(
title: string,
viewId: string,
until: () => Promise<T>,
trace = true,
): Promise<T> {
const start = performance.now();

try {
return await window.withProgress(
{
title,
location: viewId ? { viewId } : ProgressLocation.Window,
},
until,
);
} finally {
if (trace) {
const duration = Math.round(Number(performance.now() - start));
console.log(`Progress for "${title}" was displayed for ${duration}ms`);
}
}
}
29 changes: 16 additions & 13 deletions extensions/vscode/src/views/homeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import { RPackage, RVersionConfig } from "src/api/types/packages";
import { calculateTitle } from "src/utils/titles";
import { ConfigWatcherManager, WatcherManager } from "src/watchers";
import { Commands, Contexts, DebounceDelaysMS, Views } from "src/constants";
import { showProgress } from "src/utils/progress";
import { showProgress, showProgressPassthrough } from "src/utils/progress";
import { newCredential } from "src/multiStepInputs/newCredential";
import { PublisherState } from "src/state";
import { throttleWithLastPending } from "src/utils/throttle";
Expand Down Expand Up @@ -502,17 +502,17 @@ export class HomeViewProvider implements WebviewViewProvider, Disposable {
packageFile = pythonSection.packageFile;
packageMgr = pythonSection.packageManager;

const apiRequest = api.packages.getPythonPackages(
activeConfiguration.configurationName,
activeConfiguration.projectDir,
);
showProgress(
const response = await showProgressPassthrough(
"Refreshing Python Packages",
apiRequest,
Views.HomeView,
async () => {
return await api.packages.getPythonPackages(
activeConfiguration.configurationName,
activeConfiguration.projectDir,
);
},
);

const response = await apiRequest;
packages = response.data.requirements;
} catch (error: unknown) {
if (isAxiosError(error) && error.response?.status === 404) {
Expand Down Expand Up @@ -570,13 +570,16 @@ export class HomeViewProvider implements WebviewViewProvider, Disposable {
packageFile = rSection.packageFile;
packageMgr = rSection.packageManager;

const apiRequest = api.packages.getRPackages(
activeConfiguration.configurationName,
activeConfiguration.projectDir,
const response = await showProgressPassthrough(
"Refreshing R Packages",
Views.HomeView,
async () =>
await api.packages.getRPackages(
activeConfiguration.configurationName,
activeConfiguration.projectDir,
),
);
showProgress("Refreshing R Packages", apiRequest, Views.HomeView);

const response = await apiRequest;
packages = [];
Object.keys(response.data.packages).forEach((key: string) =>
packages.push(response.data.packages[key]),
Expand Down

0 comments on commit c7b2999

Please sign in to comment.