Detalhes do pacote

perfect-debounce

unjs17mMIT2.0.0

readme (leia-me)

perfect-debounce

npm version npm downloads bundle size install size codecov

Improved debounce function with Promise support.

Features

  • Well tested debounce implementation
  • Native Promise support
  • Avoid duplicate calls while promise is being resolved
  • Configurable trailing and leading behavior
  • Control methods

Usage

Install package:

npx nypm i perfect-debounce

Import:

import { debounce } from "perfect-debounce";

Debounce function:

const debounced = debounce(async () => {
  // Some heavy stuff
}, 25);

When calling debounced, it will wait at least for 25ms as configured before actually calling your function. This helps to avoid multiple calls.

Control Methods

The returned debounced function provides additional control methods:

  • debounced.cancel(): Cancel any pending invocation that has not yet occurred.
  • await debounced.flush(): Immediately invoke the pending function call (if any) and return its result.
  • debounced.isPending(): Returns true if there is a pending invocation waiting to be called, otherwise false.
debounced.cancel(); // Cancel any pending call
await debounced.flush(); // Immediately invoke pending call (if any)
debounced.isPending(); // Returns true if a call is pending

Example

const debounced = debounce(async (value) => {
  // Some async work
  return value * 2;
}, 100);

debounced(1);
debounced(2);
debounced(3);

// Check if a call is pending
console.log(debounced.isPending()); // true

// Immediately invoke the pending call
const result = await debounced.flush();
console.log(result); // 6

// Cancel any further pending calls
debounced.cancel();

To avoid initial wait, we can set leading: true option. It will cause function to be immediately called if there is no other call:

const debounced = debounce(
  async () => {
    // Some heavy stuff
  },
  25,
  { leading: true },
);

If executing async function takes longer than debounce value, duplicate calls will be still prevented a last call will happen. To disable this behavior, we can set trailing: false option:

const debounced = debounce(
  async () => {
    // Some heavy stuff
  },
  25,
  { trailing: false },
);

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Based on sindresorhus/p-debounce.

Made with 💛 Published under MIT License.

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v2.0.0

compare changes

🚀 Enhancements

  • Add control methods to enhance debounce (#35)

📖 Documentation

  • Fix typo for default value of trailing option in jsdocs (#19)
  • Update readme (84d3b11)
  • Fix typo for example of debounce function (#21)

📦 Build

🏡 Chore

⚠️ Breaking Changes

❤️ Contributors

v1.0.0

compare changes

🩹 Fixes

  • pkg: Add types subpath export (#14)

📖 Documentation

  • Use correct yarn command (#12)
  • Update installation guide for pnpm (#13)

🏡 Chore

  • Update renovate config (#11)
  • Update repo (46bc53a)
  • Eslint ignore coverage (17fe6cf)
  • Lint tests (18fe26a)

❤️ Contributors

0.1.3 (2022-03-16)

Bug Fixes

0.1.2 (2022-03-15)

Features

  • add default value for wait (a34162e)

0.1.1 (2022-03-15)

0.1.0 (2022-03-15)

0.0.1 (2022-03-15)

Features