preload / watch - part 1

The change event was not being used (since
dca542e781), so also remove that dead code.
This commit is contained in:
Manav Rathi 2024-03-25 13:31:28 +05:30
parent 67468c6ff9
commit 96ea53face
No known key found for this signature in database
4 changed files with 52 additions and 40 deletions

View file

@ -1,7 +1,5 @@
import { ipcRenderer } from "electron";
import ElectronLog from "electron-log";
import path from "path";
import { getElectronFile } from "../services/fs";
import { getWatchMappings, setWatchMappings } from "../services/watch";
import { ElectronFile, WatchMapping } from "../types";
import { isMappingPresent } from "../utils/watch";
@ -87,28 +85,5 @@ export function updateWatchMappingIgnoredFiles(
setWatchMappings(watchMappings);
}
export function registerWatcherFunctions(
addFile: (file: ElectronFile) => Promise<void>,
removeFile: (path: string) => Promise<void>,
removeFolder: (folderPath: string) => Promise<void>,
) {
ipcRenderer.removeAllListeners("watch-add");
ipcRenderer.removeAllListeners("watch-change");
ipcRenderer.removeAllListeners("watch-unlink-dir");
ipcRenderer.on("watch-add", async (_, filePath: string) => {
filePath = filePath.split(path.sep).join(path.posix.sep);
await addFile(await getElectronFile(filePath));
});
ipcRenderer.on("watch-unlink", async (_, filePath: string) => {
filePath = filePath.split(path.sep).join(path.posix.sep);
await removeFile(filePath);
});
ipcRenderer.on("watch-unlink-dir", async (_, folderPath: string) => {
folderPath = folderPath.split(path.sep).join(path.posix.sep);
await removeFolder(folderPath);
});
}
export { getWatchMappings } from "../services/watch";

View file

@ -31,7 +31,6 @@ import { createWriteStream, existsSync } from "node:fs";
import * as fs from "node:fs/promises";
import { Readable } from "node:stream";
import path from "path";
import type { ElectronFile } from "./types";
import { getDirFiles } from "./api/fs";
import {
getElectronFilesFromGoogleZip,
@ -42,12 +41,12 @@ import {
import {
addWatchMapping,
getWatchMappings,
registerWatcherFunctions,
removeWatchMapping,
updateWatchMappingIgnoredFiles,
updateWatchMappingSyncedFiles,
} from "./api/watch";
import { logErrorSentry, setupLogging } from "./main/log";
import type { ElectronFile } from "./types";
setupLogging();
@ -209,6 +208,25 @@ const showUploadZipDialog = (): Promise<{
files: ElectronFile[];
}> => ipcRenderer.invoke("showUploadZipDialog");
// - Watch
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),
);
};
// - FIXME below this
/* preload: duplicated logError */
@ -433,6 +451,9 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
showUploadDirsDialog,
showUploadZipDialog,
// - Watch
registerWatcherFunctions,
// - FS
fs: {
exists: fsExists,
@ -455,7 +476,7 @@ contextBridge.exposeInMainWorld("ElectronAPIs", {
getWatchMappings,
addWatchMapping,
removeWatchMapping,
registerWatcherFunctions,
isFolder,
updateWatchMappingSyncedFiles,
updateWatchMappingIgnoredFiles,

View file

@ -1,7 +1,16 @@
import chokidar from "chokidar";
import { BrowserWindow } from "electron";
import * as path from "path";
import { getWatchMappings } from "../api/watch";
import { logError } from "../main/log";
import { getElectronFile } from "./fs";
/**
* Convert a file system {@link filePath} that uses the local system specific
* path separators into a path that uses POSIX file separators.
*/
const normalizeToPOSIX = (filePath: string) =>
filePath.split(path.sep).join(path.posix.sep);
export function initWatcher(mainWindow: BrowserWindow) {
const mappings = getWatchMappings();
@ -13,17 +22,20 @@ export function initWatcher(mainWindow: BrowserWindow) {
awaitWriteFinish: true,
});
watcher
.on("add", (path) => {
mainWindow.webContents.send("watch-add", path);
})
.on("change", (path) => {
mainWindow.webContents.send("watch-change", path);
.on("add", async (path) => {
mainWindow.webContents.send(
"watch-add",
await getElectronFile(normalizeToPOSIX(path)),
);
})
.on("unlink", (path) => {
mainWindow.webContents.send("watch-unlink", path);
mainWindow.webContents.send("watch-unlink", normalizeToPOSIX(path));
})
.on("unlinkDir", (path) => {
mainWindow.webContents.send("watch-unlink-dir", path);
mainWindow.webContents.send(
"watch-unlink-dir",
normalizeToPOSIX(path),
);
})
.on("error", (error) => {
logError(error, "error while watching files");

View file

@ -145,6 +145,15 @@ export interface ElectronAPIsType {
files: ElectronFile[];
}>;
// - Watch
registerWatcherFunctions: (
addFile: (file: ElectronFile) => Promise<void>,
removeFile: (path: string) => Promise<void>,
removeFolder: (folderPath: string) => Promise<void>,
) => void;
// - FS legacy
checkExistsAndCreateDir: (dirPath: string) => Promise<void>;
@ -182,11 +191,6 @@ export interface ElectronAPIsType {
uploadStrategy: number,
) => Promise<void>;
removeWatchMapping: (folderPath: string) => Promise<void>;
registerWatcherFunctions: (
addFile: (file: ElectronFile) => Promise<void>,
removeFile: (path: string) => Promise<void>,
removeFolder: (folderPath: string) => Promise<void>,
) => void;
isFolder: (dirPath: string) => Promise<boolean>;
moveFile: (oldPath: string, newPath: string) => Promise<void>;
deleteFolder: (path: string) => Promise<void>;