Skip to content

Commit fbe2350

Browse files
authored
Use the same debounced restart for all watchers (#2818)
### Motivation I noticed another issue with our restart watchers: they don't share the same debounced function. This matters because currently if two separate watchers are triggered in sequence, then we restart twice. This is of course not what we want. We want to restart a single time within the debounce time, no matter how many watchers have been triggered. ### Implementation The core of the issue is that if we have multiple debounced functions, they each have their own timeout object with a different ID. That leads to this scenario: 1. Rebase watcher fires its debounced restart 2. After rebase is complete, the lockfile was modified so that watcher is fired too 3. Because the debounced restart function of the lockfile is not the same as the rebase watcher, it won't clear the timeout of the rebase invocation 4. This triggers two consecutive restarts for no reason The fix is to ensure that we only have one single debounced restart function that is used by all of the watchers. This guarantees that, no matter which watcher is fired, it will always clear the previous restart timeout and avoid doing it multiple times.
1 parent aa6fdaf commit fbe2350

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

vscode/src/workspace.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export class Workspace implements WorkspaceInterface {
2929
#inhibitRestart = false;
3030
#error = false;
3131

32+
private readonly debouncedRestart = debounce(async (reason: string) => {
33+
this.outputChannel.info(`Restarting the Ruby LSP because ${reason}`);
34+
await this.restart();
35+
}, 5000);
36+
3237
constructor(
3338
context: vscode.ExtensionContext,
3439
workspaceFolder: vscode.WorkspaceFolder,
@@ -376,7 +381,7 @@ export class Workspace implements WorkspaceInterface {
376381

377382
// Handler for only triggering restart if the contents of the file have been modified. If the file was just touched,
378383
// but the contents are the same, we don't want to restart
379-
const debouncedRestartWithHashCheck = debounce(async (uri: vscode.Uri) => {
384+
const debouncedRestartWithHashCheck = async (uri: vscode.Uri) => {
380385
const fileContents = await vscode.workspace.fs.readFile(uri);
381386
const fsPath = uri.fsPath;
382387

@@ -385,21 +390,14 @@ export class Workspace implements WorkspaceInterface {
385390
const currentSha = hash.digest("hex");
386391

387392
if (this.restartDocumentShas.get(fsPath) !== currentSha) {
388-
this.outputChannel.info(
389-
`Restarting the Ruby LSP because ${pattern} changed`,
390-
);
391-
392393
this.restartDocumentShas.set(fsPath, currentSha);
393-
await this.restart();
394+
await this.debouncedRestart(`${pattern} changed`);
394395
}
395-
}, 5000);
396+
};
396397

397-
const debouncedRestart = debounce(async () => {
398-
this.outputChannel.info(
399-
`Restarting the Ruby LSP because ${pattern} changed`,
400-
);
401-
await this.restart();
402-
}, 5000);
398+
const debouncedRestart = async () => {
399+
await this.debouncedRestart(`${pattern} changed`);
400+
};
403401

404402
context.subscriptions.push(
405403
watcher,
@@ -417,16 +415,10 @@ export class Workspace implements WorkspaceInterface {
417415
const start = () => {
418416
this.#inhibitRestart = true;
419417
};
420-
const debouncedRestart = debounce(async () => {
421-
this.outputChannel.info(
422-
`Restarting the Ruby LSP because ${glob} changed`,
423-
);
424-
await this.restart();
425-
}, 5000);
426418

427419
const stop = async () => {
428420
this.#inhibitRestart = false;
429-
await debouncedRestart();
421+
await this.debouncedRestart(`${glob} changed`);
430422
};
431423

432424
this.context.subscriptions.push(

0 commit comments

Comments
 (0)