Skip to content

Commit

Permalink
file watchers now work and force recompilation correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
azizghuloum committed Dec 26, 2024
1 parent 347b30f commit 8798cb3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
57 changes: 44 additions & 13 deletions rtsc/compile-all.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FSWatcher, watch } from "node:fs";
import { watch } from "node:fs";
import fs from "node:fs/promises";
import process from "node:process";
import path, { basename, dirname, join } from "node:path";
Expand All @@ -11,9 +11,27 @@ import { parse } from "../src/parse.ts";
import { assert } from "../src/assert.ts";

class DirWatcher {
private fswatcher: FSWatcher;
constructor(fswatcher: FSWatcher) {
this.fswatcher = fswatcher;
private dir: string;
private callbacks: { [filename: string]: (path: string) => void } = {};
public onEvent: ((file: string) => void) | undefined = undefined;
constructor(dir: string) {
this.dir = dir;
}
watchFile(path: string, callback: (path: string) => void): { close(): void } {
assert(dirname(path) === this.dir);
const filename = basename(path);
assert(!this.callbacks[filename], `multiple watches for same path '${path}'`);
this.callbacks[filename] = callback;
return {
close() {
throw new Error(`close is not yet done`);
},
};
}
processEvent(event: "rename" | "change", file: string) {
this.onEvent?.(file);
const callback = this.callbacks[file];
if (callback) callback(join(this.dir, file));
}
}

Expand All @@ -25,6 +43,24 @@ class WatchFS {
this.init();
}

watchFile(path: string, callback: (path: string) => void): { close(): void } {
const dir = dirname(path);
const watcher = this.get_or_create_watcher(dir);
return watcher.watchFile(path, callback);
}

private get_or_create_watcher(abspath: string): DirWatcher {
const existing = this.dir_watchers[abspath];
if (existing) return existing;
const fswatcher = watch(abspath, { encoding: "utf8", recursive: false }, (event, file) => {
assert(file !== null);
watcher.processEvent(event, file);
});
const watcher = new DirWatcher(abspath);
this.dir_watchers[abspath] = watcher;
return watcher;
}

private async init() {
const gitignore_content = await fs.readFile(".gitignore", { encoding: "utf8" });
const ig = ignore().add(gitignore_content).add("/.git");
Expand Down Expand Up @@ -57,11 +93,9 @@ class WatchFS {
};

const seed_dir = async (relpath: string, abspath: string) => {
const fswatcher = watch(abspath, { encoding: "utf8", recursive: false }, (event, file) => {
console.log(`watch ${event} ${file} in ${abspath}`);
if (file !== null) check_file(file, relpath, abspath);
});
this.dir_watchers[abspath] = new DirWatcher(fswatcher);
const watcher = this.get_or_create_watcher(abspath);
assert(!watcher.onEvent);
watcher.onEvent = (file) => check_file(file, relpath, abspath);
fs.readdir(relpath).then((files) =>
files.forEach((file) => check_file(file, relpath, abspath)),
);
Expand All @@ -75,10 +109,7 @@ const globals = get_globals("es2024.full");
const patterns = core_patterns(parse);
const library_manager = new LibraryManager(patterns, globals, ["es2024.full"], {
watchFile(path, callback) {
const watcher = {
close() {},
};
return watcher;
return FS.watchFile(path, callback);
},
});

Expand Down
6 changes: 3 additions & 3 deletions src/library-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ abstract class Module implements imported_module {
(x) => (x.dependant_modules = x.dependant_modules.filter((x) => x !== this)),
);
this.imported_modules = [];
dependant_modules.forEach((x) => x.force_recompile());
this.ensureUpToDate();
await Promise.all(dependant_modules.map((x) => x.force_recompile()));
await this.ensureUpToDate();
}

async file_changed(): Promise<void> {
Expand All @@ -202,7 +202,7 @@ abstract class Module implements imported_module {
switch (state.type) {
case "fresh": {
if (t && t > state.mtime) {
this.force_recompile();
await this.force_recompile();
}
return;
}
Expand Down

0 comments on commit 8798cb3

Please sign in to comment.