71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
// Metro module 6146 (f6147).
|
|
import Logger from "../modules/debug/Logger";
|
|
import Deque from "../_bundle/Deque";
|
|
const logger = new Logger("Queue");
|
|
class Queue {
|
|
constructor(logger2 = logger, defaultRetryAfter = 100) {
|
|
const r16 = {
|
|
__proto__: new.target.prototype,
|
|
queue: new Deque(),
|
|
timeout: null,
|
|
draining: false
|
|
};
|
|
r16.logger = logger2;
|
|
r16.defaultRetryAfter = defaultRetryAfter;
|
|
return r16;
|
|
}
|
|
enqueue(a0, a1, a2) {
|
|
this.queue.push({
|
|
message: a0,
|
|
success: a1,
|
|
logId: a2
|
|
});
|
|
this._drainIfNecessary();
|
|
}
|
|
get length() {
|
|
return this.queue.length;
|
|
}
|
|
_drainIfNecessary() {
|
|
let logId, c15509_1, success;
|
|
if (this.timeout === null && this.queue.length !== 0 && this.draining !== true) {
|
|
this.draining = true;
|
|
const r17 = this.queue.shift();
|
|
c15509_1 = r17;
|
|
const message = r17.message;
|
|
success = r17.success;
|
|
logId = r17.logId;
|
|
this.logger.log(`Draining message from queue LogId:${logId} QueueLength: ${this.queue.length}`);
|
|
this.drain(message, (a0, a1) => {
|
|
this.logger.log(`Finished draining message from queue LogId:${logId} QueueLength: ${this.queue.length}`);
|
|
this.draining = false;
|
|
if (a0 == null) {
|
|
setImmediate(() => this._drainIfNecessary());
|
|
try {
|
|
success(a1);
|
|
} catch ($e) {
|
|
this.logger.error("", $e);
|
|
}
|
|
} else {
|
|
let retryAfter = a0.retryAfter;
|
|
if (retryAfter == null) {
|
|
retryAfter = this.defaultRetryAfter;
|
|
}
|
|
this.logger.info(`Rate limited. Delaying draining of queue for ${retryAfter} ms. LogId:${logId} QueueLength: ${this.queue.length}`);
|
|
this.timeout = setTimeout(() => {
|
|
this.queue.unshift(c15509_1);
|
|
this.timeout = null;
|
|
this._drainIfNecessary();
|
|
}, retryAfter);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
clear() {
|
|
this.queue.clear();
|
|
clearTimeout(this.timeout);
|
|
this.timeout = null;
|
|
this.draining = false;
|
|
}
|
|
}
|
|
export default Queue;
|