Skip to content

Commit

Permalink
Switch file watcher to stop use polling; bump vscode to 1.66.0 (#1945)
Browse files Browse the repository at this point in the history
- Fixes #1808
- Fixes #1846

## Checklist

- [-] I have added
[tests](https://www.cursorless.org/docs/contributing/test-case-recorder/)
- [-] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [-] I have not broken the cheatsheet
  • Loading branch information
pokey authored Oct 18, 2023
1 parent 3e0370a commit 4223929
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 63 deletions.
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.
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);
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.

0 comments on commit 4223929

Please sign in to comment.