| 55 | * @param {import('../client').ClientBridge} client |
| 56 | */ |
| 57 | module.exports = (client) => class QuoteSession { |
| 58 | #sessionID = genSessionID('qs'); |
| 59 | |
| 60 | /** Parent client */ |
| 61 | #client = client; |
| 62 | |
| 63 | /** @type {SymbolListeners} */ |
| 64 | #symbolListeners = {}; |
| 65 | |
| 66 | /** |
| 67 | * @typedef {Object} quoteSessionOptions Quote Session options |
| 68 | * @prop {'all' | 'price'} [fields] Asked quote fields |
| 69 | * @prop {quoteField[]} [customFields] List of asked quote fields |
| 70 | */ |
| 71 | |
| 72 | /** |
| 73 | * @param {quoteSessionOptions} options Quote settings options |
| 74 | */ |
| 75 | constructor(options = {}) { |
| 76 | this.#client.sessions[this.#sessionID] = { |
| 77 | type: 'quote', |
| 78 | onData: (packet) => { |
| 79 | if (global.TW_DEBUG) console.log('§90§30§102 QUOTE SESSION §0 DATA', packet); |
| 80 | |
| 81 | if (packet.type === 'quote_completed') { |
| 82 | const symbolKey = packet.data[1]; |
| 83 | if (!this.#symbolListeners[symbolKey]) { |
| 84 | this.#client.send('quote_remove_symbols', [this.#sessionID, symbolKey]); |
| 85 | return; |
| 86 | } |
| 87 | this.#symbolListeners[symbolKey].forEach((h) => h(packet)); |
| 88 | } |
| 89 | |
| 90 | if (packet.type === 'qsd') { |
| 91 | const symbolKey = packet.data[1].n; |
| 92 | if (!this.#symbolListeners[symbolKey]) { |
| 93 | this.#client.send('quote_remove_symbols', [this.#sessionID, symbolKey]); |
| 94 | return; |
| 95 | } |
| 96 | this.#symbolListeners[symbolKey].forEach((h) => h(packet)); |
| 97 | } |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | const fields = (options.customFields && options.customFields.length > 0 |
| 102 | ? options.customFields |
| 103 | : getQuoteFields(options.fields) |
| 104 | ); |
| 105 | |
| 106 | this.#client.send('quote_create_session', [this.#sessionID]); |
| 107 | this.#client.send('quote_set_fields', [this.#sessionID, ...fields]); |
| 108 | } |
| 109 | |
| 110 | /** @type {QuoteSessionBridge} */ |
| 111 | #quoteSession = { |
| 112 | sessionID: this.#sessionID, |
| 113 | symbolListeners: this.#symbolListeners, |
| 114 | send: (t, p) => this.#client.send(t, p), |
nothing calls this directly
no test coverage detected