files: change directory monitor return

Only return the outermost monitor, hook onto that to cancel the nested ones
Also use the monitor flags in hopes of it actually picking up changes to files (it doesnt)
This commit is contained in:
2 * r + 2 * t 2025-03-08 00:30:30 +11:00
parent 630885daab
commit 619508b16c
2 changed files with 8 additions and 8 deletions

View file

@ -58,10 +58,10 @@ export default class Wallpapers extends GObject.Object {
let monitors = config.paths let monitors = config.paths
.get() .get()
.flatMap(p => monitorDirectory(p.path, () => this.update().catch(console.error), p.recursive)); .map(p => monitorDirectory(p.path, () => this.update().catch(console.error), p.recursive));
config.paths.subscribe(v => { config.paths.subscribe(v => {
for (const m of monitors) m.cancel(); for (const m of monitors) m.cancel();
monitors = v.flatMap(p => monitorDirectory(p.path, () => this.update().catch(console.error), p.recursive)); monitors = v.map(p => monitorDirectory(p.path, () => this.update().catch(console.error), p.recursive));
}); });
} }
} }

View file

@ -78,18 +78,18 @@ export const bindCurrentTime = (
export const monitorDirectory = (path: string, callback: (path: string) => void, recursive?: boolean) => { export const monitorDirectory = (path: string, callback: (path: string) => void, recursive?: boolean) => {
const file = Gio.file_new_for_path(path.replace("~", HOME)); const file = Gio.file_new_for_path(path.replace("~", HOME));
const monitor = file.monitor_directory(null, null); const monitor = file.monitor_directory(Gio.FileMonitorFlags.WATCH_MOUNTS | Gio.FileMonitorFlags.WATCH_MOVES, null);
monitor.connect("changed", callback); monitor.connect("changed", callback);
const monitors = [monitor];
if (recursive) { if (recursive) {
const enumerator = file.enumerate_children("standard::*", null, null); const enumerator = file.enumerate_children("standard::*", null, null);
let child; let child;
while ((child = enumerator.next_file(null))) while ((child = enumerator.next_file(null)))
if (child.get_file_type() === Gio.FileType.DIRECTORY) if (child.get_file_type() === Gio.FileType.DIRECTORY) {
monitors.push(...monitorDirectory(`${path}/${child.get_name()}`, callback, recursive)); const m = monitorDirectory(`${path}/${child.get_name()}`, callback, recursive);
monitor.connect("notify::cancelled", () => m.cancel());
}
} }
return monitors; return monitor;
}; };