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.

180 lines (179 loc) 7.75 kB
import { __awaiter, __rest } from "tslib"; import { PromiseWrapper } from "../promise-wrapper"; import { RequestStatus } from "../request-status"; import { isEventOfType, isPending } from "./helpers"; import { EventTypes } from "./event-types"; import { callSafe, extractErrorMessage } from "./../utils"; import { BaseRequest } from "./base-request"; import { requestFailureHandler, serviceOpenedFailureHandler, sessionStartUpFailureHandler, sessionTerminatedHandler } from "./message-handlers"; const DATA_EVENT = "data-event"; const ERROR_EVENT = "error-event"; const STATUS_EVENT = "status-event"; export class NonSubscriptionRequestImpl extends BaseRequest { constructor(sessionManager, config, operationArgs, handlers) { super(sessionManager, config, operationArgs); this.sessionManager = sessionManager; this.config = config; this.operationArgs = operationArgs; this.handlers = handlers; this.aggregatedResponseData = []; this.shouldAggregateResponse = true; super.onRequestStatusChanged(this.handleRequestStatusChanged.bind(this)); if (typeof this.handlers.partialResponseData != "function" || typeof this.handlers.responseData != "function" || typeof this.handlers.responseError != "function") { throw new Error("All handlers must be functions."); } } get api() { const that = this; return { get id() { return that.requestId; }, get settings() { return { operation: that.config.operation, service: that.config.service, aggregateResponse: that.shouldAggregateResponse, operationArgs: that.operationArgs, }; }, get status() { return that._status; }, open: this.open.bind(that), close: that.closeRequest.bind(that), onData: (callback) => { return that.registry.add(DATA_EVENT, callback); }, onError: (callback) => { return that.registry.add(ERROR_EVENT, callback); }, onEvent: that.onEvent.bind(that), onStatus: (callback) => { callSafe(callback, that._status); return that.registry.add(STATUS_EVENT, callback); }, }; } open(options) { const _super = Object.create(null, { openRequest: { get: () => super.openRequest } }); return __awaiter(this, void 0, void 0, function* () { if (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.")); } this.reset(); const _a = options !== null && options !== void 0 ? options : {}, { aggregateResponse } = _a, otherOptions = __rest(_a, ["aggregateResponse"]); this.shouldAggregateResponse = aggregateResponse === false ? false : true; this.aggregatedResponsePW = new PromiseWrapper(); _super.openRequest.call(this, Object.assign(Object.assign({}, otherOptions), { messageProcessor: this.messageProcessor.bind(this) })).catch(() => { }); return this.aggregatedResponsePW.promise; }); } handleRequestStatusChanged(event) { var _a, _b, _c; this.registry.execute(STATUS_EVENT, event.status); if (event.status === RequestStatus.Active) { if (this.shouldAggregateResponse === false) { (_a = this.aggregatedResponsePW) === null || _a === void 0 ? void 0 : _a.resolve(undefined); } return; } if (event.status === RequestStatus.Closed || event.status === RequestStatus.Completed) { return (_b = this.aggregatedResponsePW) === null || _b === void 0 ? void 0 : _b.resolve(this.aggregatedResponseData); } if (event.status === RequestStatus.Failed) { this.registry.execute(ERROR_EVENT, event.error); return (_c = this.aggregatedResponsePW) === null || _c === void 0 ? void 0 : _c.reject(event.error); } } messageProcessor(eventMessage) { return __awaiter(this, void 0, void 0, function* () { const error = [ sessionStartUpFailureHandler(eventMessage), sessionTerminatedHandler(eventMessage), serviceOpenedFailureHandler(eventMessage), requestFailureHandler(eventMessage), ].find(({ match }) => match); if (error) { return { action: "fail", error: error.data }; } if (isEventOfType(eventMessage, EventTypes.Response)) { return this.processResponseBBGEvent(eventMessage); } if (isEventOfType(eventMessage, EventTypes.PartialResponse)) { this.processPartialResponseBBGEvent(eventMessage); } return Promise.resolve({ action: 'continue' }); }); } processPartialResponseBBGEvent(eventMessage) { var _a; try { const { data, match } = this.handlers.partialResponseData(eventMessage); if (!match) { return; } if (this.shouldAggregateResponse) { (_a = this.aggregatedResponseData) === null || _a === void 0 ? void 0 : _a.push(data); } this.raiseResponseData({ data: data, isLast: false, }); } catch (_b) { this.raiseResponseData({ data: undefined, isLast: false, }); } } processResponseBBGEvent(eventMessage) { var _a; try { const responseDataResult = this.handlers.responseData(eventMessage); if (responseDataResult.match) { if (this.shouldAggregateResponse) { (_a = this.aggregatedResponseData) === null || _a === void 0 ? void 0 : _a.push(responseDataResult.data); } this.raiseResponseData({ data: responseDataResult.data, isLast: true, }); return { action: 'complete' }; } const responseErrorResult = this.handlers.responseError(eventMessage); if (responseErrorResult.match) { return { action: 'fail', error: responseErrorResult.data }; } return { action: 'fail', error: new Error(`Received a ${EventTypes.Response} event but neither data nor error messages can be matched. Check handlers.`) }; } catch (error) { const errorMsg = extractErrorMessage(error); return { action: 'fail', error: new Error(`Processing ${EventTypes.Response} event message errored with "${errorMsg}"`) }; } } raiseResponseData(responseData) { this.registry.execute(DATA_EVENT, responseData); } reset() { return __awaiter(this, void 0, void 0, function* () { this.shouldAggregateResponse = true; this.aggregatedResponseData = []; }); } }