-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathVscodeFileSystem.ts
More file actions
52 lines (44 loc) · 1.36 KB
/
VscodeFileSystem.ts
File metadata and controls
52 lines (44 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {
Disposable,
FileSystem,
PathChangeListener,
walkFiles,
} from "@cursorless/common";
import { stat } from "fs/promises";
import { max } from "lodash";
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;
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();
}
}
dispose() {
clearInterval(this.timer);
}
}