UNPKG

@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.

135 lines (134 loc) 6.8 kB
import { __awaiter, __generator, __rest } from "tslib"; import CallbackRegistryFactory from "callback-registry"; import { RequestStatus } from "../request-status"; import { getFailureReason, isPending } from "./helpers"; import CorrelationId from "./../correlationId"; var STATUS_EVENT = "status-changed-event"; var BBG_EVENT = "bbg-event"; var BaseRequest = (function () { function BaseRequest(sessionManager, config, operationArgs) { this.sessionManager = sessionManager; this.config = config; this.operationArgs = operationArgs; this.registry = CallbackRegistryFactory(); this._status = RequestStatus.Created; this.sessionInstance = this.sessionManager.getNonSubscriptionReqSessionInstance(); } BaseRequest.prototype.openRequest = function (options) { return __awaiter(this, void 0, void 0, function () { var _a, session, messageProcessor, otherOptions, handleEvent; var _this = this; return __generator(this, function (_b) { if (isPending(this._status)) { return [2, Promise.reject(new Error("Request can be opened/reopened if its status is Created, Failed, Closed or Completed. Either close the request or wait it to complete."))]; } _a = options !== null && options !== void 0 ? options : {}, session = _a.session, messageProcessor = _a.messageProcessor, otherOptions = __rest(_a, ["session", "messageProcessor"]); this.sessionInstance = this.sessionManager.getNonSubscriptionReqSessionInstance(session); this.requestId = new CorrelationId(this.config.service, this.config.operation).value; handleEvent = function (event) { _this.registry.execute(BBG_EVENT, event); }; this.requestOpened(); return [2, new Promise(function (resolve, reject) { var handleOpenSuccess = function () { _this.requestActivated(); resolve(); }; var handleOpenFail = function (error) { _this.requestFailed(error); reject(error); }; _this.sessionInstance.openRequest(_this.requestId, _this.config, _this.operationArgs, otherOptions !== null && otherOptions !== void 0 ? otherOptions : {}, handleEvent, function (eventMessage) { return _this.processEventMessage(_this.requestId, eventMessage, messageProcessor); }, handleOpenSuccess, handleOpenFail); })]; }); }); }; BaseRequest.prototype.onEvent = function (callback) { return this.registry.add(BBG_EVENT, callback); }; BaseRequest.prototype.closeRequest = function () { return __awaiter(this, void 0, void 0, function () { var onRequestClosed; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: onRequestClosed = function () { return _this.requestClosedSuccessfully(); }; return [4, this.sessionInstance.closeRequest(this.requestId, onRequestClosed)]; case 1: _a.sent(); return [2]; } }); }); }; BaseRequest.prototype.onRequestStatusChanged = function (callback) { return this.registry.add(STATUS_EVENT, callback); }; BaseRequest.prototype.processEventMessage = function (requestId, eventMessage, messageProcessor) { return __awaiter(this, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, messageProcessor(eventMessage)]; case 1: result = _a.sent(); if (this.requestId !== requestId) { return [2]; } if (isPending(this._status) == false) { return [2]; } if (result.action === 'fail') { this.requestFailed(result.error); } else if (result.action === 'complete') { this.requestCompleted(); } else if (result.action === 'close') { this.requestDisposed(); } else { return [2, Promise.resolve()]; } return [2]; } }); }); }; BaseRequest.prototype.requestDisposed = function () { var reason = "Request with an id \"" + this.requestId + "\" was closed by the event message processor."; this.sessionInstance.disposeRequest(this.requestId, reason); this._status = RequestStatus.Closed; this.raiseRequestStatusChanged({ status: this._status }); }; BaseRequest.prototype.requestOpened = function () { this._status = RequestStatus.Opened; this.raiseRequestStatusChanged({ status: this._status }); }; BaseRequest.prototype.requestActivated = function () { this._status = RequestStatus.Active; this.raiseRequestStatusChanged({ status: this._status }); }; BaseRequest.prototype.requestClosedSuccessfully = function () { this._status = RequestStatus.Closed; this.raiseRequestStatusChanged({ status: this._status }); }; BaseRequest.prototype.requestFailed = function (error) { var reason = getFailureReason(this.requestId, error); this.sessionInstance.disposeRequest(this.requestId, reason); this._status = RequestStatus.Failed; this.raiseRequestStatusChanged({ status: this._status, error: error }); }; BaseRequest.prototype.requestCompleted = function () { var reason = "Request with an id \"" + this.requestId + "\" completed."; this.sessionInstance.disposeRequest(this.requestId, reason); this._status = RequestStatus.Completed; this.raiseRequestStatusChanged({ status: this._status }); }; BaseRequest.prototype.raiseRequestStatusChanged = function (event) { this.registry.execute(STATUS_EVENT, event); }; return BaseRequest; }()); export { BaseRequest };