Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Add Websocket#subscribe() and #unsubscribe() #213

Merged
merged 7 commits into from
Jan 22, 2018

Conversation

rmm5t
Copy link
Contributor

@rmm5t rmm5t commented Jan 9, 2018

What does it do?

Related Issues

Example Usage:

websocket.unsubscribe({ channels: ['full'] });

websocket.subscribe({ product_ids: ['LTC-USD'], channels: ['ticker', 'user'] });

websocket.subscribe({
  channels: [{
    name: 'user',
    product_ids: ['ETH-USD']
  }]
});

websocket.unsubscribe({
  channels: [{
    name: 'user',
    product_ids: ['LTC-USD']
  }, {
    name: 'user',
    product_ids: ['ETH-USD']
  }]
});

@rmm5t rmm5t force-pushed the websocket-subscribe-unsubscribe branch from c78031e to 2fd0dda Compare January 20, 2018 03:50
@rmm5t
Copy link
Contributor Author

rmm5t commented Jan 20, 2018

Just rebased this (and rewrote parts of the earlier commits) against all the new changes in master.

const message = { type };

if (channels) {
message.channels = [...channels];
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -99,6 +99,117 @@ suite('WebsocketClient', () => {
});
});

test('subscribes to additional products', done => {
var client;
const server = testserver(++port, () => {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@@ -99,6 +99,117 @@ suite('WebsocketClient', () => {
});
});

test('subscribes to additional products', done => {
var client;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let

Copy link
Contributor Author

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.

@@ -17,13 +17,11 @@ class WebsocketClient extends EventEmitter {
{ channels = null } = {}
) {
super();
this.productIDs = Utils.determineProductIDs(productIDs);
this.productIDs = new Set(Utils.determineProductIDs(productIDs));
Copy link
Contributor

@fb55 fb55 Jan 22, 2018

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?

Copy link
Contributor Author

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.

@rmm5t
Copy link
Contributor Author

rmm5t commented Jan 22, 2018

@fb55 Requested changes applied.

Copy link
Contributor

@fb55 fb55 left a 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

const channel = data.channels.find(c => c.name === 'full');
channel && channel.product_ids
.filter(productID => !(productID in this.books))
.forEach(productID => this._newProduct(productID));
Copy link
Contributor

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);)?

Copy link
Contributor Author

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.

@fb55 fb55 merged commit c14bdab into coinbase:master Jan 22, 2018
@fb55
Copy link
Contributor

fb55 commented Jan 22, 2018

Awesome, thanks :)

@rmm5t rmm5t deleted the websocket-subscribe-unsubscribe branch January 22, 2018 23:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants