From 4455bb959536217c1c00841249af557e52dc9387 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 18 Apr 2024 18:31:09 +0530 Subject: [PATCH] API on electron side --- desktop/src/main/services/watch.ts | 6 +- desktop/src/preload.ts | 81 ++++++++++++++------------- web/apps/photos/src/services/watch.ts | 4 +- web/packages/next/types/ipc.ts | 33 ++--------- 4 files changed, 51 insertions(+), 73 deletions(-) diff --git a/desktop/src/main/services/watch.ts b/desktop/src/main/services/watch.ts index 8dad45e0e..b12913ed4 100644 --- a/desktop/src/main/services/watch.ts +++ b/desktop/src/main/services/watch.ts @@ -25,9 +25,9 @@ export const createWatcher = (mainWindow: BrowserWindow) => { }); watcher - .on("add", send("addFile")) - .on("unlink", send("removeFile")) - .on("unlinkDir", send("removeDir")) + .on("add", send("watchAddFile")) + .on("unlink", send("watchRemoveFile")) + .on("unlinkDir", send("watchRemoveDir")) .on("error", (error) => log.error("Error while watching files", error)); return watcher; diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts index cf5ded6d8..c23baef4e 100644 --- a/desktop/src/preload.ts +++ b/desktop/src/preload.ts @@ -40,9 +40,10 @@ import { contextBridge, ipcRenderer } from "electron/renderer"; // While we can't import other code, we can import types since they're just -// needed when compiling and will not be needed / looked around for at runtime. +// needed when compiling and will not be needed or looked around for at runtime. import type { AppUpdate, + CollectionMapping, ElectronFile, FolderWatch, PendingUploads, @@ -191,43 +192,40 @@ const showUploadZipDialog = (): Promise<{ // - Watch -const findFiles = (folderPath: string): Promise => - ipcRenderer.invoke("findFiles", folderPath); +const watchFindFiles = (folderPath: string): Promise => + ipcRenderer.invoke("watchFindFiles", folderPath); -const registerWatcherFunctions = ( - addFile: (file: ElectronFile) => Promise, - removeFile: (path: string) => Promise, - removeFolder: (folderPath: string) => Promise, -) => { - ipcRenderer.removeAllListeners("watch-add"); - ipcRenderer.removeAllListeners("watch-unlink"); - ipcRenderer.removeAllListeners("watch-unlink-dir"); - ipcRenderer.on("watch-add", (_, file: ElectronFile) => addFile(file)); - ipcRenderer.on("watch-unlink", (_, filePath: string) => - removeFile(filePath), - ); - ipcRenderer.on("watch-unlink-dir", (_, folderPath: string) => - removeFolder(folderPath), +const watchAdd = ( + folderPath: string, + collectionMapping: CollectionMapping, +): Promise => + ipcRenderer.invoke("watchAdd", folderPath, collectionMapping); + +const watchRemove = (folderPath: string): Promise => + ipcRenderer.invoke("watchRemove", folderPath); + +const watchGet = (): Promise => ipcRenderer.invoke("watchGet"); + +const watchOnAddFile = (f: (path: string, watch: FolderWatch) => void) => { + ipcRenderer.removeAllListeners("watchAddFile"); + ipcRenderer.on("watchAddFile", (_, path: string, watch: FolderWatch) => + f(path, watch), ); }; -const addWatchMapping = ( - collectionName: string, - folderPath: string, - uploadStrategy: number, -): Promise => - ipcRenderer.invoke( - "addWatchMapping", - collectionName, - folderPath, - uploadStrategy, +const watchOnRemoveFile = (f: (path: string, watch: FolderWatch) => void) => { + ipcRenderer.removeAllListeners("watchRemoveFile"); + ipcRenderer.on("watchRemoveFile", (_, path: string, watch: FolderWatch) => + f(path, watch), ); +}; -const removeWatchMapping = (folderPath: string): Promise => - ipcRenderer.invoke("removeWatchMapping", folderPath); - -const getWatchMappings = (): Promise => - ipcRenderer.invoke("getWatchMappings"); +const watchOnRemoveDir = (f: (path: string, watch: FolderWatch) => void) => { + ipcRenderer.removeAllListeners("watchRemoveDir"); + ipcRenderer.on("watchRemoveDir", (_, path: string, watch: FolderWatch) => + f(path, watch), + ); +}; const updateWatchMappingSyncedFiles = ( folderPath: string, @@ -265,6 +263,7 @@ const getElectronFilesFromGoogleZip = ( const getDirFiles = (dirPath: string): Promise => ipcRenderer.invoke("getDirFiles", dirPath); +// // These objects exposed here will become available to the JS code in our // renderer (the web/ code) as `window.ElectronAPIs.*` // @@ -296,8 +295,10 @@ const getDirFiles = (dirPath: string): Promise => // https://www.electronjs.org/docs/latest/api/context-bridge#methods // // The copy itself is relatively fast, but the problem with transfering large -// amounts of data is potentially running out of memory during the copy. For an -// alternative, see [Note: IPC streams]. +// amounts of data is potentially running out of memory during the copy. +// +// For an alternative, see [Note: IPC streams]. +// contextBridge.exposeInMainWorld("electron", { // - General @@ -353,12 +354,14 @@ contextBridge.exposeInMainWorld("electron", { // - Watch watch: { - findFiles, + findFiles: watchFindFiles, + add: watchAdd, + remove: watchRemove, + get: watchGet, + onAddFile: watchOnAddFile, + onRemoveFile: watchOnRemoveFile, + onRemoveDir: watchOnRemoveDir, }, - registerWatcherFunctions, - addWatchMapping, - removeWatchMapping, - getWatchMappings, updateWatchMappingSyncedFiles, updateWatchMappingIgnoredFiles, diff --git a/web/apps/photos/src/services/watch.ts b/web/apps/photos/src/services/watch.ts index b804e8235..d77352743 100644 --- a/web/apps/photos/src/services/watch.ts +++ b/web/apps/photos/src/services/watch.ts @@ -115,7 +115,7 @@ class FolderWatcher { * collection do files belonging to nested directories go to. */ async addWatch(folderPath: string, mapping: CollectionMapping) { - await ensureElectron().watcher.add(folderPath, mapping); + await ensureElectron().watch.add(folderPath, mapping); this.syncWithDisk(); } @@ -706,7 +706,7 @@ const deduceEvents = async ( for (const watch of activeWatches) { const folderPath = watch.folderPath; - const paths = (await electron.watcher.findFiles(folderPath)) + const paths = (await electron.watch.findFiles(folderPath)) // Filter out hidden files (files whose names begins with a dot) .filter((path) => !isHiddenFile(path)); diff --git a/web/packages/next/types/ipc.ts b/web/packages/next/types/ipc.ts index ff8ce3fa9..7bb8428dd 100644 --- a/web/packages/next/types/ipc.ts +++ b/web/packages/next/types/ipc.ts @@ -288,7 +288,7 @@ export interface Electron { * dragged/dropped or selected to set up the folder watch, will be referred * to as a folder when naming things. */ - watcher: { + watch: { /** * Return the paths of all the files under the given folder. * @@ -343,9 +343,7 @@ export interface Electron { * * The path is guaranteed to use POSIX separators ('/'). */ - onAddFile: ( - f: (path: string, watch: FolderWatch) => void, - ) => Promise; + onAddFile: (f: (path: string, watch: FolderWatch) => void) => void; /** * Register the function to invoke when a file is removed in one of the @@ -356,9 +354,7 @@ export interface Electron { * * The path is guaranteed to use POSIX separators ('/'). */ - onRemoveFile: ( - f: (path: string, watch: FolderWatch) => void, - ) => Promise; + onRemoveFile: (f: (path: string, watch: FolderWatch) => void) => void; /** * Register the function to invoke when a directory is removed in one of @@ -369,30 +365,9 @@ export interface Electron { * * The path is guaranteed to use POSIX separators ('/'). */ - onRemoveDir: ( - f: (path: string, watch: FolderWatch) => void, - ) => Promise; + onRemoveDir: (f: (path: string, watch: FolderWatch) => void) => void; }; - registerWatcherFunctions: ( - addFile: (file: ElectronFile) => Promise, - removeFile: (path: string) => Promise, - removeFolder: (folderPath: string) => Promise, - ) => void; - - removeWatchMapping: (folderPath: string) => Promise; - - /** - * TODO(MR): Outdated description - * Get the latest state of the watched folders. - * - * We persist the folder watches that the user has setup. This function goes - * through that list, prunes any folders that don't exist on disk anymore, - * and for each, also returns a list of files that exist in that folder. - */ - - getWatchMappings: () => Promise; - updateWatchMappingSyncedFiles: ( folderPath: string, files: FolderWatch["syncedFiles"],