@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.
98 lines (97 loc) • 5.37 kB
JavaScript
import { __awaiter } from "tslib";
import { generateId } from "../utils";
import { SessionLifetime } from "./types/session-lifetime";
import { Session } from "./types/session";
import { BloombergSession } from "./types";
import { SessionInstance } from "../core/session-instance";
export 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 SessionInstance(this.mdfBridge, this.configureBloombergSession({ name: Session.DataRequests, lifetime: SessionLifetime.Owned }), this.logger);
this.largeHistoricalRequestsSessionImpl = new SessionInstance(this.mdfBridge, this.configureBloombergSession({ name: Session.LargeHistoricalRequests, lifetime: SessionLifetime.Owned }), this.logger);
this.subscriptionsSession = new SessionInstance(this.mdfBridge, this.configureBloombergSession({ name: Session.RealTime, lifetime: SessionLifetime.Owned }), this.logger);
}
get all() {
return Array.from(this.sessionInstanceByName.keys());
}
createSession(settings) {
return __awaiter(this, void 0, void 0, function* () {
const bbgSession = this.configureBloombergSession(Object.assign(Object.assign({}, settings), { name: generateId(), lifetime: SessionLifetime.Owned }));
this.sessionInstanceByName.set(bbgSession.name, new SessionInstance(this.mdfBridge, bbgSession, this.logger));
return Promise.resolve(bbgSession);
});
}
closeSession(sessionName) {
return __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 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);
}
if (providedSession === Session.CreateNew) {
const shortLivedSessionConfig = this.configureBloombergSession({
name: generateId(),
lifetime: SessionLifetime.RefCounted,
});
return new SessionInstance(this.mdfBridge, shortLivedSessionConfig, this.logger);
}
if (requestKind === "non-subscription") {
return providedSession === Session.LargeHistoricalRequests
? this.largeHistoricalRequestsSessionImpl
: this.dataRequestsSessionImpl;
}
return this.subscriptionsSession;
}
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 : SessionLifetime.Owned;
let identityOptions = (settings === null || settings === void 0 ? void 0 : settings.identityOptions) != null && typeof settings.identityOptions === "object"
? settings.identityOptions
: this.libConfigIdentityOptions;
if (identityOptions) {
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 BloombergSession(name, lifetime, options, identityOptions);
}
decorateSessionName(name) {
return `${name}_${this.apiId}`;
}
}