Skip to content

Commit

Permalink
Trigger refresh when using Select Interpreter command if no envs we…
Browse files Browse the repository at this point in the history
…re found previously (#19361)

Trigger refresh when using Select Interpreter command if no envs were found previously
  • Loading branch information
Kartik Raj authored Jun 27, 2022
1 parent 1ffa23a commit a63dd9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/client/pythonEnvironments/base/locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export type TriggerRefreshOptions = {
*/
clearCache?: boolean;
/**
* Only trigger a refresh if it hasn't already been triggered for this session.
* Only trigger a refresh if it hasn't already been triggered for this session, or if no envs were found previously.
*/
ifNotTriggerredAlready?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
private progressPromises = new Map<ProgressReportStage, Deferred<void>>();

/** Keeps track of whether a refresh has been triggered for various queries. */
private wasRefreshTriggeredForQuery = new Map<PythonLocatorQuery | undefined, boolean>();
private hasRefreshFinishedForQuery = new Map<PythonLocatorQuery | undefined, boolean>();

private readonly progress = new EventEmitter<ProgressNotificationEvent>();

Expand Down Expand Up @@ -102,13 +102,12 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection

public triggerRefresh(query?: PythonLocatorQuery, options?: TriggerRefreshOptions): Promise<void> {
const stopWatch = new StopWatch();
if (options?.ifNotTriggerredAlready) {
if (this.wasRefreshTriggered(query)) {
return Promise.resolve(); // Refresh was already triggered, return.
}
}
let refreshPromise = this.getRefreshPromiseForQuery(query);
if (!refreshPromise) {
if (options?.ifNotTriggerredAlready && this.hasRefreshFinished(query) && this.getEnvs().length) {
// Do not trigger another refresh if a refresh has previously finished and envs were found.
return Promise.resolve();
}
refreshPromise = this.startRefresh(query, options);
}
return refreshPromise.then(() => this.sendTelemetry(query, stopWatch));
Expand Down Expand Up @@ -191,8 +190,8 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
return this.refreshesPerQuery.get(query)?.promise ?? this.refreshesPerQuery.get(undefined)?.promise;
}

private wasRefreshTriggered(query?: PythonLocatorQuery) {
return this.wasRefreshTriggeredForQuery.get(query) ?? this.wasRefreshTriggeredForQuery.get(undefined);
private hasRefreshFinished(query?: PythonLocatorQuery) {
return this.hasRefreshFinishedForQuery.get(query) ?? this.hasRefreshFinishedForQuery.get(undefined);
}

/**
Expand Down Expand Up @@ -245,7 +244,7 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
}

private sendTelemetry(query: PythonLocatorQuery | undefined, stopWatch: StopWatch) {
if (!query && !this.wasRefreshTriggered(query)) {
if (!query && !this.hasRefreshFinished(query)) {
// Intent is to capture time taken for discovery of all envs to complete the first time.
sendTelemetryEvent(EventName.PYTHON_INTERPRETER_DISCOVERY, stopWatch.elapsedTime, {
interpreters: this.cache.getAllEnvs().length,
Expand All @@ -254,6 +253,6 @@ export class EnvsCollectionService extends PythonEnvsWatcher<PythonEnvCollection
.filter((e) => getEnvPath(e.executable.filename, e.location).pathType === 'envFolderPath').length,
});
}
this.wasRefreshTriggeredForQuery.set(query, true);
this.hasRefreshFinishedForQuery.set(query, true);
}
}

0 comments on commit a63dd9e

Please sign in to comment.