Skip to content

Commit

Permalink
fix(websocket): allow subscription to provide top level markets
Browse files Browse the repository at this point in the history
  • Loading branch information
bradennapier committed Aug 31, 2020
1 parent 8722f93 commit 9fe4fe0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
22 changes: 20 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,22 @@ const webSocketClient = new idex.WebSocketClient({
await webSocketClient.connect();
```
##### subscribe
- **See: [WebSocket Subscriptions](https://docs.idex.io/#websocket-subscriptions)
**
Subscribe to a given set of subscriptions, optionally providing a list of top level
markets or a cid property.
###### Parameters
- `subscriptions` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<AuthTokenWebSocketRequestAuthenticatedSubscription>**
- `markets` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>?** Optionally provide top level markets
- `cid` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** A custom identifier to identify the matching response
Returns **this**
##### subscribeAuthenticated
Strictly typed subscribe which only can be used on authenticated subscriptions.
Expand All @@ -665,7 +681,8 @@ See [API specification](https://docs.idex.io/#get-authentication-token)
###### Parameters
- `subscriptions` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<AuthTokenWebSocketRequestAuthenticatedSubscription>**
- `cid` **\[[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)]** A custom identifier to identify the matching response
- `markets` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>?** Optionally provide top level markets
- `cid` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** A custom identifier to identify the matching response
Returns **this**
Expand All @@ -676,7 +693,8 @@ Subscribe which only can be used on non-authenticated subscriptions
###### Parameters
- `subscriptions` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<WebSocketRequestUnauthenticatedSubscription>**
- `cid` **\[[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)]** A custom identifier to identify the matching response
- `markets` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>?** Optionally provide top level markets
- `cid` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** A custom identifier to identify the matching response
Returns **this**
Expand Down
38 changes: 32 additions & 6 deletions src/client/webSocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,25 @@ export class WebSocketClient {
return this.sendMessage({ method: 'subscriptions' });
}

/**
* Subscribe to a given set of subscriptions, optionally providing a list of top level
* markets or a cid property.
*
* @see {@link https://docs.idex.io/#websocket-subscriptions|WebSocket Subscriptions}
*
* @param {AuthTokenWebSocketRequestAuthenticatedSubscription[]} subscriptions
* @param {string[]} [markets] - Optionally provide top level markets
* @param {string} [cid] - A custom identifier to identify the matching response
*/
public subscribe(
subscriptions: Array<
| types.AuthTokenWebSocketRequestSubscription
| types.WebSocketRequestSubscribeShortNames
>,
markets?: string[],
cid?: string,
): this {
this.subscribeRequest(subscriptions, cid).catch((error) => {
this.subscribeRequest(subscriptions, markets, cid).catch((error) => {
this.handleWebSocketError({
error,
message: error.message,
Expand All @@ -221,37 +232,43 @@ export class WebSocketClient {
* See {@link https://docs.idex.io/#get-authentication-token|API specification}
*
* @param {AuthTokenWebSocketRequestAuthenticatedSubscription[]} subscriptions
* @param {[string]} cid - A custom identifier to identify the matching response
* @param {string[]} [markets] - Optionally provide top level markets
* @param {string} [cid] - A custom identifier to identify the matching response
*/
public subscribeAuthenticated(
subscriptions: types.AuthTokenWebSocketRequestAuthenticatedSubscription[],
markets?: string[],
cid?: string,
): this {
this.subscribe(subscriptions, cid);
this.subscribe(subscriptions, markets, cid);
return this;
}

/**
* Subscribe which only can be used on non-authenticated subscriptions
*
* @param {WebSocketRequestUnauthenticatedSubscription[]} subscriptions
* @param {[string]} cid - A custom identifier to identify the matching response
* @param {string[]} [markets] - Optionally provide top level markets
* @param {string} [cid] - A custom identifier to identify the matching response
*/
public subscribeUnauthenticated(
subscriptions: types.WebSocketRequestUnauthenticatedSubscription[],
markets?: string[],
cid?: string,
): this {
this.subscribe(subscriptions, cid);
this.subscribe(subscriptions, markets, cid);
return this;
}

public unsubscribe(
subscriptions: types.WebSocketRequestUnsubscribeSubscription[],
markets?: string[],
cid?: string,
): this {
return this.sendMessage({
cid,
method: 'unsubscribe',
markets,
subscriptions,
});
}
Expand All @@ -263,6 +280,7 @@ export class WebSocketClient {
| types.AuthTokenWebSocketRequestSubscription
| types.WebSocketRequestSubscribeShortNames
>,
markets?: string[],
cid?: string,
): Promise<this> {
const authSubscriptions = subscriptions.filter(
Expand All @@ -271,7 +289,12 @@ export class WebSocketClient {

// Public subscriptions can be subscribed all at once
if (authSubscriptions.length === 0) {
return this.sendMessage({ cid, method: 'subscribe', subscriptions });
return this.sendMessage({
cid,
method: 'subscribe',
markets,
subscriptions,
});
}

const { webSocketTokenManager } = this;
Expand Down Expand Up @@ -308,6 +331,7 @@ export class WebSocketClient {
return this.sendMessage({
cid,
method: 'subscribe',
markets,
subscriptions: subscriptions.map(removeWalletFromSdkSubscription),
token: webSocketTokenManager.getLastCachedToken(uniqueWallets[0]),
});
Expand All @@ -322,6 +346,7 @@ export class WebSocketClient {
this.sendMessage({
cid,
method: 'subscribe',
markets,
subscriptions: publicSubscriptions,
});
}
Expand All @@ -331,6 +356,7 @@ export class WebSocketClient {
this.sendMessage({
cid,
method: 'subscribe',
markets,
subscriptions: [removeWalletFromSdkSubscription(authSubscription)],
token: webSocketTokenManager.getLastCachedToken(
authSubscription.wallet,
Expand Down

0 comments on commit 9fe4fe0

Please sign in to comment.