upstox-js-sdk
Version:
The official Node Js client for communicating with the Upstox API
103 lines (101 loc) • 4.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MarketDataStreamerV3 = void 0;
var _MarketDataFeederV = require("./MarketDataFeederV3");
var _Streamer = _interopRequireDefault(require("./Streamer"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
var _MarketDataStreamerV3_brand = /*#__PURE__*/new WeakSet();
class MarketDataStreamerV3 extends _Streamer.default {
constructor(instrumentKeys = [], mode = "ltpc") {
super();
_classPrivateMethodInitSpec(this, _MarketDataStreamerV3_brand);
//Required enums
_defineProperty(this, "Mode", Object.freeze({
LTPC: "ltpc",
FULL: "full",
OPTION: "option_greeks",
D30: "full_d30"
}));
this.instrumentKeys = instrumentKeys;
this.mode = mode;
this.subscriptions = {
[this.Mode.LTPC]: new Set(),
[this.Mode.FULL]: new Set(),
[this.Mode.OPTION]: new Set(),
[this.Mode.D30]: new Set()
};
if (!Object.values(this.Mode).includes(mode)) {
throw new Error("Invalid mode provided ", mode);
}
// Populate initial subscriptions if provided
instrumentKeys.forEach(key => this.subscriptions[mode].add(key));
}
subscriptionEventListeners() {
this.streamer.on("open", () => {
Object.entries(this.subscriptions).forEach(([mode, keys]) => {
if (keys.size > 0) {
this.subscribe(Array.from(keys), mode);
}
});
});
}
async connect() {
this.streamer = new _MarketDataFeederV.MarketDataFeederV3(this.instrumentKeys, this.mode);
this.setupEventListeners();
this.subscriptionEventListeners();
await this.streamer.connect();
}
disconnect() {
this.streamer.disconnect();
_assertClassBrand(_MarketDataStreamerV3_brand, this, _clearSubscriptions).call(this);
}
subscribe(instrumentKeys, mode) {
if (this.isInvalidMode(mode)) {
return;
}
this.streamer.subscribe(instrumentKeys, mode);
this.subscriptions[mode] = new Set([...this.subscriptions[mode], ...instrumentKeys]);
}
unsubscribe(instrumentKeys) {
this.streamer.unsubscribe(instrumentKeys);
Object.values(this.subscriptions).forEach(set => {
instrumentKeys.forEach(key => set.delete(key));
});
}
changeMode(instrumentKeys, newMode) {
if (this.isInvalidMode(newMode)) {
return;
}
this.streamer.changeMode(instrumentKeys, newMode);
Object.keys(this.subscriptions).forEach(mode => {
instrumentKeys.forEach(key => {
this.subscriptions[mode].delete(key);
});
});
// Add keys to the new mode
this.subscriptions[newMode] = new Set([...this.subscriptions[newMode], ...instrumentKeys]);
this.mode = newMode;
}
isInvalidMode(mode) {
if (!Object.values(this.Mode).includes(mode)) {
this.emit(this.Event.ERROR, `Invalid mode provided ${mode}`);
return true;
}
return false;
}
}
exports.MarketDataStreamerV3 = MarketDataStreamerV3;
function _clearSubscriptions() {
this.subscriptions[this.Mode.LTPC].clear();
this.subscriptions[this.Mode.FULL].clear();
this.subscriptions[this.Mode.OPTION].clear();
this.subscriptions[this.Mode.D30].clear();
}