Convert refresh to return a promise

This commit is contained in:
Robert
2023-05-25 17:36:44 +01:00
parent 5a2cb8bc41
commit 5405b1bf29
5 changed files with 12 additions and 10 deletions

View File

@@ -9,7 +9,7 @@ import { getErrorMessage } from "../pure/helpers-pure";
*/ */
export abstract class Discovery<T> extends DisposableObject { export abstract class Discovery<T> extends DisposableObject {
private retry = false; private retry = false;
private discoveryInProgress = false; private currentDiscoveryPromise: Promise<void> | undefined;
constructor(private readonly name: string) { constructor(private readonly name: string) {
super(); super();
@@ -18,8 +18,10 @@ export abstract class Discovery<T> extends DisposableObject {
/** /**
* Force the discovery process to run. Normally invoked by the derived class when a relevant file * Force the discovery process to run. Normally invoked by the derived class when a relevant file
* system change is detected. * system change is detected.
*
* Returns a promise that resolves when the refresh is complete, including any retries.
*/ */
public refresh(): void { public refresh(): Promise<void> {
// We avoid having multiple discovery operations in progress at the same time. Otherwise, if we // We avoid having multiple discovery operations in progress at the same time. Otherwise, if we
// got a storm of refresh requests due to, say, the copying or deletion of a large directory // got a storm of refresh requests due to, say, the copying or deletion of a large directory
// tree, we could potentially spawn a separate simultaneous discovery operation for each // tree, we could potentially spawn a separate simultaneous discovery operation for each
@@ -36,14 +38,14 @@ export abstract class Discovery<T> extends DisposableObject {
// other change notifications that might be coming along. However, this would create more // other change notifications that might be coming along. However, this would create more
// latency in the common case, in order to save a bit of latency in the uncommon case. // latency in the common case, in order to save a bit of latency in the uncommon case.
if (this.discoveryInProgress) { if (this.currentDiscoveryPromise !== undefined) {
// There's already a discovery operation in progress. Tell it to restart when it's done. // There's already a discovery operation in progress. Tell it to restart when it's done.
this.retry = true; this.retry = true;
} else { } else {
// No discovery in progress, so start one now. // No discovery in progress, so start one now.
this.discoveryInProgress = true; this.currentDiscoveryPromise = this.launchDiscovery();
void this.launchDiscovery();
} }
return this.currentDiscoveryPromise;
} }
/** /**
@@ -71,7 +73,7 @@ export abstract class Discovery<T> extends DisposableObject {
this.retry = false; this.retry = false;
await this.launchDiscovery(); await this.launchDiscovery();
} else { } else {
this.discoveryInProgress = false; this.currentDiscoveryPromise = undefined;
// If the discovery was successful, then update any listeners with the results. // If the discovery was successful, then update any listeners with the results.
if (results !== undefined) { if (results !== undefined) {

View File

@@ -21,7 +21,7 @@ export class QueriesModule extends DisposableObject {
const queryDiscovery = new QueryDiscovery(app, cliServer); const queryDiscovery = new QueryDiscovery(app, cliServer);
this.push(queryDiscovery); this.push(queryDiscovery);
queryDiscovery.refresh(); void queryDiscovery.refresh();
const queriesPanel = new QueriesPanel(queryDiscovery); const queriesPanel = new QueriesPanel(queryDiscovery);
this.push(queriesPanel); this.push(queriesPanel);

View File

@@ -64,7 +64,7 @@ export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
private handleDidChange(uri: Uri): void { private handleDidChange(uri: Uri): void {
if (!QLTestDiscovery.ignoreTestPath(uri.fsPath)) { if (!QLTestDiscovery.ignoreTestPath(uri.fsPath)) {
this.refresh(); void this.refresh();
} }
} }
protected async discover(): Promise<QLTestDiscoveryResults> { protected async discover(): Promise<QLTestDiscoveryResults> {

View File

@@ -115,7 +115,7 @@ export class QLTestAdapter extends DisposableObject implements TestAdapter {
this.qlTestDiscovery = this.push( this.qlTestDiscovery = this.push(
new QLTestDiscovery(workspaceFolder, cliServer), new QLTestDiscovery(workspaceFolder, cliServer),
); );
this.qlTestDiscovery.refresh(); void this.qlTestDiscovery.refresh();
this.push(this.qlTestDiscovery.onDidChangeTests(this.discoverTests, this)); this.push(this.qlTestDiscovery.onDidChangeTests(this.discoverTests, this));
} }

View File

@@ -92,7 +92,7 @@ class WorkspaceFolderHandler extends DisposableObject {
this.push( this.push(
this.testDiscovery.onDidChangeTests(this.handleDidChangeTests, this), this.testDiscovery.onDidChangeTests(this.handleDidChangeTests, this),
); );
this.testDiscovery.refresh(); void this.testDiscovery.refresh();
} }
private handleDidChangeTests(): void { private handleDidChangeTests(): void {