From 9bb4e84e657d60fb671ee7986fae39c863d313ff Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Thu, 9 Sep 2021 00:31:57 -0700 Subject: [PATCH 1/2] Ensure we do not start a new refresh for each change event --- .../composite/envsCollectionService.ts | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/client/pythonEnvironments/base/locators/composite/envsCollectionService.ts b/src/client/pythonEnvironments/base/locators/composite/envsCollectionService.ts index 54887ce732fa..e95c11cb496a 100644 --- a/src/client/pythonEnvironments/base/locators/composite/envsCollectionService.ts +++ b/src/client/pythonEnvironments/base/locators/composite/envsCollectionService.ts @@ -21,6 +21,9 @@ export class EnvsCollectionService extends PythonEnvsWatcher>(); + /** Keeps track of whether there are any scheduled refreshes other than the ongoing one for various queries. */ + private scheduledRefreshes = new Map(); + private readonly refreshStarted = new EventEmitter(); public get onRefreshStart(): Event { @@ -33,12 +36,18 @@ export class EnvsCollectionService extends PythonEnvsWatcher - this.triggerNewRefresh().then(() => { + this.locator.onChanged((event) => { + const query = undefined; // We can also form a query based on the event, but skip that for simplicity. + const isNewRefreshScheduled = this.scheduledRefreshes.get(query); + if (isNewRefreshScheduled) { + // If there is already a new refresh scheduled for the query, no need to start another one. + return; + } + this.scheduleNewRefresh(query).then(() => { // Once refresh of cache is complete, notify changes. this.fire({ type: event.type, searchLocation: event.searchLocation }); - }), - ); + }); + }); this.cache.onChanged((e) => { this.fire(e); }); @@ -71,18 +80,7 @@ export class EnvsCollectionService extends PythonEnvsWatcher { - const refreshPromise = this.getRefreshPromiseForQuery(query); - const nextRefreshPromise = refreshPromise - ? refreshPromise.then(() => this.startRefresh(query)) - : this.startRefresh(query); - return nextRefreshPromise; - } - - private async startRefresh(query: PythonLocatorQuery | undefined): Promise { + private startRefresh(query: PythonLocatorQuery | undefined): Promise { const stopWatch = new StopWatch(); this.refreshStarted.fire(); const iterator = this.locator.iterEnvs(query); @@ -148,4 +146,18 @@ export class EnvsCollectionService extends PythonEnvsWatcher { + this.scheduledRefreshes.set(query, true); + const refreshPromise = this.getRefreshPromiseForQuery(query) ?? Promise.resolve(); + const nextRefreshPromise = refreshPromise.then(() => { + // No more scheduled refreshes for this query as we're about to start the scheduled one. + this.scheduledRefreshes.set(query, false); + this.startRefresh(query); + }); + return nextRefreshPromise; + } } From ad4f714a60d7a053c9004c1c8fb42f1922eea8f6 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Thu, 9 Sep 2021 00:35:29 -0700 Subject: [PATCH 2/2] News entry --- news/2 Fixes/17339.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/2 Fixes/17339.md diff --git a/news/2 Fixes/17339.md b/news/2 Fixes/17339.md new file mode 100644 index 000000000000..bc2ccdc20a86 --- /dev/null +++ b/news/2 Fixes/17339.md @@ -0,0 +1 @@ +Ensure we do not start a new discovery for an event if one is already scheduled.