Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Add debounce to restart watchers (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Feb 26, 2024
1 parent de1b906 commit 0aa9e97
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
26 changes: 26 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,29 @@ export async function pathExists(
return false;
}
}

// Creates a debounced version of a function with the specified delay. If the function is invoked before the delay runs
// out, then the previous invocation of the function gets cancelled and a new one is scheduled.
//
// Example:
// ```typescript
// // Invoking debouncedFoo will only execute after a second has passed since the last of all invocations
// const debouncedFoo = debounce(this.foo.bind(this), 1000);
// ```
export function debounce(fn: (...args: any[]) => Promise<void>, delay: number) {
let timeoutID: NodeJS.Timeout | null = null;

return function (...args: any[]) {
if (timeoutID) {
clearTimeout(timeoutID);
}

return new Promise((resolve, reject) => {
timeoutID = setTimeout(() => {
fn(...args)
.then((result) => resolve(result))
.catch((error) => reject(error));
}, delay);
});
};
}
27 changes: 17 additions & 10 deletions src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
LOG_CHANNEL,
WorkspaceInterface,
STATUS_EMITTER,
debounce,
} from "./common";
import { WorkspaceChannel } from "./workspaceChannel";

Expand Down Expand Up @@ -256,11 +257,15 @@ export class Workspace implements WorkspaceInterface {
const watcher = vscode.workspace.createFileSystemWatcher(
new vscode.RelativePattern(this.workspaceFolder, pattern),
);
context.subscriptions.push(watcher);

watcher.onDidChange(this.restart.bind(this));
watcher.onDidCreate(this.restart.bind(this));
watcher.onDidDelete(this.restart.bind(this));
const debouncedRestart = debounce(this.restart.bind(this), 5000);

context.subscriptions.push(
watcher,
watcher.onDidChange(debouncedRestart),
watcher.onDidCreate(debouncedRestart),
watcher.onDidDelete(debouncedRestart),
);
}

private registerRebaseWatcher(context: vscode.ExtensionContext) {
Expand All @@ -276,7 +281,6 @@ export class Workspace implements WorkspaceInterface {
".git/{rebase-merge,rebase-apply}",
),
);
context.subscriptions.push(workspaceWatcher, parentWatcher);

const startRebase = () => {
this.#rebaseInProgress = true;
Expand All @@ -286,10 +290,13 @@ export class Workspace implements WorkspaceInterface {
await this.restart();
};

// When one of the rebase files are created, we set this flag to prevent restarting during the rebase
workspaceWatcher.onDidCreate(startRebase);

// Once they are deleted and the rebase is complete, then we restart
workspaceWatcher.onDidDelete(stopRebase);
context.subscriptions.push(
workspaceWatcher,
parentWatcher,
// When one of the rebase files are created, we set this flag to prevent restarting during the rebase
workspaceWatcher.onDidCreate(startRebase),
// Once they are deleted and the rebase is complete, then we restart
workspaceWatcher.onDidDelete(stopRebase),
);
}
}

0 comments on commit 0aa9e97

Please sign in to comment.