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.

122 lines 5.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseRequest = void 0; const tslib_1 = require("tslib"); const callback_registry_1 = tslib_1.__importDefault(require("callback-registry")); const request_status_1 = require("../request-status"); const helpers_1 = require("./helpers"); const correlationId_1 = tslib_1.__importDefault(require("./../correlationId")); const STATUS_EVENT = "status-changed-event"; const BBG_EVENT = "bbg-event"; /** * Represents the lifecycle of a non-subscription request. */ class BaseRequest { constructor(sessionManager, config, operationArgs) { this.sessionManager = sessionManager; this.config = config; this.operationArgs = operationArgs; this.registry = callback_registry_1.default(); this._status = request_status_1.RequestStatus.Created; this.sessionInstance = this.sessionManager.getNonSubscriptionReqSessionInstance(); } openRequest(options) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (helpers_1.isPending(this._status)) { return 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.")); } const _a = options !== null && options !== void 0 ? options : {}, { session, messageProcessor } = _a, otherOptions = tslib_1.__rest(_a, ["session", "messageProcessor"]); this.sessionInstance = this.sessionManager.getNonSubscriptionReqSessionInstance(session); // Providing the service and operation as a suffix for the id to make troubleshooting easier. this.requestId = new correlationId_1.default(this.config.service, this.config.operation).value; const handleEvent = (event) => { this.registry.execute(BBG_EVENT, event); }; this.requestOpened(); return new Promise((resolve, reject) => { const handleOpenSuccess = () => { this.requestActivated(); resolve(); }; const handleOpenFail = (error) => { this.requestFailed(error); reject(error); }; this.sessionInstance.openRequest(this.requestId, this.config, this.operationArgs, otherOptions !== null && otherOptions !== void 0 ? otherOptions : {}, handleEvent, (eventMessage) => this.processEventMessage(this.requestId, eventMessage, messageProcessor), handleOpenSuccess, handleOpenFail); }); }); } onEvent(callback) { return this.registry.add(BBG_EVENT, callback); } closeRequest() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const onRequestClosed = () => this.requestClosedSuccessfully(); yield this.sessionInstance.closeRequest(this.requestId, onRequestClosed); }); } onRequestStatusChanged(callback) { return this.registry.add(STATUS_EVENT, callback); } processEventMessage(requestId, eventMessage, messageProcessor) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const result = yield messageProcessor(eventMessage); if (this.requestId !== requestId) { // If the requestId has changed, ignore the message. return; } if (helpers_1.isPending(this._status) == false) { // Request has been disposed. return; } if (result.action === 'fail') { this.requestFailed(result.error); } else if (result.action === 'complete') { this.requestCompleted(); } else if (result.action === 'close') { this.requestDisposed(); } else { // Continue processing the request, no action needed. return Promise.resolve(); } }); } requestDisposed() { const reason = `Request with an id "${this.requestId}" was closed by the event message processor.`; this.sessionInstance.disposeRequest(this.requestId, reason); this._status = request_status_1.RequestStatus.Closed; this.raiseRequestStatusChanged({ status: this._status }); } requestOpened() { this._status = request_status_1.RequestStatus.Opened; this.raiseRequestStatusChanged({ status: this._status }); } requestActivated() { this._status = request_status_1.RequestStatus.Active; this.raiseRequestStatusChanged({ status: this._status }); } requestClosedSuccessfully() { this._status = request_status_1.RequestStatus.Closed; this.raiseRequestStatusChanged({ status: this._status }); } requestFailed(error) { const reason = helpers_1.getFailureReason(this.requestId, error); this.sessionInstance.disposeRequest(this.requestId, reason); this._status = request_status_1.RequestStatus.Failed; this.raiseRequestStatusChanged({ status: this._status, error }); } requestCompleted() { const reason = `Request with an id "${this.requestId}" completed.`; this.sessionInstance.disposeRequest(this.requestId, reason); this._status = request_status_1.RequestStatus.Completed; this.raiseRequestStatusChanged({ status: this._status }); } raiseRequestStatusChanged(event) { this.registry.execute(STATUS_EVENT, event); } } exports.BaseRequest = BaseRequest; //# sourceMappingURL=base-request.js.map