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

Add debounce to restart watchers #1040

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,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),
);
}
}
Loading