Add watcher polling settings.

This commit is contained in:
Patrick Fic
2025-03-26 11:32:41 -07:00
parent 791c518920
commit e2ccbf7007
11 changed files with 247 additions and 65 deletions

View File

@@ -8,6 +8,8 @@ import {
SettingsWatchedFilePathsAdd,
SettingsWatchedFilePathsGet,
SettingsWatchedFilePathsRemove,
SettingsWatcherPollingGet,
SettingsWatcherPollingSet,
} from "./ipcMainHandler.settings";
import { ipcMainHandleAuthStateChanged } from "./ipcMainHandler.user";
import { autoUpdater } from "electron-updater";
@@ -78,6 +80,14 @@ ipcMain.handle(
ipcTypes.toMain.settings.filepaths.remove,
SettingsWatchedFilePathsRemove,
);
ipcMain.handle(
ipcTypes.toMain.settings.watcher.getpolling,
SettingsWatcherPollingGet,
);
ipcMain.handle(
ipcTypes.toMain.settings.watcher.setpolling,
SettingsWatcherPollingSet,
);
//Watcher Handlers
ipcMain.on(ipcTypes.toMain.watcher.start, () => {

View File

@@ -2,7 +2,13 @@ import { BrowserWindow, dialog, IpcMainInvokeEvent } from "electron";
import log from "electron-log/main";
import _ from "lodash";
import Store from "../store/store";
import { addWatcherPath, removeWatcherPath, watcher } from "../watcher/watcher";
import {
addWatcherPath,
removeWatcherPath,
StartWatcher,
StopWatcher,
watcher,
} from "../watcher/watcher";
const SettingsWatchedFilePathsAdd = async (): Promise<string[]> => {
const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set.
@@ -41,8 +47,39 @@ const SettingsWatchedFilePathsGet = async (): Promise<string[]> => {
return filepaths;
};
const SettingsWatcherPollingGet = async (): Promise<{
enabled: boolean;
interval: number;
}> => {
const pollingEnabled: { enabled: boolean; interval: number } =
Store.get("settings.polling");
return { enabled: pollingEnabled.enabled, interval: pollingEnabled.interval };
};
const SettingsWatcherPollingSet = async (
event: IpcMainInvokeEvent,
pollingSettings: {
enabled: boolean;
interval: number;
},
): Promise<{
enabled: boolean;
interval: number;
}> => {
log.info("Polling set", pollingSettings);
const { enabled, interval } = pollingSettings;
Store.set("settings.polling", { enabled, interval });
//Restart the watcher with these new settings.
await StopWatcher();
StartWatcher();
return { enabled, interval };
};
export {
SettingsWatchedFilePathsAdd,
SettingsWatchedFilePathsGet,
SettingsWatchedFilePathsRemove,
SettingsWatcherPollingGet,
SettingsWatcherPollingSet,
};

View File

@@ -1,4 +1,5 @@
import Store from "electron-store";
const store = new Store({
defaults: {
settings: {
@@ -6,7 +7,7 @@ const store = new Store({
runWatcherOnStartup: true,
polling: {
enabled: false,
pollingInterval: 30000,
interval: 30000,
},
},
app: {
@@ -25,4 +26,10 @@ const store = new Store({
},
});
// store.onDidAnyChange((newValue, oldValue) => {
// console.log("Settings changed", newValue, oldValue);
// const mainWindow = BrowserWindow.getAllWindows()[0];
// mainWindow?.webContents.send(ipcTypes.toRenderer.store.didChange, newValue);
// });
export default store;

View File

@@ -7,7 +7,7 @@ import ipcTypes from "../../util/ipcTypes.json";
import ImportJob from "../decoder/decoder";
import store from "../store/store";
let watcher: FSWatcher;
let watcher: FSWatcher | null;
async function StartWatcher(): Promise<boolean> {
const filePaths: string[] = store.get("settings.filepaths") || [];
@@ -33,13 +33,19 @@ async function StartWatcher(): Promise<boolean> {
}
}
const pollingSettings =
(store.get("settings.polling") as {
enabled?: boolean;
interval?: number;
}) || {};
watcher = chokidar.watch(filePaths, {
ignored: (filepath, stats) => {
const p = path.parse(filepath);
return !stats?.isFile() && p.ext !== "" && p.ext.toUpperCase() !== ".ENV"; //Only watch for .ENV files.
},
usePolling: store.get("settings.polling").enabled || false,
interval: store.get("settings.polling").pollingInterval || 1000,
usePolling: pollingSettings.enabled || false,
interval: pollingSettings.interval || 30000,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
@@ -73,34 +79,39 @@ async function StartWatcher(): Promise<boolean> {
// errorTypeCheck(error)
// );
})
.on("ready", onWatcherReady)
.on("raw", function (event, path, details) {
// This event should be triggered everytime something happens.
// console.log("Raw event info:", event, path, details);
});
.on("ready", onWatcherReady);
// .on("raw", function (event, path, details) {
// // This event should be triggered everytime something happens.
// // console.log("Raw event info:", event, path, details);
// });
return true;
}
function removeWatcherPath(path: string): void {
watcher.unwatch(path);
log.debug(`Stopped watching path: ${path}`);
if (watcher) {
watcher.unwatch(path);
log.debug(`Stopped watching path: ${path}`);
}
}
function addWatcherPath(path: string | string[]): void {
watcher.add(path);
log.debug(`Started watching path: ${path}`);
if (watcher) {
watcher.add(path);
log.debug(`Started watching path: ${path}`);
}
}
function onWatcherReady(): void {
const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set.
new Notification({
title: "Watcher Started",
body: "Newly exported estimates will be automatically uploaded.",
}).show();
log.info("Confirmed watched paths:", watcher.getWatched());
mainWindow.webContents.send(ipcTypes.toRenderer.watcher.started);
if (watcher) {
const mainWindow = BrowserWindow.getAllWindows()[0]; //TODO: Filter to only main window once a proper key has been set.
new Notification({
title: "Watcher Started",
body: "Newly exported estimates will be automatically uploaded.",
}).show();
log.info("Confirmed watched paths:", watcher.getWatched());
mainWindow.webContents.send(ipcTypes.toRenderer.watcher.started);
}
}
async function StopWatcher(): Promise<boolean> {