From e0418670422906e72899109a888f664e2b0f323a Mon Sep 17 00:00:00 2001 From: Rushikesh Tote Date: Sat, 4 Jun 2022 12:42:21 +0530 Subject: [PATCH] add / remove files from watcher when mapping added / removed --- src/main.ts | 4 ++-- src/preload.ts | 6 +++--- src/utils/ipcComms.ts | 20 ++++++++++++++++++-- src/utils/watch.ts | 31 ++++++++----------------------- 4 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9f9217c14..483a5eb43 100644 --- a/src/main.ts +++ b/src/main.ts @@ -74,8 +74,8 @@ if (!gotTheLock) { tray.setToolTip('ente'); tray.setContextMenu(buildContextMenu(mainWindow)); - setupIpcComs(tray, mainWindow); - initWatcher(mainWindow); + const watcher = initWatcher(mainWindow); + setupIpcComs(tray, mainWindow, watcher); if (!isDev) { AppUpdater.checkForUpdate(tray, mainWindow); } diff --git a/src/preload.ts b/src/preload.ts index 9b942d4b1..954f1d712 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -13,13 +13,13 @@ import { ElectronFile } from './types'; import { getPosixFilePathsFromDir, getWatchMappings, - updateFilesInWatchMapping, initWatcher, registerWatcherFunctions, setWatchMappings, addWatchMapping, removeWatchMapping, } from './utils/watch'; +import path from 'path'; const { ipcRenderer } = electron; @@ -159,7 +159,8 @@ const showUploadZipDialog = async () => { const selectFolder = async () => { try { - const folderPath = await ipcRenderer.invoke('select-folder'); + let folderPath: string = await ipcRenderer.invoke('select-folder'); + folderPath = folderPath.split(path.sep).join(path.posix.sep); return folderPath; } catch (e) { logError(e, 'error while selecting folder'); @@ -194,7 +195,6 @@ windowObject['ElectronAPIs'] = { getPosixFilePathsFromDir, selectFolder, getWatchMappings, - updateFilesInWatchMapping, setWatchMappings, initWatcher, addWatchMapping, diff --git a/src/utils/ipcComms.ts b/src/utils/ipcComms.ts index 4e21d18ce..94e965c33 100644 --- a/src/utils/ipcComms.ts +++ b/src/utils/ipcComms.ts @@ -3,10 +3,12 @@ import { createWindow } from './createWindow'; import { buildContextMenu } from './menuUtil'; import { logErrorSentry } from './sentry'; import { getFilesFromDir } from './upload'; +import chokidar from 'chokidar'; export default function setupIpcComs( tray: Tray, - mainWindow: BrowserWindow + mainWindow: BrowserWindow, + watcher: chokidar.FSWatcher ): void { ipcMain.handle('select-dir', async () => { const result = await dialog.showOpenDialog({ @@ -64,7 +66,21 @@ export default function setupIpcComs( return files; }); - ipcMain.handle('log-error', (_, err, msg, info?) => { + ipcMain.handle( + 'add-watcher', + async (_: Electron.IpcMainEvent, args: { dir: string }) => { + watcher.add(args.dir); + } + ); + + ipcMain.handle( + 'remove-watcher', + async (_: Electron.IpcMainEvent, args: { dir: string }) => { + watcher.unwatch(args.dir); + } + ); + + ipcMain.handle('log-error', (event, err, msg, info?) => { logErrorSentry(err, msg, info); }); } diff --git a/src/utils/watch.ts b/src/utils/watch.ts index 1ed7c7aeb..638cb682a 100644 --- a/src/utils/watch.ts +++ b/src/utils/watch.ts @@ -1,4 +1,3 @@ -import * as fs from 'promise-fs'; import path from 'path'; import chokidar from 'chokidar'; import { watchStore } from '../services/store'; @@ -26,6 +25,10 @@ export async function addWatchMapping( return; } + await ipcRenderer.invoke('add-watcher', { + dir: folderPath, + }); + watchMappings.push({ collectionName, folderPath, @@ -45,6 +48,10 @@ export async function removeWatchMapping(collectionName: string) { return; } + await ipcRenderer.invoke('remove-watcher', { + dir: watchMapping.folderPath, + }); + watchMappings.splice(watchMappings.indexOf(watchMapping), 1); setWatchMappings(watchMappings); @@ -65,28 +72,6 @@ export async function getPosixFilePathsFromDir(dirPath: string) { return files; } -export async function updateFilesInWatchMapping( - collectionName: string, - files: { path: string; id: number }[] -) { - try { - const mappings = getWatchMappings(); - const mapping = mappings.find( - (m) => m.collectionName === collectionName - ); - if (mapping) { - mapping.files = files; - } else { - throw new Error( - `No mapping found for collection ${collectionName}` - ); - } - watchStore.set('mappings', mappings); - } catch (e) { - logError(e, 'error while updating watch mappings'); - } -} - export function initWatcher(mainWindow: BrowserWindow) { const mappings = getWatchMappings(); const folderPaths = mappings.map((mapping) => {