API on electron side

This commit is contained in:
Manav Rathi 2024-04-18 18:31:09 +05:30
parent 275b763648
commit 4455bb9595
No known key found for this signature in database
4 changed files with 51 additions and 73 deletions

View file

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

View file

@ -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<string[]> =>
ipcRenderer.invoke("findFiles", folderPath);
const watchFindFiles = (folderPath: string): Promise<string[]> =>
ipcRenderer.invoke("watchFindFiles", folderPath);
const registerWatcherFunctions = (
addFile: (file: ElectronFile) => Promise<void>,
removeFile: (path: string) => Promise<void>,
removeFolder: (folderPath: string) => Promise<void>,
) => {
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<void> =>
ipcRenderer.invoke("watchAdd", folderPath, collectionMapping);
const watchRemove = (folderPath: string): Promise<void> =>
ipcRenderer.invoke("watchRemove", folderPath);
const watchGet = (): Promise<FolderWatch[]> => 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<void> =>
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<void> =>
ipcRenderer.invoke("removeWatchMapping", folderPath);
const getWatchMappings = (): Promise<FolderWatch[]> =>
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<ElectronFile[]> =>
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<ElectronFile[]> =>
// 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,

View file

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

View file

@ -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<void>;
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<void>;
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<void>;
onRemoveDir: (f: (path: string, watch: FolderWatch) => void) => void;
};
registerWatcherFunctions: (
addFile: (file: ElectronFile) => Promise<void>,
removeFile: (path: string) => Promise<void>,
removeFolder: (folderPath: string) => Promise<void>,
) => void;
removeWatchMapping: (folderPath: string) => Promise<void>;
/**
* 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<FolderWatch[]>;
updateWatchMappingSyncedFiles: (
folderPath: string,
files: FolderWatch["syncedFiles"],