From 05ef22c1a564a6e28fb2f98fb29ca05b1c9752c3 Mon Sep 17 00:00:00 2001 From: Gili Tzabari Date: Tue, 5 Dec 2023 13:42:21 -0500 Subject: [PATCH] Update multiple files at once in watch mode --- build/Project.mts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/build/Project.mts b/build/Project.mts index 80b5e59..dc3d9e2 100644 --- a/build/Project.mts +++ b/build/Project.mts @@ -21,6 +21,7 @@ import eslintConfig from "../.eslintrc.mjs"; import {Logger} from "winston"; import {mode} from "./mode.mjs"; import parseArgs from "minimist"; +import _ from "lodash"; class Project @@ -293,7 +294,7 @@ class Project console.timeEnd("bundleResources"); } - public async test() + private async test() { console.time("test"); const binPath = path.posix.resolve("./node_modules/.bin"); @@ -359,12 +360,29 @@ class Project private async watchFiles(paths: string | ReadonlyArray, callback: (sources: string[]) => Promise): Promise { + const changes: Set = new Set(); + const project = this; + + async function processUpdate() + { + const filesToProcess = new Set(changes); + const posixPaths = []; + for (const changed of filesToProcess) + { + changes.delete(changed); + // Use POSIX paths across all platforms + const posixPath = changed.split(path.posix.sep).join(path.posix.sep); + posixPaths.push(posixPath); + } + project.log.info(`Updating: [${Array.from(filesToProcess).join(", ")}]`); + await callback(posixPaths); + } + + const queueUpdate = _.debounce(processUpdate, 500); const onUpdate = async (changed: string, stats: fs.Stats) => { - this.log.info(`Updating: ${changed}`); - // Use POSIX paths across all platforms - const posixPath = changed.split(path.posix.sep).join(path.posix.sep); - await callback([posixPath]); + changes.add(changed); + await queueUpdate(); }; chokidar.watch(paths).on("add", onUpdate). on("addDir", onUpdate).