Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch file watcher to stop use polling; bump vscode to 1.66.0 #1945

Merged
merged 5 commits into from
Oct 18, 2023
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
2 changes: 1 addition & 1 deletion packages/cursorless-vscode-e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@types/lodash": "4.14.181",
"@types/mocha": "^8.0.4",
"@types/sinon": "^10.0.2",
"@types/vscode": "~1.61.0",
"@types/vscode": "~1.66.0",
"chai": "^4.3.6",
"js-yaml": "^4.1.0",
"mocha": "^10.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/cursorless-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://www.cursorless.org/",
"engines": {
"vscode": "^1.61.0"
"vscode": "^1.66.0"
},
"extensionKind": [
"ui",
Expand Down Expand Up @@ -1069,7 +1069,7 @@
"@types/sinon": "^10.0.2",
"@types/tinycolor2": "1.4.3",
"@types/uuid": "^8.3.4",
"@types/vscode": "~1.61.0",
"@types/vscode": "~1.66.0",
"chai": "^4.3.6",
"esbuild": "^0.17.11",
"fast-xml-parser": "^4.2.5",
Expand Down
55 changes: 10 additions & 45 deletions packages/cursorless-vscode/src/ide/vscode/VscodeFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,17 @@
import {
Disposable,
FileSystem,
PathChangeListener,
walkFiles,
} from "@cursorless/common";
import { stat } from "fs/promises";
import { max } from "lodash";
import { Disposable, FileSystem, PathChangeListener } from "@cursorless/common";
import { RelativePattern, workspace } from "vscode";

export class VscodeFileSystem implements FileSystem {
watchDir(path: string, onDidChange: PathChangeListener): Disposable {
// Just poll for now; we can take advantage of VSCode's sophisticated
// watcher later. Note that we would need to do a version check, as VSCode
// file watcher is only available in more recent versions of VSCode.
pokey marked this conversation as resolved.
Show resolved Hide resolved
return new PollingFileSystemWatcher(path, onDidChange);
}
}

const CHECK_INTERVAL_MS = 1000;
// FIXME: Support globs?
const watcher = workspace.createFileSystemWatcher(
new RelativePattern(path, "**"),
);

class PollingFileSystemWatcher implements Disposable {
private maxMtimeMs: number = -1;
private timer: NodeJS.Timer;

constructor(
private readonly path: string,
private readonly onDidChange: PathChangeListener,
) {
this.checkForChanges = this.checkForChanges.bind(this);
this.timer = setInterval(this.checkForChanges, CHECK_INTERVAL_MS);
}

private async checkForChanges() {
const paths = await walkFiles(this.path);

const maxMtime =
max(
(await Promise.all(paths.map((file) => stat(file)))).map(
(stat) => stat.mtimeMs,
),
) ?? 0;

if (maxMtime > this.maxMtimeMs) {
this.maxMtimeMs = maxMtime;
this.onDidChange();
}
}
watcher.onDidChange(onDidChange);
pokey marked this conversation as resolved.
Show resolved Hide resolved
watcher.onDidCreate(onDidChange);
watcher.onDidDelete(onDidChange);

dispose() {
clearInterval(this.timer);
return watcher;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,9 @@ function watchDir(
new vscode.RelativePattern(path, `**/*${CURSORLESS_HAT_SHAPES_SUFFIX}`),
);

return vscode.Disposable.from(
hatsDirWatcher,
hatsDirWatcher.onDidChange(onDidChange),
hatsDirWatcher.onDidCreate(onDidChange),
hatsDirWatcher.onDidDelete(onDidChange),
);
hatsDirWatcher.onDidChange(onDidChange);
hatsDirWatcher.onDidCreate(onDidChange);
hatsDirWatcher.onDidDelete(onDidChange);

return hatsDirWatcher;
}
2 changes: 1 addition & 1 deletion packages/vscode-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@cursorless/common": "workspace:*"
},
"devDependencies": {
"@types/vscode": "~1.61.0"
"@types/vscode": "~1.66.0"
},
"types": "./out/index.d.ts",
"exports": {
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.