Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Watching the filesystem example: debounce skips events #109

Closed
c-antin opened this issue Jan 26, 2023 · 2 comments
Closed

Watching the filesystem example: debounce skips events #109

c-antin opened this issue Jan 26, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@c-antin
Copy link

c-antin commented Jan 26, 2023

The debounce example here is somewhat misleading.
There is a chance that events get skipped, because debounce aborts prior function calls.

A better approach would be to collect the events:

import { debounce } from "https://deno.land/std@0.174.0/async/debounce.ts";

const collector: Deno.FsEvent[] = [];
const collectDebounce = debounce(() => {
  const events = collector.splice(0, Infinity);
  console.log(events);
}, 2000);
const collect = (event: Deno.FsEvent) => {
  collector.push(event);
  collectDebounce();
};

const watcher = Deno.watchFs("./");

for await (const event of watcher) {
  collect(event);
}

This affects the following examples:
https://examples.deno.land/watching-files
https://deno.land/std@0.174.0/async/debounce.ts?s=debounce#example_0

@c-antin c-antin added the bug Something isn't working label Jan 26, 2023
@lino-levan
Copy link
Contributor

Two unrelated notes:

I agree that the way the example is currently written, one might be misled into missing events. What this example was intended to communicate is that you should not be relying on filesystem events directly to do hot reloading of files. You should instead be using a (debounced) event to do the reload whatever you need to reload. If we were to change this example, I would borderline recommend using Deno's --watch flag (which is what fresh does) as an alternative to trying to hack something together with watchFs

@kt3k kt3k transferred this issue from denoland/std Nov 15, 2023
@kt3k
Copy link
Member

kt3k commented Nov 15, 2023

The events from Deno.watchFs are usually very verbose. I think the existing example is fine because this is what most users of Deno.watchFs need to do.

(For example, single file save generates 3 events with the same payload (at least on my machine). Most users probably would want to ignore first 2 events.)

@kt3k kt3k closed this as not planned Won't fix, can't repro, duplicate, stale Nov 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants