The remaining 2

This commit is contained in:
Manav Rathi 2024-04-18 21:13:31 +05:30
parent 76053c4029
commit a9ccec6398
No known key found for this signature in database
4 changed files with 79 additions and 71 deletions

View file

@ -59,12 +59,12 @@ import {
setPendingUploadFiles,
} from "./services/upload";
import {
updateWatchMappingIgnoredFiles,
updateWatchMappingSyncedFiles,
watchAdd,
watchFindFiles,
watchGet,
watchRemove,
watchUpdateIgnoredFiles,
watchUpdateSyncedFiles,
} from "./services/watch";
import { openDirectory, openLogDirectory } from "./util";
@ -236,19 +236,19 @@ export const attachFSWatchIPCHandlers = (watcher: FSWatcher) => {
watchRemove(watcher, folderPath),
);
ipcMain.handle(
"watchUpdateSyncedFiles",
(_, syncedFiles: FolderWatch["syncedFiles"], folderPath: string) =>
watchUpdateSyncedFiles(syncedFiles, folderPath),
);
ipcMain.handle(
"watchUpdateIgnoredFiles",
(_, ignoredFiles: FolderWatch["ignoredFiles"], folderPath: string) =>
watchUpdateIgnoredFiles(ignoredFiles, folderPath),
);
ipcMain.handle("watchFindFiles", (_, folderPath: string) =>
watchFindFiles(folderPath),
);
ipcMain.handle(
"updateWatchMappingSyncedFiles",
(_, folderPath: string, files: FolderWatch["syncedFiles"]) =>
updateWatchMappingSyncedFiles(folderPath, files),
);
ipcMain.handle(
"updateWatchMappingIgnoredFiles",
(_, folderPath: string, files: FolderWatch["ignoredFiles"]) =>
updateWatchMappingIgnoredFiles(folderPath, files),
);
};

View file

@ -100,6 +100,8 @@ export const watchAdd = async (
setFolderWatches(watches);
watcher.add(folderPath);
return watches;
};
export const watchRemove = async (watcher: FSWatcher, folderPath: string) => {
@ -114,39 +116,33 @@ export const watchRemove = async (watcher: FSWatcher, folderPath: string) => {
return filtered;
};
export function updateWatchMappingSyncedFiles(
export const watchUpdateSyncedFiles = (
syncedFiles: FolderWatch["syncedFiles"],
folderPath: string,
files: FolderWatch["syncedFiles"],
): void {
const watchMappings = getWatchMappings();
const watchMapping = watchMappings.find(
(mapping) => mapping.folderPath === folderPath,
) => {
setFolderWatches(
folderWatches().map((watch) => {
if (watch.folderPath == folderPath) {
watch.syncedFiles = syncedFiles;
}
return watch;
}),
);
};
if (!watchMapping) {
throw Error(`Watch mapping not found`);
}
watchMapping.syncedFiles = files;
setWatchMappings(watchMappings);
}
export function updateWatchMappingIgnoredFiles(
export const watchUpdateIgnoredFiles = (
ignoredFiles: FolderWatch["ignoredFiles"],
folderPath: string,
files: FolderWatch["ignoredFiles"],
): void {
const watchMappings = getWatchMappings();
const watchMapping = watchMappings.find(
(mapping) => mapping.folderPath === folderPath,
) => {
setFolderWatches(
folderWatches().map((watch) => {
if (watch.folderPath == folderPath) {
watch.ignoredFiles = ignoredFiles;
}
return watch;
}),
);
if (!watchMapping) {
throw Error(`Watch mapping not found`);
}
watchMapping.ignoredFiles = files;
setWatchMappings(watchMappings);
}
};
export const watchFindFiles = async (dirPath: string) => {
const items = await fs.readdir(dirPath, { withFileTypes: true });

View file

@ -192,16 +192,28 @@ const showUploadZipDialog = (): Promise<{
// - Watch
const watchGet = (): Promise<FolderWatch[]> => ipcRenderer.invoke("watchGet");
const watchAdd = (
folderPath: string,
collectionMapping: CollectionMapping,
): Promise<void> =>
): Promise<FolderWatch[]> =>
ipcRenderer.invoke("watchAdd", folderPath, collectionMapping);
const watchRemove = (folderPath: string): Promise<void> =>
const watchRemove = (folderPath: string): Promise<FolderWatch[]> =>
ipcRenderer.invoke("watchRemove", folderPath);
const watchGet = (): Promise<FolderWatch[]> => ipcRenderer.invoke("watchGet");
const watchUpdateSyncedFiles = (
syncedFiles: FolderWatch["syncedFiles"],
folderPath: string,
): Promise<void> =>
ipcRenderer.invoke("watchUpdateSyncedFiles", syncedFiles, folderPath);
const watchUpdateIgnoredFiles = (
ignoredFiles: FolderWatch["ignoredFiles"],
folderPath: string,
): Promise<void> =>
ipcRenderer.invoke("watchUpdateIgnoredFiles", ignoredFiles, folderPath);
const watchOnAddFile = (f: (path: string, watch: FolderWatch) => void) => {
ipcRenderer.removeAllListeners("watchAddFile");
@ -227,18 +239,6 @@ const watchOnRemoveDir = (f: (path: string, watch: FolderWatch) => void) => {
const watchFindFiles = (folderPath: string): Promise<string[]> =>
ipcRenderer.invoke("watchFindFiles", folderPath);
const updateWatchMappingSyncedFiles = (
folderPath: string,
files: FolderWatch["syncedFiles"],
): Promise<void> =>
ipcRenderer.invoke("updateWatchMappingSyncedFiles", folderPath, files);
const updateWatchMappingIgnoredFiles = (
folderPath: string,
files: FolderWatch["ignoredFiles"],
): Promise<void> =>
ipcRenderer.invoke("updateWatchMappingIgnoredFiles", folderPath, files);
// - Upload
const pendingUploads = (): Promise<PendingUploads | undefined> =>
@ -361,9 +361,9 @@ contextBridge.exposeInMainWorld("electron", {
onRemoveFile: watchOnRemoveFile,
onRemoveDir: watchOnRemoveDir,
findFiles: watchFindFiles,
updateSyncedFiles: watchUpdateSyncedFiles,
updateIgnoredFiles: watchUpdateIgnoredFiles,
},
updateWatchMappingSyncedFiles,
updateWatchMappingIgnoredFiles,
// - Upload

View file

@ -308,19 +308,41 @@ export interface Electron {
*
* @param collectionMapping Determines how nested directories (if any)
* get mapped to Ente collections.
*
* @returns The updated list of watches.
*/
add: (
folderPath: string,
collectionMapping: CollectionMapping,
) => Promise<void>;
) => Promise<FolderWatch[]>;
/**
* Remove the pre-existing watch for the given {@link folderPath}.
*
* Persist this removal, and also stop listening for file system events
* that happen within the {@link folderPath}.
*
* @returns The updated list of watches.
*/
remove: (folderPath: string) => Promise<void>;
remove: (folderPath: string) => Promise<FolderWatch[]>;
/**
* Update the list of synced files for the folder watch associated
* with the given {@link folderPath}.
*/
updateSyncedFiles: (
syncedFiles: FolderWatch["syncedFiles"],
folderPath: string,
) => Promise<void>;
/**
* Update the list of ignored file paths for the folder watch
* associated with the given {@link folderPath}.
*/
updateIgnoredFiles: (
ignoredFiles: FolderWatch["ignoredFiles"],
folderPath: string,
) => Promise<void>;
/**
* Register the function to invoke when a file is added in one of the
@ -368,16 +390,6 @@ export interface Electron {
findFiles: (folderPath: string) => Promise<string[]>;
};
updateWatchMappingSyncedFiles: (
folderPath: string,
files: FolderWatch["syncedFiles"],
) => Promise<void>;
updateWatchMappingIgnoredFiles: (
folderPath: string,
files: FolderWatch["ignoredFiles"],
) => Promise<void>;
// - Upload
/**