@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.
111 lines (110 loc) • 5.05 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import CallbackRegistryFactory from "callback-registry";
import { RequestStatus } from "../request-status";
import { getFailureReason, isPending } from "./helpers";
import CorrelationId from "./../correlationId";
const STATUS_EVENT = "status-changed-event";
const BBG_EVENT = "bbg-event";
export class BaseRequest {
constructor(sessionManager, config, operationArgs) {
this.sessionManager = sessionManager;
this.config = config;
this.operationArgs = operationArgs;
this.registry = CallbackRegistryFactory();
this._status = RequestStatus.Created;
this.sessionInstance = this.sessionManager.getNonSubscriptionReqSessionInstance();
}
openRequest(options) {
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."));
}
const _a = options !== null && options !== void 0 ? options : {}, { session, messageProcessor } = _a, otherOptions = __rest(_a, ["session", "messageProcessor"]);
this.sessionInstance = this.sessionManager.getNonSubscriptionReqSessionInstance(session);
this.requestId = new CorrelationId(this.config.service, this.config.operation).value;
const handleEvent = (event) => {
this.registry.execute(BBG_EVENT, event);
};
this.requestOpened();
return new Promise((resolve, reject) => {
const handleOpenSuccess = () => {
this.requestActivated();
resolve();
};
const handleOpenFail = (error) => {
this.requestFailed(error);
reject(error);
};
this.sessionInstance.openRequest(this.requestId, this.config, this.operationArgs, otherOptions !== null && otherOptions !== void 0 ? otherOptions : {}, handleEvent, (eventMessage) => this.processEventMessage(this.requestId, eventMessage, messageProcessor), handleOpenSuccess, handleOpenFail);
});
});
}
onEvent(callback) {
return this.registry.add(BBG_EVENT, callback);
}
closeRequest() {
return __awaiter(this, void 0, void 0, function* () {
const onRequestClosed = () => this.requestClosedSuccessfully();
yield this.sessionInstance.closeRequest(this.requestId, onRequestClosed);
});
}
onRequestStatusChanged(callback) {
return this.registry.add(STATUS_EVENT, callback);
}
processEventMessage(requestId, eventMessage, messageProcessor) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield messageProcessor(eventMessage);
if (this.requestId !== requestId) {
return;
}
if (isPending(this._status) == false) {
return;
}
if (result.action === 'fail') {
this.requestFailed(result.error);
}
else if (result.action === 'complete') {
this.requestCompleted();
}
else if (result.action === 'close') {
this.requestDisposed();
}
else {
return Promise.resolve();
}
});
}
requestDisposed() {
const reason = `Request with an id "${this.requestId}" was closed by the event message processor.`;
this.sessionInstance.disposeRequest(this.requestId, reason);
this._status = RequestStatus.Closed;
this.raiseRequestStatusChanged({ status: this._status });
}
requestOpened() {
this._status = RequestStatus.Opened;
this.raiseRequestStatusChanged({ status: this._status });
}
requestActivated() {
this._status = RequestStatus.Active;
this.raiseRequestStatusChanged({ status: this._status });
}
requestClosedSuccessfully() {
this._status = RequestStatus.Closed;
this.raiseRequestStatusChanged({ status: this._status });
}
requestFailed(error) {
const reason = getFailureReason(this.requestId, error);
this.sessionInstance.disposeRequest(this.requestId, reason);
this._status = RequestStatus.Failed;
this.raiseRequestStatusChanged({ status: this._status, error });
}
requestCompleted() {
const reason = `Request with an id "${this.requestId}" completed.`;
this.sessionInstance.disposeRequest(this.requestId, reason);
this._status = RequestStatus.Completed;
this.raiseRequestStatusChanged({ status: this._status });
}
raiseRequestStatusChanged(event) {
this.registry.execute(STATUS_EVENT, event);
}
}