add / remove files from watcher when mapping added / removed

This commit is contained in:
Rushikesh Tote 2022-06-04 12:42:21 +05:30
parent f919f928a2
commit e041867042
4 changed files with 31 additions and 30 deletions

View file

@ -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);
}

View file

@ -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,

View file

@ -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);
});
}

View file

@ -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) => {