UNPKG

upstox-js-sdk

Version:

The official Node Js client for communicating with the Upstox API

103 lines (101 loc) 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MarketDataStreamerV3 = void 0; var _MarketDataFeederV = require("./MarketDataFeederV3"); var _Streamer = _interopRequireDefault(require("./Streamer")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; } var _clearSubscriptions = /*#__PURE__*/new WeakSet(); class MarketDataStreamerV3 extends _Streamer.default { constructor(instrumentKeys = [], mode = "ltpc") { super(); _classPrivateMethodInitSpec(this, _clearSubscriptions); //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(); _classPrivateMethodGet(this, _clearSubscriptions, _clearSubscriptions2).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 _clearSubscriptions2() { this.subscriptions[this.Mode.LTPC].clear(); this.subscriptions[this.Mode.FULL].clear(); this.subscriptions[this.Mode.OPTION].clear(); this.subscriptions[this.Mode.D30].clear(); }