🛫 Added debounce to chokidar event listeners

This commit is contained in:
2022-04-14 14:11:36 -07:00
parent d9f6991aef
commit ff2e4531f0

View File

@@ -5,6 +5,7 @@ import path from "path";
import fs from "fs"; import fs from "fs";
import { IExtractionOptions, IFolderData } from "threetwo-ui-typings"; import { IExtractionOptions, IFolderData } from "threetwo-ui-typings";
import { SocketIOMixin } from "../mixins/socket.io.mixin"; import { SocketIOMixin } from "../mixins/socket.io.mixin";
import { debounce } from "lodash";
export const io = SocketIOMixin(); export const io = SocketIOMixin();
export default class ApiService extends Service { export default class ApiService extends Service {
@@ -123,9 +124,10 @@ export default class ApiService extends Service {
// Filewatcher // Filewatcher
const fileWatcher = chokidar.watch( const fileWatcher = chokidar.watch(
path.resolve("/comics"), path.resolve("./comics"),
{ {
ignored: (filePath) => path.extname(filePath) === '.dctmp', ignored: (filePath) =>
path.extname(filePath) === ".dctmp",
persistent: true, persistent: true,
usePolling: true, usePolling: true,
interval: 5000, interval: 5000,
@@ -175,49 +177,77 @@ export default class ApiService extends Service {
}); });
}; };
fileWatcher fileWatcher
.once("add", async (path, stats) => { .once(
console.log("Watcher detected new files."); "add",
console.log( debounce((path, stats) => {
`File ${path} has been added with stats: ${JSON.stringify( console.log("Watcher detected new files.");
stats, null, 2 console.log(
)}` `File ${path} has been added with stats: ${JSON.stringify(
); stats,
null,
console.log("File copy started..."); 2
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", console.log("File copy started...");
result: path, 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,
});
}); });
}); }, 500)
}) )
.once("change", (path, stats) => .once(
console.log( "change",
`File ${path} has been changed. Stats: ${JSON.stringify(stats, null, 2)}` debounce(
(path, stats) =>
console.log(
`File ${path} has been changed. Stats: ${JSON.stringify(
stats,
null,
2
)}`
),
500
) )
) )
.once("unlink", (path) => .once(
console.log(`File ${path} has been removed`) "unlink",
debounce(
(path) =>
console.log(
`File ${path} has been removed`
),
500
)
) )
.once("addDir", (path) => .once(
console.log(`Directory ${path} has been added`) "addDir",
debounce(
(path) =>
console.log(
`Directory ${path} has been added`
),
500
)
); );
}); });
}, },