1 | const x = Infinity;
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function LimitWaiter(options) {
|
29 | this.mLim = options ? options.minutelyLimit ? Number.isSafeInteger(options.minutelyLimit) ? options.minutelyLimit > 0 ? options.minutelyLimit : x : x : x : x;
|
30 | this.hLim = options ? options.hourlyLimit ? Number.isSafeInteger(options.hourlyLimit) ? options.hourlyLimit > 0 ? options.hourlyLimit : x : x : x : x;
|
31 | this.startWaitingCallback = options ? options.startWaitingCallback ? options.startWaitingCallback : function () { } : function () { };
|
32 | this.endWaitingCallback = options ? options.endWaitingCallback ? options.endWaitingCallback : function () { } : function () { };
|
33 | this.waitingTickCallback = options ? options.waitingTickCallback ? options.waitingTickCallback : function () { } : function () { };
|
34 | this.test = options ? options.test ? options.test : false : false;
|
35 | this.mC = 0;
|
36 | this.hC = 0;
|
37 | this.totalC = 0;
|
38 | this.callbackQueue = [];
|
39 | this.alreadyWorking = false;
|
40 | }
|
41 |
|
42 | LimitWaiter.prototype.wait = function (apiCallFunction) {
|
43 | this.callbackQueue.push(apiCallFunction);
|
44 | if (!this.alreadyWorking) {
|
45 | workOnQueueLW(this);
|
46 | }
|
47 | }
|
48 |
|
49 | let workOnQueueLW = async (ctx) => {
|
50 | while (ctx.callbackQueue.length > 0) {
|
51 | ctx.alreadyWorking = true;
|
52 | await checkWaitingLW(ctx)
|
53 | .then(() => {
|
54 | let cb = ctx.callbackQueue[0];
|
55 | ctx.callbackQueue.shift();
|
56 | cb();
|
57 |
|
58 | ctx.mC++;
|
59 | ctx.hC++;
|
60 | ctx.totalC++;
|
61 | })
|
62 | }
|
63 | ctx.alreadyWorking = false;
|
64 | }
|
65 |
|
66 | function checkWaitingLW(ctx) {
|
67 | return new Promise(function (resolve) {
|
68 | if (ctx.mC < ctx.mLim && ctx.hC < ctx.hLim) {
|
69 | resolve();
|
70 | } else if (ctx.hC >= ctx.hLim) {
|
71 | let d = new Date().getMinutes();
|
72 | let minutes = 60 - d;
|
73 | let seconds = minutes * 60;
|
74 | ctx.startWaitingCallback({
|
75 | currentCallsInAnHour: ctx.hC,
|
76 | hourlyLimit: ctx.hLim,
|
77 | secondsToWaitTilNextHour: seconds
|
78 | })
|
79 | seconds = ctx.test ? 5 : seconds;
|
80 | waitSeconds(seconds, ctx.waitingTickCallback)
|
81 | .then(() => {
|
82 | ctx.hC = ctx.mC = 0;
|
83 | ctx.endWaitingCallback({
|
84 | secondsWaited: seconds,
|
85 | callsInQueue: ctx.callbackQueue.length,
|
86 | totalCalls: ctx.totalC
|
87 | })
|
88 | resolve()
|
89 | })
|
90 | } else if (ctx.mC >= ctx.mLim) {
|
91 | let d = new Date().getSeconds();
|
92 | let seconds = 60 - d;
|
93 | ctx.startWaitingCallback({
|
94 | currentCallsInAMinute: ctx.mC,
|
95 | minutelyLimit: ctx.mLim,
|
96 | secondsToWaitTilNextMinute: seconds
|
97 | })
|
98 | seconds = ctx.test ? 5 : seconds;
|
99 | waitSeconds(seconds, ctx.waitingTickCallback)
|
100 | .then(() => {
|
101 | ctx.mC = 0;
|
102 | ctx.endWaitingCallback({
|
103 | secondsWaited: seconds,
|
104 | callsInQueue: ctx.callbackQueue.length,
|
105 | totalCalls: ctx.totalC
|
106 | })
|
107 | resolve()
|
108 | })
|
109 | }
|
110 | })
|
111 | }
|
112 |
|
113 | function waitSeconds(seconds, tickCallback) {
|
114 | return new Promise(function (resolve) {
|
115 | let count = 1;
|
116 | let v = setInterval(function () {
|
117 | tickCallback({
|
118 | secondsToWait: seconds - count,
|
119 | secondsWaited: count
|
120 | })
|
121 | count++;
|
122 | if (count > seconds) {
|
123 | clearInterval(v);
|
124 | resolve();
|
125 | }
|
126 | }, 1000)
|
127 | })
|
128 | }
|
129 |
|
130 | exports.LimitWaiter = LimitWaiter; |
\ | No newline at end of file |