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.

50 lines (49 loc) 2.18 kB
import { SessionState } from "./../bridge/types"; import { ConnectionStatus } from "./connection-status"; import CallbackRegistryFactory from "callback-registry"; import { CONNECTION_RETRY_PERIOD_DEFAULT } from "./../constants"; const STATUS_CHANGED_EVENT = "status-changed"; export class Connection { constructor(mdfBridge, connectionPeriodMsecs) { this.mdfBridge = mdfBridge; this.connectionPeriodMsecs = connectionPeriodMsecs; this.registry = CallbackRegistryFactory(); this.connectionStatus = ConnectionStatus.Disconnected; this.subscribe(); } onConnectionStatusChanged(callback) { if (typeof callback != "function") { throw new Error("callback must be a function."); } return this.registry.add(STATUS_CHANGED_EVENT, callback, [this.connectionStatus]); } setConnectionStatus(newConnectionStatus) { if (this.connectionStatus !== newConnectionStatus) { this.connectionStatus = newConnectionStatus; this.registry.execute(STATUS_CHANGED_EVENT, this.connectionStatus); } } subscribe() { const handleData = (sessionStatus) => { if (sessionStatus == null) { return; } const newConnectionStatus = sessionStatus.state === SessionState.Connected ? ConnectionStatus.Connected : ConnectionStatus.Disconnected; this.setConnectionStatus(newConnectionStatus); }; const handleError = () => { this.setConnectionStatus(ConnectionStatus.Disconnected); }; const handleClose = () => { this.setConnectionStatus(ConnectionStatus.Disconnected); }; const 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, }); } }