upstox-js-sdk
Version:
The official Node Js client for communicating with the Upstox API
121 lines (117 loc) • 5.87 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _events = require("events");
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
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 _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
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"); } /**
* Interface for classes that manage a WebSocket connection.
*/
var _enableAutoReconnect = /*#__PURE__*/new WeakMap();
var _interval = /*#__PURE__*/new WeakMap();
var _retryCount = /*#__PURE__*/new WeakMap();
var _reconnectInterval = /*#__PURE__*/new WeakMap();
var _reconnectOpenListener = /*#__PURE__*/new WeakMap();
var _reconnectCloseListener = /*#__PURE__*/new WeakMap();
var _Streamer_brand = /*#__PURE__*/new WeakSet();
class Streamer extends _events.EventEmitter {
constructor() {
super();
_classPrivateMethodInitSpec(this, _Streamer_brand);
_defineProperty(this, "Event", Object.freeze({
OPEN: "open",
CLOSE: "close",
MESSAGE: "message",
ERROR: "error",
RECONNECTING: "reconnecting",
AUTO_RECONNECT_STOPPED: "autoReconnectStopped"
}));
_classPrivateFieldInitSpec(this, _enableAutoReconnect, void 0);
_classPrivateFieldInitSpec(this, _interval, void 0);
_classPrivateFieldInitSpec(this, _retryCount, void 0);
_classPrivateFieldInitSpec(this, _reconnectInterval, void 0);
_classPrivateFieldInitSpec(this, _reconnectOpenListener, void 0);
_classPrivateFieldInitSpec(this, _reconnectCloseListener, void 0);
_classPrivateFieldSet(_enableAutoReconnect, this, true);
_classPrivateFieldSet(_interval, this, 1);
_classPrivateFieldSet(_retryCount, this, 5);
_classPrivateFieldSet(_reconnectInterval, this, null);
_classPrivateFieldSet(_reconnectOpenListener, this, null);
_classPrivateFieldSet(_reconnectCloseListener, this, null);
_assertClassBrand(_Streamer_brand, this, _prepareAutoReconnect).call(this);
if (new.target === Streamer) {
throw new TypeError("Cannot construct Streamer instances directly");
}
}
async connect() {
throw new Error("Method 'connect()' must be implemented.");
}
addEventsListeners() {
throw new Error("Method 'addEventsListeners()' must be implemented.");
}
setupEventListeners() {
this.streamer.on("open", () => this.emit(this.Event.OPEN));
this.streamer.on("data", data => {
this.emit(this.Event.MESSAGE, data);
});
this.streamer.on("error", error => this.emit(this.Event.ERROR, error));
this.streamer.on("close", () => this.emit(this.Event.CLOSE));
}
autoReconnect(enable, interval = 1, retryCount = 5) {
_classPrivateFieldSet(_enableAutoReconnect, this, enable);
_classPrivateFieldSet(_interval, this, interval);
_classPrivateFieldSet(_retryCount, this, retryCount);
if (!enable) {
this.emit(this.Event.AUTO_RECONNECT_STOPPED, "Stopped by client.");
return;
}
_assertClassBrand(_Streamer_brand, this, _prepareAutoReconnect).call(this);
}
}
function _prepareAutoReconnect() {
let counter = 0;
const attemptReconnect = async () => {
if (!_classPrivateFieldGet(_enableAutoReconnect, this)) {
clearInterval(_classPrivateFieldGet(_reconnectInterval, this));
return;
}
if (this.streamer.shouldReconnect()) {
this.emit(this.Event.RECONNECTING, `Auto reconnect attempt ${counter + 1}/${_classPrivateFieldGet(_retryCount, this)}`);
await this.connect();
counter++;
}
if (counter >= _classPrivateFieldGet(_retryCount, this)) {
clearInterval(_classPrivateFieldGet(_reconnectInterval, this));
this.streamer.clearSubscriptions();
this.emit(this.Event.AUTO_RECONNECT_STOPPED, `retryCount of ${_classPrivateFieldGet(_retryCount, this)} exhausted.`);
return;
}
};
// Remove existing listeners if they exist
if (_classPrivateFieldGet(_reconnectOpenListener, this)) {
this.removeListener("open", _classPrivateFieldGet(_reconnectOpenListener, this));
}
if (_classPrivateFieldGet(_reconnectCloseListener, this)) {
this.removeListener("close", _classPrivateFieldGet(_reconnectCloseListener, this));
}
// Define new listeners
_classPrivateFieldSet(_reconnectOpenListener, this, () => {
clearInterval(_classPrivateFieldGet(_reconnectInterval, this));
counter = 0;
});
_classPrivateFieldSet(_reconnectCloseListener, this, () => {
_classPrivateFieldSet(_reconnectInterval, this, setInterval(attemptReconnect, _classPrivateFieldGet(_interval, this) * 1000));
});
// Attach the new listeners
this.on("open", _classPrivateFieldGet(_reconnectOpenListener, this));
this.on("close", _classPrivateFieldGet(_reconnectCloseListener, this));
}
var _default = exports.default = Streamer;