UNPKG

@el3um4s/ipc-for-electron-chokidar

Version:

Allow the renderer to use chokidar (Minimal and efficient cross-platform file watching library)

65 lines (64 loc) 3 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const nameAPI = "chokidar"; const defaultApiKey = "ipc"; const folderChanged = (options) => __awaiter(void 0, void 0, void 0, function* () { const { callback } = options; const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || defaultApiKey; const api = globalThis[apiKey][nameAPI]; return new Promise((resolve) => { api.receive("folderChanged", (data) => { const { path, eventName, nameWatcher } = data; if (callback) { callback({ path, eventName, nameWatcher }); } resolve({ path, eventName, nameWatcher }); }); }); }); const watchFolder = (options) => __awaiter(void 0, void 0, void 0, function* () { const { callback, folderPath, nameWatcher } = options; const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || defaultApiKey; const api = globalThis[apiKey][nameAPI]; api.send("watchFolder", { folderPath, nameWatcher }); return folderChanged({ callback, apiKey }); }); const fileChanged = (options) => __awaiter(void 0, void 0, void 0, function* () { const { callback } = options; const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || defaultApiKey; const api = globalThis[apiKey][nameAPI]; return new Promise((resolve) => { api.receive("fileChanged", (data) => { const { path, eventName, nameWatcher } = data; if (callback) { callback({ path, eventName, nameWatcher }); } resolve({ path, eventName, nameWatcher }); }); }); }); const watchFile = (options) => __awaiter(void 0, void 0, void 0, function* () { const { callback, folderPath, nameWatcher } = options; const apiKey = (options === null || options === void 0 ? void 0 : options.apiKey) || defaultApiKey; const api = globalThis[apiKey][nameAPI]; api.send("watchFile", { folderPath, nameWatcher }); return fileChanged({ callback, apiKey }); }); const renderer = { watchFolder, watchFile, on: { folderChanged, fileChanged, }, }; exports.default = renderer;