2.0.1 • Published 6 years ago

yf-limiter v2.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
6 years ago

yf-limiter

version: 2.0.1

A limiter that prevent overflow from action / event / request ..

Installation

Node:

$ npm install yf-limiter

Example

Import

const YF = require('yf-limiter');

Debounce

let count = 1,
    debounced = YF.debounce( function(){
        console.log( 'called' )
    }, 2000 )

setInterval( function() {
    if( count < 5 ) {
        debounced();
    }
    count++;
} , 1000 );

// '', '', '', '', called ...

Throttle

let throttled = YF.throttle( function(){
        console.log( 'called' )
    }, 2000 )

setInterval( throttled , 1000 );

// called, '', called, '', called, '', called, '', called ...

or ( pass, fail ):

let throttled = YF.throttle( 2000 )

setInterval( ()=>{
    if( throttled() ) {
        console.log( 'called' )
    } else {
        console.log( '' )
    }
} , 1000 );

// called, '', called, '', called, '', called, '', called ...

Limiter

5 times function called will be allowed in every 10 second, the other would be rejected

let limited = YF.limiter( function(){
          console.log( 'called' )
     }, 5, 10 )

setInterval( limited , 1000 );

// called, called, called, called, called, '', '', '', '', '', called, called ...

or ( pass, fail ):

let limited = YF.limiter( 5, 10 )

setInterval( ()=>{
    if( limited() ) {
        console.log( 'called' )
    } else {
        console.log( '' )
    }
    
}, 1000 );

// called, called, called, called, called, '', '', '', '', '', called, called ...

or ( split the timeline, pass last argument as true to create a split limiter, and name for each function called ):

let limited = YF.limiter( function(a){
          console.log( a )
     }, 5, 10, true )

setInterval( ()=>{

    limited('1')
    if(run % 3) limited('2')
    if(run % 6 && run < 50) limited('3')
    
}, 1000 );

// [1] 1, 1, 1, 1, 1, '', '', '' ...
// [2] '', 2, 2, '', 2, 2, '', 2, '' ...
// [3] '', 3, 3, 3, 3, 3, '', '' ...

API

YF.debounce(fn, timeout)

* Debounce a function called.
* @param {function} fn // function
* @param {int} timeout // time in million second
* 
* @return {function} // the debounced function

YF.throttle(fn, timeout)

YF.throttle(timeout)

* Throttle a function called.
* @param {function} fn // function
* @param {int} timeout // time in million second
* 
* @return {function} // the throttled function

YF.limiter(fn, limit, cycle)

YF.limiter(fn, limit, cycle, split)

* Limit a function called times in a time period.
* @param {function} fn // function
* @param {int} limit // number of allowed times
* @param {int} cycle // time in second
* @param {string} split // split the timeline by name
* 
* @param {string} name // timeline name
* 
* @return {function} // the limited function

License

MIT

2.0.1

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago

1.0.0

6 years ago