From 2793dc7132dcf2940527e3677eff3ead7e677818 Mon Sep 17 00:00:00 2001 From: Nicolai Van der Storm Date: Sun, 10 Mar 2024 17:09:47 +0100 Subject: [PATCH 1/2] added recursive option to monitorFile --- src/utils/file.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/utils/file.ts b/src/utils/file.ts index 9fe062f2..8b638169 100644 --- a/src/utils/file.ts +++ b/src/utils/file.ts @@ -76,6 +76,7 @@ export function monitorFile( path: string, callback?: (file: Gio.File, event: Gio.FileMonitorEvent) => void, flags = Gio.FileMonitorFlags.NONE, + recursive = true, ) { // FIXME: remove the checking in the next release // @ts-expect-error @@ -93,6 +94,30 @@ export function monitorFile( if (callback) mon.connect('changed', (_, file, _f, event) => callback(file, event)); + // If recursive is enabled enumerate files in the subfolders + const enumerator = file.enumerate_children('standard::*', + Gio.FileQueryInfoFlags.NONE, null); + while (recursive) { + try { + const fileInfo = enumerator.next_file(null); + + if (!fileInfo) + break; + + const fileType = fileInfo.get_file_type(); + if (fileType === Gio.FileType.DIRECTORY) { + const subfolder = file.get_child(fileInfo.get_name()); + const subfolderPath = subfolder.get_path(); + + if (subfolderPath !== null) + monitorFile(subfolderPath, callback, flags, recursive); + } + } catch (error) { + logError(error); + } + } + + // we need to save a reference in case the user doesn't // otherwise GC will pick it up fileMonitors.set(mon, true); From 19375bb61ce101edbdd8bbd9dc08249d927df10b Mon Sep 17 00:00:00 2001 From: Nicolai Van der Storm Date: Tue, 19 Mar 2024 09:53:07 +0100 Subject: [PATCH 2/2] Changed to an options param for `monitorFile` --- src/utils/file.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/utils/file.ts b/src/utils/file.ts index 8b638169..1ca374f9 100644 --- a/src/utils/file.ts +++ b/src/utils/file.ts @@ -75,21 +75,23 @@ const fileMonitors: Map = new Map; export function monitorFile( path: string, callback?: (file: Gio.File, event: Gio.FileMonitorEvent) => void, - flags = Gio.FileMonitorFlags.NONE, - recursive = true, + options: { flags: Gio.FileMonitorFlags; recursive: boolean } = { + flags: Gio.FileMonitorFlags.NONE, + recursive: true, + }, ) { // FIXME: remove the checking in the next release - // @ts-expect-error - if (flags === 'file' || flags === 'directory') { - throw Error( - `${flags}` + ' passed as a parameter in `monitorFile`. ' + - 'Specifying the type is no longer required.', + if (typeof options === 'number') { + console.warn( + `${options}` + + ' passed as a parameter in `options`.\n' + + 'options parameter should be {flags: Gio.FileMonitorFlags, recursive: boolean}.', ); } try { const file = Gio.File.new_for_path(path); - const mon = file.monitor(flags, null); + const mon = file.monitor(options.flags, null); if (callback) mon.connect('changed', (_, file, _f, event) => callback(file, event)); @@ -97,7 +99,7 @@ export function monitorFile( // If recursive is enabled enumerate files in the subfolders const enumerator = file.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NONE, null); - while (recursive) { + while (options.recursive) { try { const fileInfo = enumerator.next_file(null); @@ -110,14 +112,13 @@ export function monitorFile( const subfolderPath = subfolder.get_path(); if (subfolderPath !== null) - monitorFile(subfolderPath, callback, flags, recursive); + monitorFile(subfolderPath, callback, options); } } catch (error) { logError(error); } } - // we need to save a reference in case the user doesn't // otherwise GC will pick it up fileMonitors.set(mon, true);