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.

107 lines 6.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionsManager = void 0; const tslib_1 = require("tslib"); const utils_1 = require("../utils"); const session_lifetime_1 = require("./types/session-lifetime"); const session_1 = require("./types/session"); const types_1 = require("./types"); const session_instance_1 = require("../core/session-instance"); class SessionsManager { constructor(mdfBridge, apiId, libConfigSettings, logger) { var _a, _b; this.mdfBridge = mdfBridge; this.apiId = apiId; this.libConfigSettings = libConfigSettings; this.logger = logger; this.sessionInstanceByName = new Map(); this.libConfigSessionOptions = ((_a = this.libConfigSettings) === null || _a === void 0 ? void 0 : _a.options) != null && typeof this.libConfigSettings.options === "object" ? this.libConfigSettings.options : undefined; this.libConfigIdentityOptions = ((_b = this.libConfigSettings) === null || _b === void 0 ? void 0 : _b.identityOptions) != null && typeof this.libConfigSettings.identityOptions === "object" ? this.libConfigSettings.identityOptions : undefined; this.dataRequestsSessionImpl = new session_instance_1.SessionInstance(this.mdfBridge, this.configureBloombergSession({ name: session_1.Session.DataRequests, lifetime: session_lifetime_1.SessionLifetime.Owned }), this.logger); this.largeHistoricalRequestsSessionImpl = new session_instance_1.SessionInstance(this.mdfBridge, this.configureBloombergSession({ name: session_1.Session.LargeHistoricalRequests, lifetime: session_lifetime_1.SessionLifetime.Owned }), this.logger); this.subscriptionsSession = new session_instance_1.SessionInstance(this.mdfBridge, this.configureBloombergSession({ name: session_1.Session.RealTime, lifetime: session_lifetime_1.SessionLifetime.Owned }), this.logger); } get all() { return Array.from(this.sessionInstanceByName.keys()); } createSession(settings) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const bbgSession = this.configureBloombergSession(Object.assign(Object.assign({}, settings), { name: utils_1.generateId(), lifetime: session_lifetime_1.SessionLifetime.Owned })); this.sessionInstanceByName.set(bbgSession.name, new session_instance_1.SessionInstance(this.mdfBridge, bbgSession, this.logger)); return Promise.resolve(bbgSession); }); } closeSession(sessionName) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!this.sessionInstanceByName.has(sessionName)) { throw new Error("Either this session was closed or it was not created by this api instance."); } yield this.mdfBridge.closeSession(sessionName); this.sessionInstanceByName.delete(sessionName); }); } getNonSubscriptionReqSessionInstance(providedSession) { return this.getSessionInstance({ providedSession, requestKind: "non-subscription", }); } getSubscriptionReqSessionInstance(providedSession) { return this.getSessionInstance({ providedSession, requestKind: "subscription", }); } getSessionInstance({ providedSession, requestKind, }) { if (providedSession instanceof types_1.BloombergSession) { if (!this.sessionInstanceByName.has(providedSession.name)) { throw new Error("Either this session was closed or it was not created by this api instance."); } return this.sessionInstanceByName.get(providedSession.name); } // Short lived. if (providedSession === session_1.Session.CreateNew) { const shortLivedSessionConfig = this.configureBloombergSession({ name: utils_1.generateId(), lifetime: session_lifetime_1.SessionLifetime.RefCounted, }); return new session_instance_1.SessionInstance(this.mdfBridge, shortLivedSessionConfig, this.logger); } if (requestKind === "non-subscription") { return providedSession === session_1.Session.LargeHistoricalRequests ? this.largeHistoricalRequestsSessionImpl : this.dataRequestsSessionImpl; } return this.subscriptionsSession; } /** * Creates a session config. Unless lifetime is provided in settings then it is set to "Owned". */ configureBloombergSession(settings) { var _a, _b; const name = this.decorateSessionName(settings.name); const lifetime = (_a = settings === null || settings === void 0 ? void 0 : settings.lifetime) !== null && _a !== void 0 ? _a : session_lifetime_1.SessionLifetime.Owned; let identityOptions = (settings === null || settings === void 0 ? void 0 : settings.identityOptions) != null && typeof settings.identityOptions === "object" ? settings.identityOptions : this.libConfigIdentityOptions; if (identityOptions) { // Workaround an exception in the bridge. identityOptions = Object.assign(Object.assign({}, identityOptions), { authUser: (_b = identityOptions === null || identityOptions === void 0 ? void 0 : identityOptions.authUser) !== null && _b !== void 0 ? _b : {} }); } const options = (settings === null || settings === void 0 ? void 0 : settings.options) != null && typeof settings.options === "object" ? settings.options : this.libConfigSessionOptions; return new types_1.BloombergSession(name, lifetime, options, identityOptions); } decorateSessionName(name) { return `${name}_${this.apiId}`; } } exports.SessionsManager = SessionsManager; //# sourceMappingURL=sessions-manager.js.map