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

fix missing watchAll #109

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
29 changes: 12 additions & 17 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,25 @@ class Server {
}

class FileWatchers {
watchers: FSWatcher[] = [];
#watchers: FSWatcher[] = [];

constructor(
readonly root: string,
readonly files: {name: string}[],
readonly cb: (name: string) => void
) {}

async watchAll() {
const fileset = [...new Set(this.files.map(({name}) => name))];
static async watchAll(root: string, files: {name: string}[], cb: (name: string) => void) {
const watchers = new FileWatchers();
const fileset = [...new Set(files.map(({name}) => name))];
for (const name of fileset) {
const watchPath = await FileWatchers.getWatchPath(this.root, name);
const watchPath = await FileWatchers.getWatchPath(root, name);
let prevState = await maybeStat(watchPath);
this.watchers.push(
watchers.#watchers.push(
watch(watchPath, async () => {
const newState = await maybeStat(watchPath);
// Ignore if the file was truncated or not modified.
if (prevState?.mtimeMs === newState?.mtimeMs || newState?.size === 0) return;
prevState = newState;
this.cb(name);
cb(name);
})
);
}
return watchers;
}

static async getWatchPath(root: string, name: string) {
Expand All @@ -183,8 +179,8 @@ class FileWatchers {
}

close() {
this.watchers.forEach((w) => w.close());
this.watchers = [];
this.#watchers.forEach((w) => w.close());
this.#watchers = [];
}
}

Expand Down Expand Up @@ -212,8 +208,7 @@ function handleWatch(socket: WebSocket, options: {root: string; resolver: CellRe

async function refreshMarkdown(path: string): Promise<WatchListener<string>> {
let current = await readMarkdown(path, root);
attachmentWatcher = new FileWatchers(root, current.parse.files, refreshAttachment(current.parse));
await attachmentWatcher.watchAll();
attachmentWatcher = await FileWatchers.watchAll(root, current.parse.files, refreshAttachment(current.parse));
return async function watcher(event) {
switch (event) {
case "rename": {
Expand All @@ -237,7 +232,7 @@ function handleWatch(socket: WebSocket, options: {root: string; resolver: CellRe
const diff = resolveDiffs(diffMarkdown(current, updated), resolver);
send({type: "update", diff, previousHash: current.hash, updatedHash: updated.hash});
attachmentWatcher?.close();
attachmentWatcher = new FileWatchers(root, updated.parse.files, refreshAttachment(updated.parse));
attachmentWatcher = await FileWatchers.watchAll(root, updated.parse.files, refreshAttachment(updated.parse));
current = updated;
break;
}
Expand Down