62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
// Metro module 10633 (f10634).
|
|
class LeakyBucket {
|
|
constructor(a0, a1) {
|
|
this._capacity = a0;
|
|
this._tokenCount = a0;
|
|
this._queue = [];
|
|
this._intervalPeriod = a1 / a0;
|
|
this._intervalID = null;
|
|
}
|
|
_processQueue() {
|
|
setTimeout(() => {
|
|
if (this._queue.length > 0 && this._tokenCount > 0) {
|
|
this._tokenCount = this._tokenCount - 1;
|
|
if (this._intervalID == null) {
|
|
this._intervalID = setInterval(() => this._iterate(), this._intervalPeriod);
|
|
}
|
|
const r18 = this._queue.shift();
|
|
if (r18 != null) {
|
|
r18.resolve();
|
|
}
|
|
this._processQueue();
|
|
}
|
|
}, 0);
|
|
}
|
|
_iterate() {
|
|
this._tokenCount = Math.min(this._capacity, this._tokenCount + 1);
|
|
if (this._tokenCount >= this._capacity && this._intervalID != null) {
|
|
clearInterval(this._intervalID);
|
|
this._intervalID = null;
|
|
}
|
|
this._processQueue();
|
|
}
|
|
process(signal) {
|
|
return new Promise((a0, reject) => {
|
|
let c26767_1;
|
|
if (signal?.aborted) {
|
|
reject(new Error("Already aborted"));
|
|
} else {
|
|
const r7 = {
|
|
resolve: a0,
|
|
signal
|
|
};
|
|
c26767_1 = r7;
|
|
this._queue.push(r7);
|
|
if (signal) {
|
|
signal.addEventListener("abort", () => {
|
|
const r11 = this._queue.indexOf(c26767_1);
|
|
if (r11 >= 0) {
|
|
this._queue.splice(r11, 1);
|
|
}
|
|
reject(new Error("Aborted"));
|
|
}, {
|
|
once: true
|
|
});
|
|
}
|
|
this._processQueue();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
export default LeakyBucket;
|