48 lines
7.1 KiB
JavaScript
48 lines
7.1 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const events_1 = require("events");
|
|
const promisify_event_1 = __importDefault(require("promisify-event"));
|
|
const time_limit_promise_1 = __importDefault(require("time-limit-promise"));
|
|
const REMOTE_REDIRECT_TIMEOUT = 10000;
|
|
const ADDING_CONNECTION_WAITING_TIMEOUT = 10000;
|
|
class RemotesQueue {
|
|
constructor() {
|
|
this.events = new events_1.EventEmitter();
|
|
this.shiftingTimeout = Promise.resolve();
|
|
this.pendingConnections = {};
|
|
}
|
|
add(remoteConnection) {
|
|
const connectionReadyPromise = (0, promisify_event_1.default)(remoteConnection, 'ready')
|
|
.then(() => this.remove(remoteConnection));
|
|
this.pendingConnections[remoteConnection.id] = {
|
|
connection: remoteConnection,
|
|
readyPromise: connectionReadyPromise,
|
|
};
|
|
this.events.emit('connection-added', remoteConnection.id);
|
|
}
|
|
remove(remoteConnection) {
|
|
delete this.pendingConnections[remoteConnection.id];
|
|
}
|
|
shift() {
|
|
const shiftingPromise = this.shiftingTimeout
|
|
.then(async () => {
|
|
let headId = Object.keys(this.pendingConnections)[0];
|
|
if (!headId)
|
|
headId = await (0, time_limit_promise_1.default)((0, promisify_event_1.default)(this.events, 'connection-added'), ADDING_CONNECTION_WAITING_TIMEOUT);
|
|
return headId ? this.pendingConnections[headId].connection : null;
|
|
});
|
|
this.shiftingTimeout = shiftingPromise
|
|
.then(connection => {
|
|
if (!connection)
|
|
return Promise.resolve();
|
|
return (0, time_limit_promise_1.default)(this.pendingConnections[connection.id].readyPromise, REMOTE_REDIRECT_TIMEOUT);
|
|
});
|
|
return shiftingPromise;
|
|
}
|
|
}
|
|
exports.default = RemotesQueue;
|
|
module.exports = exports.default;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJma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AgICAgICAgICAgaWYgKCFoZWFkSWQpXG4gICAgICAgICAgICAgICAgICAgIGhlYWRJZCA9IGF3YWl0IGdldFRpbWVMaW1pdGVkUHJvbWlzZShwcm9taXNpZnlFdmVudCh0aGlzLmV2ZW50cywgJ2Nvbm5lY3Rpb24tYWRkZWQnKSwgQURESU5HX0NPTk5FQ1RJT05fV0FJVElOR19USU1FT1VUKTtcblxuICAgICAgICAgICAgICAgIHJldHVybiBoZWFkSWQgPyB0aGlzLnBlbmRpbmdDb25uZWN0aW9uc1toZWFkSWRdLmNvbm5lY3Rpb24gOiBudWxsO1xuICAgICAgICAgICAgfSk7XG5cbiAgICAgICAgdGhpcy5zaGlmdGluZ1RpbWVvdXQgPSBzaGlmdGluZ1Byb21pc2VcbiAgICAgICAgICAgIC50aGVuKGNvbm5lY3Rpb24gPT4ge1xuICAgICAgICAgICAgICAgIGlmICghY29ubmVjdGlvbilcbiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIFByb21pc2UucmVzb2x2ZSgpO1xuXG4gICAgICAgICAgICAgICAgcmV0dXJuIGdldFRpbWVMaW1pdGVkUHJvbWlzZSh0aGlzLnBlbmRpbmdDb25uZWN0aW9uc1tjb25uZWN0aW9uLmlkXS5yZWFkeVByb21pc2UsIFJFTU9URV9SRURJUkVDVF9USU1FT1VUKTtcbiAgICAgICAgICAgIH0pO1xuXG4gICAgICAgIHJldHVybiBzaGlmdGluZ1Byb21pc2U7XG4gICAgfVxufVxuIl19
|