Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Added callback param to end() method, isEnded flag - to stop ws requests

Yerofey Sf1452fa48f76
diff --git a/main.js b/main.js
index 5ac47b4..ff859f4 100644
--- a/main.js
+++ b/main.js
@@ -88,6 +88,9 @@ module.exports = (autoInit = true) => {
/** List of subscribed symbols */
let subscribed = [];
+ /** Stop WS calls */
+ let isEnded = false;
+
/**
* Send a custom packet
* @param {string} t Packet type
@@ -183,9 +186,16 @@ module.exports = (autoInit = true) => {
/**
* Close the websocket connection
- * @param {string} symbol Market symbol (Example: BTCEUR or COINBASE:BTCEUR)
+ * @param {function} callback Callback function to execute after websocket is closed
*/
- end() { ws.close(); },
+ end(callback) {
+ isEnded = true;
+ ws.close();
+
+ if (typeof callback === 'function') {
+ callback();
+ }
+ },
search,
getScreener,
@@ -286,6 +296,10 @@ module.exports = (autoInit = true) => {
}
chartCBs[chartSession] = (packet) => {
+ if (isEnded) {
+ return;
+ }
+
if (['timescale_update', 'du'].includes(packet.type)) {
updatePeriods(packet);
onUpdate([...periods].reverse());