npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bscotch/debounce-watch

v1.1.0

Published

Monitor files for changes, debounce events, and finally trigger consequences.

Downloads

241

Readme

Debounce Watch

⚠ In active development and subject to major change! ⚠

There are some great libraries out there for triggering code when files change, including chokidar and nodemon.

A file watcher utility that debounces file changes before triggering an event is much harder to find. That's the purpose of Debounce Watch.

📢 Announcement 📢 The deployed version of this package found on npm is from an internal fork of the public repo. The public repo will eventually be deleted. Non-Bscotch users should use the npm package for the most up-to-date version.

What's "Debouncing"?

The goal of "Debouncing" is to prevent an event-invoked action from being invoked multiple times while redundant events keep happening. In essence, once the events start we want to wait until they stop before firing off some action in response.

This is useful for cases where file system changes occur in batches but you only want subsequent actions to occur once all changes are complete (e.g. when compiling a Typescript project to JavaScript).

Nodemon and chokidar provide delay options to approximately get at this, such that the triggered action will occur that amount of time after the first event is detected. However, there is no way to guarantee that the delay will encompass all events in a batch.

How does Debounce Watch work?

Debounce Watch uses chokidar to watch for file system changes, collects a list of all changes that have occurred, and calls a function you provide on that list once the file system changes become less frequent than your debounce timeout.

Requirements

  • Available as ECMAScript Modules only, so will not work with Node.js < v13.2

Usage

Install with npm install @bscotch/debounce-watch.

Use in your code:

// (Example in Typescript)

import { debounceWatch } from '@bscotch/debounce-watch';
import type { DebouncedEventsProcessor } from '@bscotch/debounce-watch';

// Your function can be sync or async
const processDebouncedEvents: DebouncedEventsProcessor = (events) => {
  for (const event of events) {
    if (event.event == 'add') {
      // Do something related to a file being added!
    }
  }
};

const watcher = await debounceWatch('folder/of/files', processDebouncedEvents, {
  onlyFileExtensions: ['ts', 'js'],
  debounceWaitSeconds: 0.2,
  allowOverlappingRuns: true,
});