-
Notifications
You must be signed in to change notification settings - Fork 320
Add Websocket#subscribe() and #unsubscribe() #213
Conversation
OrderbookSync now tracks new product subscriptions.
c78031e
to
2fd0dda
Compare
Just rebased this (and rewrote parts of the earlier commits) against all the new changes in |
lib/clients/websocket.js
Outdated
const message = { type }; | ||
|
||
if (channels) { | ||
message.channels = [...channels]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do this in onOpen
? No real need here to create copies of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the copies were because Sets were being used. I'll kill 'em.
tests/websocket.spec.js
Outdated
@@ -99,6 +99,117 @@ suite('WebsocketClient', () => { | |||
}); | |||
}); | |||
|
|||
test('subscribes to additional products', done => { | |||
var client; | |||
const server = testserver(++port, () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't we stop incrementing the port?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. This must have been left over from multiple rebases. I'll fix.
tests/websocket.spec.js
Outdated
@@ -99,6 +99,117 @@ suite('WebsocketClient', () => { | |||
}); | |||
}); | |||
|
|||
test('subscribes to additional products', done => { | |||
var client; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I'll fix.
lib/clients/websocket.js
Outdated
@@ -17,13 +17,11 @@ class WebsocketClient extends EventEmitter { | |||
{ channels = null } = {} | |||
) { | |||
super(); | |||
this.productIDs = Utils.determineProductIDs(productIDs); | |||
this.productIDs = new Set(Utils.determineProductIDs(productIDs)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what these sets are used for — why aren't we using plain arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sets are a better construct for unique lists (obviously), but if you don't like them, I'll clean them out.
@fb55 Requested changes applied. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more nit, otherwise lgtm
lib/orderbook_sync.js
Outdated
const channel = data.channels.find(c => c.name === 'full'); | ||
channel && channel.product_ids | ||
.filter(productID => !(productID in this.books)) | ||
.forEach(productID => this._newProduct(productID)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use same pattern as above (this.productIDs.forEach(this._newProduct, this);
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dig it. Good eye. Done.
Awesome, thanks :) |
What does it do?
Websocket#[un]subscribeProduct()
and#[un]subscribeChannel()
, the changes here simplify the method signatures down to just a stateless#subscribe()
and#unsubscribe()
that is a direct pass through to the GDAX API.subscribe
/unsubscribe
messages directly.Related Issues
Example Usage: