34 lines
No EOL
1.1 KiB
TypeScript
34 lines
No EOL
1.1 KiB
TypeScript
import { Gtk } from "ags/gtk4";
|
|
import Gio from "gi://Gio";
|
|
|
|
export function watchFile(
|
|
path: string,
|
|
onChange: () => void,
|
|
owner: Gtk.Widget
|
|
): Gio.FileMonitor | null {
|
|
try {
|
|
const file = Gio.File.new_for_path(path);
|
|
const parent = file.get_parent();
|
|
if (parent) {
|
|
try {
|
|
parent.make_directory_with_parents(null);
|
|
} catch (e: any) {
|
|
if (!e.message?.includes("File exists")) {
|
|
console.error("Failed to create directory:", e);
|
|
}
|
|
}
|
|
}
|
|
const monitor = file.monitor(Gio.FileMonitorFlags.NONE, null);
|
|
monitor.connect("changed", (_monitor, _file, _otherFile, eventType) => {
|
|
if (eventType === Gio.FileMonitorEvent.CHANGES_DONE_HINT) {
|
|
onChange();
|
|
}
|
|
});
|
|
// Attach to owner to avoid GC
|
|
(owner as any)[`__monitor_${path.replace(/\//g, "_")}`] = monitor;
|
|
return monitor;
|
|
} catch (err) {
|
|
console.error("Failed to set up monitor for", path, err);
|
|
return null;
|
|
}
|
|
} |