Skip to content

Commit 0f70936

Browse files
authored
Merge pull request #143 from brandonros/patch-3
expand QuoteMarket to accept extended session
2 parents 392a08a + c16ab85 commit 0f70936

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/quote/market.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
/**
66
* @param {import('./session').QuoteSessionBridge} quoteSession
77
*/
8+
89
module.exports = (quoteSession) => class QuoteMarket {
910
#symbolListeners = quoteSession.symbolListeners;
1011

1112
#symbol;
1213

14+
#session;
15+
16+
#symbolKey;
17+
1318
#symbolListenerID = 0;
1419

1520
#lastData = {};
@@ -38,20 +43,24 @@ module.exports = (quoteSession) => class QuoteMarket {
3843

3944
/**
4045
* @param {string} symbol Market symbol (like: 'BTCEUR' or 'KRAKEN:BTCEUR')
46+
* @param {string} session Market session (like: 'regular' or 'extended')
4147
*/
42-
constructor(symbol) {
48+
constructor(symbol, session = 'regular') {
4349
this.#symbol = symbol;
50+
this.#session = session;
51+
this.#symbolKey = `=${JSON.stringify({ session, symbol })}`;
4452

45-
if (!this.#symbolListeners[symbol]) {
46-
this.#symbolListeners[symbol] = [];
53+
if (!this.#symbolListeners[this.#symbolKey]) {
54+
this.#symbolListeners[this.#symbolKey] = [];
4755
quoteSession.send('quote_add_symbols', [
4856
quoteSession.sessionID,
49-
symbol,
57+
this.#symbolKey,
5058
]);
5159
}
52-
this.#symbolListenerID = this.#symbolListeners[symbol].length;
5360

54-
this.#symbolListeners[symbol][this.#symbolListenerID] = (packet) => {
61+
this.#symbolListenerID = this.#symbolListeners[this.#symbolKey].length;
62+
63+
this.#symbolListeners[this.#symbolKey][this.#symbolListenerID] = (packet) => {
5564
if (global.TW_DEBUG) console.log('§90§30§105 MARKET §0 DATA', packet);
5665

5766
if (packet.type === 'qsd' && packet.data[1].s === 'ok') {
@@ -112,12 +121,12 @@ module.exports = (quoteSession) => class QuoteMarket {
112121

113122
/** Close this listener */
114123
close() {
115-
if (this.#symbolListeners[this.#symbol].length <= 1) {
124+
if (this.#symbolListeners[this.#symbolKey].length <= 1) {
116125
quoteSession.send('quote_remove_symbols', [
117126
quoteSession.sessionID,
118-
this.#symbol,
127+
this.#symbolKey,
119128
]);
120129
}
121-
delete this.#symbolListeners[this.#symbol][this.#symbolListenerID];
130+
delete this.#symbolListeners[this.#symbolKey][this.#symbolListenerID];
122131
}
123132
};

src/quote/session.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ module.exports = (client) => class QuoteSession {
7979
if (global.TW_DEBUG) console.log('§90§30§102 QUOTE SESSION §0 DATA', packet);
8080

8181
if (packet.type === 'quote_completed') {
82-
const symbol = packet.data[1];
83-
if (!this.#symbolListeners[symbol]) {
84-
this.#client.send('quote_remove_symbols', [this.#sessionID, symbol]);
82+
const symbolKey = packet.data[1];
83+
if (!this.#symbolListeners[symbolKey]) {
84+
this.#client.send('quote_remove_symbols', [this.#sessionID, symbolKey]);
8585
return;
8686
}
87-
this.#symbolListeners[symbol].forEach((h) => h(packet));
87+
this.#symbolListeners[symbolKey].forEach((h) => h(packet));
8888
}
8989

9090
if (packet.type === 'qsd') {
91-
const symbol = packet.data[1].n;
92-
if (!this.#symbolListeners[symbol]) {
93-
this.#client.send('quote_remove_symbols', [this.#sessionID, symbol]);
91+
const symbolKey = packet.data[1].n;
92+
if (!this.#symbolListeners[symbolKey]) {
93+
this.#client.send('quote_remove_symbols', [this.#sessionID, symbolKey]);
9494
return;
9595
}
96-
this.#symbolListeners[symbol].forEach((h) => h(packet));
96+
this.#symbolListeners[symbolKey].forEach((h) => h(packet));
9797
}
9898
},
9999
};

0 commit comments

Comments
 (0)