2 constructor(capacity, refillRate) {
3 if (typeof refillRate !== 'function') {
4 const _refillRate = refillRate;
5 refillRate = () => _refillRate;
7 if (typeof capacity !== 'function') {
8 const _capacity = capacity;
9 capacity = () => _capacity;
12 this.capacity = capacity;
13 this.refillRate = refillRate;
14 this.count = capacity();
15 this.lastRefill = Date.now();
19 const now = Date.now();
20 const delta = Math.floor(
21 (now - this.lastRefill) / 1000 * this.refillRate()
24 this.count = Math.min(this.capacity(), this.count + delta);
25 this.lastRefill = now;
28 if (this.count === 0) {
37 export { TokenBucket };