limiter-component
Advanced tools
Comparing version 1.0.1 to 1.1.0
85
index.js
@@ -1,20 +0,43 @@ | ||
module.exports = limiter; | ||
/*global setTimeout, clearTimeout */ | ||
function limiter(interval, penaltyInterval = 5 * interval) { | ||
let queue = []; | ||
let lastTrigger = 0; | ||
let penaltyCounter = 0; | ||
let skipCounter = 0; | ||
let timer; | ||
function limiter(interval, penaltyInterval) { | ||
return { | ||
trigger, | ||
penalty, | ||
skip, | ||
cancel | ||
}; | ||
var queue = [], | ||
lastTrigger = 0, | ||
penaltyCounter = 0, | ||
skipCounter = 0, | ||
timer; | ||
function trigger(fn) { | ||
if (since() >= currentInterval() && !queue.length) { | ||
runNow(fn); | ||
} else { | ||
queue.push(fn); | ||
schedule(); | ||
} | ||
} | ||
function now() { | ||
return Date.now(); | ||
function penalty() { | ||
penaltyCounter += 1; | ||
} | ||
function skip() { | ||
skipCounter += 1; | ||
} | ||
function cancel() { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
queue = []; | ||
} | ||
function since() { | ||
return now() - lastTrigger; | ||
return Date.now() - lastTrigger; | ||
} | ||
@@ -33,6 +56,5 @@ | ||
skipCounter = 0; | ||
} else { | ||
lastTrigger = Date.now(); | ||
} | ||
else { | ||
lastTrigger = now(); | ||
} | ||
} | ||
@@ -49,5 +71,4 @@ | ||
function schedule() { | ||
var delay; | ||
if (!timer && queue.length) { | ||
delay = currentInterval() - since(); | ||
const delay = currentInterval() - since(); | ||
if (delay < 0) { | ||
@@ -59,34 +80,2 @@ return deque(); | ||
} | ||
function trigger(fn) { | ||
if (since() >= currentInterval() && !queue.length) { | ||
runNow(fn); | ||
} else { | ||
queue.push(fn); | ||
schedule(); | ||
} | ||
} | ||
function penalty() { | ||
penaltyCounter += 1; | ||
} | ||
function skip() { | ||
skipCounter += 1; | ||
} | ||
function cancel() { | ||
if (timer) { | ||
clearTimeout(timer); | ||
} | ||
queue = []; | ||
} | ||
penaltyInterval = penaltyInterval || 5 * interval; | ||
return { | ||
trigger: trigger, | ||
penalty: penalty, | ||
skip: skip, | ||
cancel: cancel | ||
}; | ||
} |
{ | ||
"name": "limiter-component", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Limits the rate of function calls to one per period. It delays but does not throttle the calls.", | ||
@@ -17,3 +17,3 @@ "scripts": { | ||
"devDependencies": { | ||
"mocha": "~3", | ||
"mocha": "~10", | ||
"jshint": "~2" | ||
@@ -20,0 +20,0 @@ }, |
[![NPM version][npm-image]][npm-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Dependency Status][gemnasium-image]][gemnasium-url] | ||
[![Build Status][build-image]][build-url] | ||
[![Dependency Status][deps-image]][deps-url] | ||
@@ -77,9 +77,9 @@ # limiter | ||
[npm-image]: https://img.shields.io/npm/v/limiter-component.svg | ||
[npm-image]: https://img.shields.io/npm/v/limiter-component | ||
[npm-url]: https://npmjs.org/package/limiter-component | ||
[travis-url]: https://travis-ci.org/pirxpilot/limiter | ||
[travis-image]: https://img.shields.io/travis/pirxpilot/limiter.svg | ||
[build-url]: https://github.com/pirxpilot/limiter/actions/workflows/check.yaml | ||
[build-image]: https://img.shields.io/github/workflow/status/pirxpilot/limiter/check | ||
[gemnasium-image]: https://img.shields.io/gemnasium/pirxpilot/limiter.svg | ||
[gemnasium-url]: https://gemnasium.com/pirxpilot/limiter | ||
[deps-image]: https://img.shields.io/librariesio/release/npm/limiter-component | ||
[deps-url]: https://libraries.io/npm/limiter-component |
5127
4
67