Skip to content

Commit a0c6624

Browse files
fix: unsubscribe fails depending on channel id type
`Object.keys` casts to strings, channel ids can be are numbers, the compare function does not cast. in general its faster to do a direct object access instead of converting to an array and doing a search on the array.
1 parent e13ca2d commit a0c6624

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/transports/ws2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class WSv2 extends EventEmitter {
233233
* @returns {boolean} isSubscribed
234234
*/
235235
hasChannel (chanId) {
236-
return _includes(Object.keys(this._channelMap), chanId)
236+
return !!this._channelMap[chanId]
237237
}
238238

239239
/**

test/lib/transports/channels.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-env mocha */
2+
3+
'use strict'
4+
5+
const assert = require('assert')
6+
7+
const WSv2 = require('../../../lib/transports/ws2')
8+
9+
const API_KEY = 'dummy'
10+
const API_SECRET = 'dummy'
11+
12+
const createTestWSv2Instance = (params = {}) => {
13+
return new WSv2({
14+
apiKey: API_KEY,
15+
apiSecret: API_SECRET,
16+
url: 'ws://localhost:9997',
17+
...params
18+
})
19+
}
20+
21+
describe('WSv2 channels', () => {
22+
it('numeric and string channel ids work', () => {
23+
const ws = createTestWSv2Instance()
24+
25+
ws._channelMap = {
26+
83297: {
27+
event: 'subscribed',
28+
channel: 'book',
29+
chanId: 83297,
30+
symbol: 'tADAUSD',
31+
prec: 'P0',
32+
freq: 'F0',
33+
len: '25',
34+
pair: 'ADAUSD'
35+
}
36+
}
37+
38+
assert.strictEqual(ws.hasChannel(83297), true)
39+
assert.strictEqual(ws.hasChannel('83297'), true)
40+
assert.strictEqual(ws.hasChannel('1337'), false)
41+
assert.strictEqual(ws.hasChannel(1337), false)
42+
})
43+
})

0 commit comments

Comments
 (0)