Skip to content

Cache the promise returned by the command finder #8317

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

Merged
merged 2 commits into from
Oct 31, 2019
Merged
Changes from all 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
30 changes: 23 additions & 7 deletions src/client/datascience/jupyter/jupyterCommandFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ProgressNotification = Progress<{ message?: string | undefined; increment?:
export class JupyterCommandFinder {
private readonly processServicePromise: Promise<IProcessService>;
private jupyterPath?: string;
private readonly commands = new Map<JupyterCommands, IFindCommandResult>();
private readonly commands = new Map<JupyterCommands, Promise<IFindCommandResult>>();
constructor(
private readonly interpreterService: IInterpreterService,
private readonly executionFactory: IPythonExecutionFactory,
Expand Down Expand Up @@ -100,13 +100,29 @@ export class JupyterCommandFinder {

// Only log telemetry if not already found (meaning the first time)
const timer = new StopWatch();
try {
const result = await this.findBestCommandImpl(command, cancelToken);
this.commands.set(command, result);
return result;
} finally {
sendTelemetryEvent(Telemetry.FindJupyterCommand, timer.elapsedTime, { command });
const promise = this.findBestCommandImpl(command, cancelToken)
.finally(() => sendTelemetryEvent(Telemetry.FindJupyterCommand, timer.elapsedTime, { command }));

if (cancelToken) {
let promiseCompleted = false;
promise.finally(() => promiseCompleted = true).ignoreErrors();

// If the promise is not pending, then remove the item from cache.
// As the promise would not complete correctly, as its been cancelled.
if (cancelToken.isCancellationRequested && !promiseCompleted) {
this.commands.delete(command);
}
cancelToken.onCancellationRequested(() => {
// If the promise is not pending, then remove the item from cache.
// As the promise would not complete correctly, as its been cancelled.
if (!promiseCompleted) {
this.commands.delete(command);
}
});
}

this.commands.set(command, promise);
return promise;
}

/**
Expand Down