Issues (33)

es/ratelimit.js (3 issues)

1 View Code Duplication
var RateLimit = /** @class */ (function () {
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
2
    function RateLimit(calls, time, callsLeft, timeLeft) {
3
        if (callsLeft === void 0) { callsLeft = calls; }
0 ignored issues
show
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
4
        if (timeLeft === void 0) { timeLeft = time; }
0 ignored issues
show
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
5
        this.originCalls = calls;
6
        this.originTime = time;
7
        this.currentCalls = callsLeft;
8
        this.currentTime = timeLeft;
9
        this.active = false;
10
    }
11
    RateLimit.prototype.reset = function () {
12
        this.currentCalls = this.originCalls;
13
        this.currentTime = this.originTime;
14
    };
15
    RateLimit.prototype.decrementCall = function () { this.currentCalls--; };
16
    RateLimit.prototype.decrementTime = function () { this.currentTime--; };
17
    RateLimit.prototype.getOriginalCalls = function () { return this.originCalls; };
18
    RateLimit.prototype.getOriginalTime = function () { return this.originTime; };
19
    RateLimit.prototype.getCallsLeft = function () { return this.currentCalls; };
20
    RateLimit.prototype.getTimeLeft = function () { return this.currentTime; };
21
    RateLimit.prototype.isActive = function () { return this.active; };
22
    RateLimit.prototype.setActive = function (active) {
23
        this.active = active;
24
    };
25
    RateLimit.prototype.startTimer = function () {
26
        var _this = this;
27
        this.setActive(true);
28
        this.timer = setInterval(function () {
29
            _this.decrementTime();
30
            if (_this.getTimeLeft() <= 0) {
31
                _this.breakTimer();
32
            }
33
        }, 1000);
34
    };
35
    RateLimit.prototype.breakTimer = function () {
36
        this.reset();
37
        this.setActive(false);
38
        clearInterval(this.timer);
39
    };
40
    RateLimit.prototype.restartTimer = function () {
41
        this.breakTimer();
42
        this.startTimer();
43
    };
44
    return RateLimit;
45
}());
46
export { RateLimit };
47
//# sourceMappingURL=ratelimit.js.map