@glue42/bbg-market-data
Version:
A high-level API that wraps existing Glue42 Bloomberg Bridge Market Data interop methods. The API is based on the jBloomberg open source wrapper.
211 lines (210 loc) • 11.8 kB
JavaScript
import { __assign, __awaiter, __generator, __rest } from "tslib";
import { PromiseWrapper } from "../promise-wrapper";
import { createSequence } from "../seq";
import { callSafe, clone } from "../utils";
var SessionInstance = (function () {
function SessionInstance(mdfBridge, sessionConfig, logger) {
this.mdfBridge = mdfBridge;
this.sessionConfig = sessionConfig;
this.logger = logger;
this.activeRequests = new Map();
}
SessionInstance.prototype.openRequest = function (requestId, requestConfig, operationArgs, otherSettings, onEvent, messageProcessor, onSuccess, onFail) {
var _this = this;
var _a;
var unsubscribeEvents = this.subscribeMdfEvents(requestId, onEvent, messageProcessor);
var reqInfo = {
unsubscribeEvents: unsubscribeEvents,
mdfOpenPW: new PromiseWrapper(),
messageSequence: createSequence(),
};
this.activeRequests.set(requestId, reqInfo);
var mdfArgs = {
requestCorrelationId: requestId,
operationArgs: operationArgs,
settings: __assign({ sessionName: this.sessionConfig.name, sessionLifetime: this.sessionConfig.lifetime, sessionOptions: this.sessionConfig.options, sessionIdentityOptions: this.sessionConfig.identityOptions }, otherSettings),
operation: requestConfig.operation,
service: requestConfig.service,
};
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Session \"" + this.sessionConfig.name + "/" + this.sessionConfig.lifetime + "\" is attempting to open a non-subscription request with an id \"" + requestId + "\".");
this.mdfBridge
.createRequest(mdfArgs)
.then(function () { return _this.handleOpenSuccess(requestId, onSuccess); })
.catch(function (error) { return _this.handleOpenFail(requestId, error, onFail); });
};
SessionInstance.prototype.openSubscriptionRequest = function (requestId, config, subscriptions, otherSettings, onEvent, messageProcessor, onSuccess, onFail) {
var _this = this;
var _a;
var unsubscribeEvents = this.subscribeMdfEvents(requestId, onEvent, messageProcessor);
var reqInfo = {
unsubscribeEvents: unsubscribeEvents,
mdfOpenPW: new PromiseWrapper(),
messageSequence: createSequence(),
};
this.activeRequests.set(requestId, reqInfo);
var mdfArgs = {
requestCorrelationId: requestId,
subscriptions: subscriptions,
settings: __assign({ sessionName: this.sessionConfig.name, sessionLifetime: this.sessionConfig.lifetime, sessionOptions: this.sessionConfig.options, sessionIdentityOptions: this.sessionConfig.identityOptions }, otherSettings),
service: config.service,
};
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Session \"" + this.sessionConfig.name + "/" + this.sessionConfig.lifetime + "\" is attempting to open a subscription request with an id \"" + requestId + "\".");
this.mdfBridge
.createSubscriptionRequest(mdfArgs)
.then(function () { return _this.handleOpenSuccess(requestId, onSuccess); })
.catch(function (error) { return _this.handleOpenFail(requestId, error, onFail); });
};
SessionInstance.prototype.onBloombergEvent = function (requestId, callback) {
return this.mdfBridge.onEvent(requestId, callback);
};
SessionInstance.prototype.closeRequest = function (requestId, onClosed) {
var _this = this;
var _a, _b;
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Session \"" + this.sessionConfig.name + "\" is attempting to close a request with an id \"" + requestId + "\".");
var reqInfo = this.activeRequests.get(requestId);
if (!reqInfo) {
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.warn("Session \"" + this.sessionConfig.name + "\" cannot close a request with an id \"" + requestId + "\", it does not exists in state.");
return Promise.resolve();
}
if (reqInfo.closePromise instanceof Promise) {
return reqInfo.closePromise;
}
reqInfo.closePromise = (function () { return __awaiter(_this, void 0, void 0, function () {
var openSuccess, reason, _a;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 2, , 3]);
return [4, ((_b = reqInfo.mdfOpenPW) === null || _b === void 0 ? void 0 : _b.promise)];
case 1:
openSuccess = _c.sent();
if (openSuccess) {
reason = "User closed request with an id \"" + requestId + "\".";
this.dispose(requestId, reason, true);
callSafe(onClosed);
}
return [3, 3];
case 2:
_a = _c.sent();
return [3, 3];
case 3: return [2];
}
});
}); })();
return reqInfo.closePromise;
};
SessionInstance.prototype.disposeRequest = function (requestId, reason) {
this.dispose(requestId, reason, true);
};
SessionInstance.prototype.dispose = function (requestId, reason, shouldCloseRequest) {
var _a, _b, _c, _d, _e;
if (shouldCloseRequest === void 0) { shouldCloseRequest = false; }
return __awaiter(this, void 0, void 0, function () {
var reqInfo, error_1;
return __generator(this, function (_f) {
switch (_f.label) {
case 0:
reqInfo = this.activeRequests.get(requestId);
if (!reqInfo) {
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Session \"" + this.sessionConfig.name + "\" cannot dispose request with an id \"" + requestId + "\", it does not exists in state.");
return [2];
}
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.info("Session \"" + this.sessionConfig.name + "\" is disposing request with an id \"" + requestId + "\", reason: " + reason);
this.activeRequests.delete(requestId);
reqInfo.unsubscribeEvents();
reqInfo.messageSequence.clear();
(_c = this.logger) === null || _c === void 0 ? void 0 : _c.info("Session \"" + this.sessionConfig.name + "\" cleaned up request with an id \"" + requestId + "\".");
if (!shouldCloseRequest) return [3, 4];
_f.label = 1;
case 1:
_f.trys.push([1, 3, , 4]);
return [4, this.mdfBridge.closeRequest(requestId, reason)];
case 2:
_f.sent();
(_d = this.logger) === null || _d === void 0 ? void 0 : _d.info("Session \"" + this.sessionConfig.name + "\" - bridge closed successfully request with an id \"" + requestId + "\".");
return [3, 4];
case 3:
error_1 = _f.sent();
(_e = this.logger) === null || _e === void 0 ? void 0 : _e.warn("Session \"" + this.sessionConfig.name + "\" - bridge failed to close request with an id \"" + requestId + "\" - " + JSON.stringify(error_1));
return [3, 4];
case 4: return [2];
}
});
});
};
SessionInstance.prototype.handleOpenSuccess = function (requestId, callback) {
var _a, _b;
return __awaiter(this, void 0, void 0, function () {
var reqInfo;
var _this = this;
return __generator(this, function (_c) {
reqInfo = this.activeRequests.get(requestId);
if (!reqInfo) {
return [2];
}
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Session \"" + this.sessionConfig.name + "\" - bridge opened successfully a request with an id \"" + requestId + "\".");
reqInfo.mdfOpenPW.resolve(true);
if (reqInfo.closePromise instanceof Promise) {
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.info("Session \"" + this.sessionConfig.name + "\" - request with an id \"" + requestId + "\" got \"close\" command before the \"open\" task completes.");
return [2, reqInfo.closePromise];
}
callSafe(callback);
setTimeout(function () {
var _a, _b;
var reqInfo = _this.activeRequests.get(requestId);
if (reqInfo) {
(_a = _this.logger) === null || _a === void 0 ? void 0 : _a.info("Session \"" + _this.sessionConfig.name + "\" - starting message processor sequence for request with an id \"" + requestId + "\"");
(_b = reqInfo === null || reqInfo === void 0 ? void 0 : reqInfo.messageSequence) === null || _b === void 0 ? void 0 : _b.run();
}
}, 0);
return [2];
});
});
};
SessionInstance.prototype.handleOpenFail = function (id, error, callback) {
var _a;
var reqInfo = this.activeRequests.get(id);
if (!reqInfo) {
return;
}
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.info("Session \"" + this.sessionConfig.name + "\" - failed to open the request with an id \"" + id + "\". Will attempt at disposing the request.");
var reason = "Opening request failed with " + error.message;
this.dispose(id, reason, false);
reqInfo.mdfOpenPW.resolve(false);
callSafe(callback, error);
};
SessionInstance.prototype.subscribeMdfEvents = function (id, onEvent, messageProcessor) {
var _this = this;
return this.mdfBridge.onEvent(id, function (event) {
if (event == null) {
return;
}
var reqInfo = _this.activeRequests.get(id);
if (!reqInfo) {
return;
}
var clonedEvent = clone(event);
onEvent(clonedEvent);
var evType = event.eventType;
var eventMessages = event.eventMessages;
eventMessages.forEach(function (_a) {
var correlationIds = _a.correlationIds, message = __rest(_a, ["correlationIds"]);
var eventType = evType;
var action = function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, messageProcessor({ correlationIds: correlationIds, message: message, eventType: eventType })];
case 1:
_a.sent();
return [2];
}
});
}); };
reqInfo.messageSequence.enqueue(action);
});
});
};
return SessionInstance;
}());
export { SessionInstance };