upstox-js-sdk
Version:
The official Node Js client for communicating with the Upstox API
81 lines (78 loc) • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.MarketDataStreamer = void 0;
var _MarketDataFeeder = require("./MarketDataFeeder");
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 _MarketDataStreamer_brand = /*#__PURE__*/new WeakSet();
class MarketDataStreamer extends _Streamer.default {
constructor(instrumentKeys = [], mode = "") {
super();
_classPrivateMethodInitSpec(this, _MarketDataStreamer_brand);
//Required enums
_defineProperty(this, "Mode", Object.freeze({
LTPC: "ltpc",
FULL: "full"
}));
this.instrumentKeys = instrumentKeys;
this.mode = mode;
this.subscriptions = {
[this.Mode.LTPC]: new Set(),
[this.Mode.FULL]: new Set()
};
// 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 _MarketDataFeeder.MarketDataFeeder(this.instrumentKeys, this.mode);
this.setupEventListeners();
this.subscriptionEventListeners();
await this.streamer.connect();
}
disconnect() {
this.streamer.disconnect();
_assertClassBrand(_MarketDataStreamer_brand, this, _clearSubscriptions).call(this);
}
subscribe(instrumentKeys, mode) {
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) {
this.streamer.changeMode(instrumentKeys, newMode);
const oldMode = newMode === this.Mode.LTPC ? this.Mode.FULL : this.Mode.LTPC;
// Remove keys from the old mode
instrumentKeys.forEach(key => {
this.subscriptions[oldMode].delete(key);
});
// Add keys to the new mode
this.subscriptions[newMode] = new Set([...this.subscriptions[newMode], ...instrumentKeys]);
}
}
exports.MarketDataStreamer = MarketDataStreamer;
function _clearSubscriptions() {
this.subscriptions[this.Mode.LTPC].clear();
this.subscriptions[this.Mode.FULL].clear();
}