From 3c072e7f9a9ae7957b2aa110d102e85f13aafc7c Mon Sep 17 00:00:00 2001 From: Oscar Otero Date: Tue, 27 Aug 2024 12:29:48 +0200 Subject: [PATCH] fixed double processing issue in the watcher --- core/watcher.ts | 58 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/core/watcher.ts b/core/watcher.ts index ff47fd80..9f6765e2 100644 --- a/core/watcher.ts +++ b/core/watcher.ts @@ -77,7 +77,7 @@ export default class FSWatcher implements Watcher { async start() { const { root, ignore, debounce } = this.options; const watcher = Deno.watchFs(root); - const changeQueue: Set[] = []; + const changes = new Set(); let timer = 0; let runningCallback = false; @@ -86,23 +86,33 @@ export default class FSWatcher implements Watcher { const callback = async () => { runningCallback = true; - let changes: Set | undefined; - while ((changes = changeQueue.pop()) !== undefined) { - try { - const result = await this.dispatchEvent({ - type: "change", - files: changes, - }); - if (false === result) { - runningCallback = false; - return watcher.close(); - } - } catch (error) { - await this.dispatchEvent({ type: "error", error }); + const files = new Set(changes); + changes.clear(); + + if (!files.size) { + runningCallback = false; + return; + } + + try { + const result = await this.dispatchEvent({ + type: "change", + files: files, + }); + if (false === result) { + runningCallback = false; + return watcher.close(); } + } catch (error) { + await this.dispatchEvent({ type: "error", error }); } runningCallback = false; + + // New changes detected while processing + if (changes.size) { + callback(); + } }; for await (const event of watcher) { @@ -124,23 +134,13 @@ export default class FSWatcher implements Watcher { continue; } - const changes = new Set(); paths.forEach((path) => changes.add(normalizePath(relative(root, path)))); - // If we're already processing and have a pending - // queue item, we can merge all future changes together - if (runningCallback && changeQueue.length > 0) { - const last = changeQueue[changeQueue.length - 1]; - changeQueue[changeQueue.length - 1] = last.union(changes); - } else { - changeQueue.unshift(changes); - - // Only start if processing queue is not already running - if (!runningCallback) { - // Debounce - clearTimeout(timer); - timer = setTimeout(callback, debounce ?? 100); - } + // Only start if processing queue is not already running + if (!runningCallback) { + // Debounce + clearTimeout(timer); + timer = setTimeout(callback, debounce ?? 100); } } }