@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.
53 lines (52 loc) • 2.38 kB
JavaScript
import { SessionState } from "./../bridge/types";
import { ConnectionStatus } from "./connection-status";
import CallbackRegistryFactory from "callback-registry";
import { CONNECTION_RETRY_PERIOD_DEFAULT } from "./../constants";
var STATUS_CHANGED_EVENT = "status-changed";
var Connection = (function () {
function Connection(mdfBridge, connectionPeriodMsecs) {
this.mdfBridge = mdfBridge;
this.connectionPeriodMsecs = connectionPeriodMsecs;
this.registry = CallbackRegistryFactory();
this.connectionStatus = ConnectionStatus.Disconnected;
this.subscribe();
}
Connection.prototype.onConnectionStatusChanged = function (callback) {
if (typeof callback != "function") {
throw new Error("callback must be a function.");
}
return this.registry.add(STATUS_CHANGED_EVENT, callback, [this.connectionStatus]);
};
Connection.prototype.setConnectionStatus = function (newConnectionStatus) {
if (this.connectionStatus !== newConnectionStatus) {
this.connectionStatus = newConnectionStatus;
this.registry.execute(STATUS_CHANGED_EVENT, this.connectionStatus);
}
};
Connection.prototype.subscribe = function () {
var _this = this;
var handleData = function (sessionStatus) {
if (sessionStatus == null) {
return;
}
var newConnectionStatus = sessionStatus.state === SessionState.Connected ? ConnectionStatus.Connected : ConnectionStatus.Disconnected;
_this.setConnectionStatus(newConnectionStatus);
};
var handleError = function () {
_this.setConnectionStatus(ConnectionStatus.Disconnected);
};
var handleClose = function () {
_this.setConnectionStatus(ConnectionStatus.Disconnected);
};
var retryPeriodMsecs = typeof this.connectionPeriodMsecs != "number" || this.connectionPeriodMsecs < CONNECTION_RETRY_PERIOD_DEFAULT
? CONNECTION_RETRY_PERIOD_DEFAULT
: this.connectionPeriodMsecs;
this.mdfBridge.subscribeForConnectionStatus(retryPeriodMsecs, {
onData: handleData,
onError: handleError,
onClose: handleClose,
});
};
return Connection;
}());
export { Connection };