dbounce.js

Basic thread-wide de-bounce helper
git clone git://git.finwo.net/lib/dbounce.js
Log | Files | Refs | README

README.md (1648B)


      1 # dbounce
      2 
      3 Basic de-bounce helper
      4 
      5 > **Please Note:** Due to the very limited scope of this module, I do not anticipate needing to make very many changes to it.  Expect long stretches of zero updates—that does not mean that the module is outdated.
      6 
      7 ## Motivation
      8 
      9 There are multitudes of ways to handle de-bouncing in most frameworks. This
     10 library is my way of trying to make de-bouncing calls in my projects consistent,
     11 regardless of the rest of the software stack used.
     12 
     13 ## Installation
     14 
     15 Use your favorite package manager to install it from [npmjs](https://npmjs.com).
     16 I'll be using `npm` in the examples in this readme.
     17 
     18 ```bash
     19 npm install --save dbounce
     20 ```
     21 
     22 ## Usage
     23 
     24 ```javascript
     25 
     26 // Use the import syntax
     27 import { dbounce } from 'dbounce';
     28 
     29 // Or use commonjs import
     30 const { dbounce } = require('dbounce');
     31 
     32 // Debounced function
     33 async function callHandler(arg) {
     34   if (!await dbounce(`callHandler|${arg}`)) return;
     35   console.log('TRIGGERED');
     36 }
     37 
     38 // Call it a bunch of times
     39 // Results in a single "TRIGGERED"
     40 for(let i=0; i<10; i++) {
     41   callHandler('Same-argument');
     42 }
     43 
     44 // Calling with different arguments will trigger all of them
     45 for(let i=0; i<10; i++) {
     46   callHandler(i);
     47 }
     48 
     49 // You can also choose a custom debounce ttl
     50 // Will only trigger after 2 seconds in this case
     51 async function slowHandler() {
     52   if (!await dbounce('slowHandler', 2000)) return;
     53   // Your debounced code here
     54 }
     55 
     56 ```
     57 
     58 ## Contributing
     59 
     60 Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
     61 
     62 Please make sure to update tests as appropriate.
     63 
     64 ## License
     65 
     66 [MIT](https://choosealicense.com/licenses/mit/)