From ff2e4531f012029c7d9fda0423bc050dd82a0986 Mon Sep 17 00:00:00 2001 From: Rishi Ghan Date: Thu, 14 Apr 2022 14:11:36 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=AB=20Added=20debounce=20to=20chokidar?= =?UTF-8?q?=20event=20listeners?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/api.service.ts | 110 +++++++++++++++++++++++++--------------- 1 file changed, 70 insertions(+), 40 deletions(-) diff --git a/services/api.service.ts b/services/api.service.ts index c935a76..e762831 100644 --- a/services/api.service.ts +++ b/services/api.service.ts @@ -5,6 +5,7 @@ import path from "path"; import fs from "fs"; import { IExtractionOptions, IFolderData } from "threetwo-ui-typings"; import { SocketIOMixin } from "../mixins/socket.io.mixin"; +import { debounce } from "lodash"; export const io = SocketIOMixin(); export default class ApiService extends Service { @@ -123,9 +124,10 @@ export default class ApiService extends Service { // Filewatcher const fileWatcher = chokidar.watch( - path.resolve("/comics"), + path.resolve("./comics"), { - ignored: (filePath) => path.extname(filePath) === '.dctmp', + ignored: (filePath) => + path.extname(filePath) === ".dctmp", persistent: true, usePolling: true, interval: 5000, @@ -175,49 +177,77 @@ export default class ApiService extends Service { }); }; fileWatcher - .once("add", async (path, stats) => { - console.log("Watcher detected new files."); - console.log( - `File ${path} has been added with stats: ${JSON.stringify( - stats, null, 2 - )}` - ); - - console.log("File copy started..."); - fs.stat(path, (err, stat) => { - if (err) { - console.log( - "Error watching file for copy completion. ERR: " + - err.message - ); - console.log( - "Error file not processed. PATH: " + - path - ); - throw err; - } - setTimeout( - checkFileCopyComplete, - fileCopyDelaySeconds * 1000, - path, - stat + .once( + "add", + debounce((path, stats) => { + console.log("Watcher detected new files."); + console.log( + `File ${path} has been added with stats: ${JSON.stringify( + stats, + null, + 2 + )}` ); - client.emit("action", { - type: "LS_COMIC_ADDED", - result: path, + + console.log("File copy started..."); + fs.stat(path, (err, stat) => { + if (err) { + console.log( + "Error watching file for copy completion. ERR: " + + err.message + ); + console.log( + "Error file not processed. PATH: " + + path + ); + throw err; + } + setTimeout( + checkFileCopyComplete, + fileCopyDelaySeconds * 1000, + path, + stat + ); + client.emit("action", { + type: "LS_COMIC_ADDED", + result: path, + }); }); - }); - }) - .once("change", (path, stats) => - console.log( - `File ${path} has been changed. Stats: ${JSON.stringify(stats, null, 2)}` + }, 500) + ) + .once( + "change", + debounce( + (path, stats) => + console.log( + `File ${path} has been changed. Stats: ${JSON.stringify( + stats, + null, + 2 + )}` + ), + 500 ) ) - .once("unlink", (path) => - console.log(`File ${path} has been removed`) + .once( + "unlink", + debounce( + (path) => + console.log( + `File ${path} has been removed` + ), + 500 + ) ) - .once("addDir", (path) => - console.log(`Directory ${path} has been added`) + .once( + "addDir", + debounce( + (path) => + console.log( + `Directory ${path} has been added` + ), + 500 + ) ); }); },