aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/ws_orderbook_channel.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/connect/src/ws_orderbook_channel.ts')
-rw-r--r--packages/connect/src/ws_orderbook_channel.ts66
1 files changed, 37 insertions, 29 deletions
diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts
index 6687025c0..822a022f4 100644
--- a/packages/connect/src/ws_orderbook_channel.ts
+++ b/packages/connect/src/ws_orderbook_channel.ts
@@ -1,5 +1,5 @@
-import {assert} from '@0xproject/assert';
-import {schemas} from '@0xproject/json-schemas';
+import { assert } from '@0xproject/assert';
+import { schemas } from '@0xproject/json-schemas';
import * as _ from 'lodash';
import * as WebSocket from 'websocket';
@@ -8,21 +8,20 @@ import {
OrderbookChannelHandler,
OrderbookChannelMessageTypes,
OrderbookChannelSubscriptionOpts,
- SignedOrder,
WebsocketClientEventType,
WebsocketConnectionEventType,
} from './types';
-import {orderbookChannelMessageParsers} from './utils/orderbook_channel_message_parsers';
+import { orderbookChannelMessageParser } from './utils/orderbook_channel_message_parser';
/**
* This class includes all the functionality related to interacting with a websocket endpoint
* that implements the standard relayer API v0
*/
export class WebSocketOrderbookChannel implements OrderbookChannel {
- private apiEndpointUrl: string;
- private client: WebSocket.client;
- private connectionIfExists?: WebSocket.connection;
- private subscriptionCounter = 0;
+ private _apiEndpointUrl: string;
+ private _client: WebSocket.client;
+ private _connectionIfExists?: WebSocket.connection;
+ private _subscriptionCounter = 0;
/**
* Instantiates a new WebSocketOrderbookChannel instance
* @param url The relayer API base WS url you would like to interact with
@@ -30,8 +29,8 @@ export class WebSocketOrderbookChannel implements OrderbookChannel {
*/
constructor(url: string) {
assert.isUri('url', url);
- this.apiEndpointUrl = url;
- this.client = new WebSocket.client();
+ this._apiEndpointUrl = url;
+ this._client = new WebSocket.client();
}
/**
* Subscribe to orderbook snapshots and updates from the websocket
@@ -42,16 +41,19 @@ export class WebSocketOrderbookChannel implements OrderbookChannel {
*/
public subscribe(subscriptionOpts: OrderbookChannelSubscriptionOpts, handler: OrderbookChannelHandler): void {
assert.doesConformToSchema(
- 'subscriptionOpts', subscriptionOpts, schemas.relayerApiOrderbookChannelSubscribePayload);
+ 'subscriptionOpts',
+ subscriptionOpts,
+ schemas.relayerApiOrderbookChannelSubscribePayload,
+ );
assert.isFunction('handler.onSnapshot', _.get(handler, 'onSnapshot'));
assert.isFunction('handler.onUpdate', _.get(handler, 'onUpdate'));
assert.isFunction('handler.onError', _.get(handler, 'onError'));
assert.isFunction('handler.onClose', _.get(handler, 'onClose'));
- this.subscriptionCounter += 1;
+ this._subscriptionCounter += 1;
const subscribeMessage = {
type: 'subscribe',
channel: 'orderbook',
- requestId: this.subscriptionCounter,
+ requestId: this._subscriptionCounter,
payload: subscriptionOpts,
};
this._getConnection((error, connection) => {
@@ -75,44 +77,50 @@ export class WebSocketOrderbookChannel implements OrderbookChannel {
* Close the websocket and stop receiving updates
*/
public close() {
- if (!_.isUndefined(this.connectionIfExists)) {
- this.connectionIfExists.close();
+ if (!_.isUndefined(this._connectionIfExists)) {
+ this._connectionIfExists.close();
}
}
private _getConnection(callback: (error?: Error, connection?: WebSocket.connection) => void) {
- if (!_.isUndefined(this.connectionIfExists) && this.connectionIfExists.connected) {
- callback(undefined, this.connectionIfExists);
+ if (!_.isUndefined(this._connectionIfExists) && this._connectionIfExists.connected) {
+ callback(undefined, this._connectionIfExists);
} else {
- this.client.on(WebsocketClientEventType.Connect, connection => {
- this.connectionIfExists = connection;
- callback(undefined, this.connectionIfExists);
+ this._client.on(WebsocketClientEventType.Connect, connection => {
+ this._connectionIfExists = connection;
+ callback(undefined, this._connectionIfExists);
});
- this.client.on(WebsocketClientEventType.ConnectFailed, error => {
+ this._client.on(WebsocketClientEventType.ConnectFailed, error => {
callback(error, undefined);
});
- this.client.connect(this.apiEndpointUrl);
+ this._client.connect(this._apiEndpointUrl);
}
}
- private _handleWebSocketMessage(requestId: number, subscriptionOpts: OrderbookChannelSubscriptionOpts,
- message: WebSocket.IMessage, handler: OrderbookChannelHandler): void {
+ private _handleWebSocketMessage(
+ requestId: number,
+ subscriptionOpts: OrderbookChannelSubscriptionOpts,
+ message: WebSocket.IMessage,
+ handler: OrderbookChannelHandler,
+ ): void {
if (!_.isUndefined(message.utf8Data)) {
try {
const utf8Data = message.utf8Data;
- const parserResult = orderbookChannelMessageParsers.parser(utf8Data);
- const type = parserResult.type;
+ const parserResult = orderbookChannelMessageParser.parse(utf8Data);
if (parserResult.requestId === requestId) {
switch (parserResult.type) {
- case (OrderbookChannelMessageTypes.Snapshot): {
+ case OrderbookChannelMessageTypes.Snapshot: {
handler.onSnapshot(this, subscriptionOpts, parserResult.payload);
break;
}
- case (OrderbookChannelMessageTypes.Update): {
+ case OrderbookChannelMessageTypes.Update: {
handler.onUpdate(this, subscriptionOpts, parserResult.payload);
break;
}
default: {
handler.onError(
- this, subscriptionOpts, new Error(`Message has missing a type parameter: ${utf8Data}`));
+ this,
+ subscriptionOpts,
+ new Error(`Message has missing a type parameter: ${utf8Data}`),
+ );
}
}
}