From 64a85dfb9cc60212dcf1c70ca7be4874936dd332 Mon Sep 17 00:00:00 2001 From: fragosti Date: Thu, 16 Aug 2018 17:03:11 -0700 Subject: Rename websocket files and types --- packages/connect/src/orderbook_channel_factory.ts | 32 ------- packages/connect/src/orders_channel_factory.ts | 32 +++++++ packages/connect/src/types.ts | 18 ++-- packages/connect/src/utils/assert.ts | 4 +- packages/connect/src/ws_orderbook_channel.ts | 101 ---------------------- packages/connect/src/ws_orders_channel.ts | 101 ++++++++++++++++++++++ 6 files changed, 143 insertions(+), 145 deletions(-) delete mode 100644 packages/connect/src/orderbook_channel_factory.ts create mode 100644 packages/connect/src/orders_channel_factory.ts delete mode 100644 packages/connect/src/ws_orderbook_channel.ts create mode 100644 packages/connect/src/ws_orders_channel.ts diff --git a/packages/connect/src/orderbook_channel_factory.ts b/packages/connect/src/orderbook_channel_factory.ts deleted file mode 100644 index 5134af323..000000000 --- a/packages/connect/src/orderbook_channel_factory.ts +++ /dev/null @@ -1,32 +0,0 @@ -import * as WebSocket from 'websocket'; - -import { OrderbookChannel, OrderbookChannelHandler } from './types'; -import { assert } from './utils/assert'; -import { WebSocketOrderbookChannel } from './ws_orderbook_channel'; - -export const orderbookChannelFactory = { - /** - * Instantiates a new WebSocketOrderbookChannel instance - * @param url The relayer API base WS url you would like to interact with - * @param handler An OrderbookChannelHandler instance that responds to various - * channel updates - * @return An OrderbookChannel Promise - */ - async createWebSocketOrderbookChannelAsync( - url: string, - handler: OrderbookChannelHandler, - ): Promise { - assert.isUri('url', url); - assert.isOrderbookChannelHandler('handler', handler); - return new Promise((resolve, reject) => { - const client = new WebSocket.w3cwebsocket(url); - client.onopen = () => { - const orderbookChannel = new WebSocketOrderbookChannel(client, handler); - resolve(orderbookChannel); - }; - client.onerror = err => { - reject(err); - }; - }); - }, -}; diff --git a/packages/connect/src/orders_channel_factory.ts b/packages/connect/src/orders_channel_factory.ts new file mode 100644 index 000000000..4e9b74b2c --- /dev/null +++ b/packages/connect/src/orders_channel_factory.ts @@ -0,0 +1,32 @@ +import * as WebSocket from 'websocket'; + +import { OrdersChannel, OrdersChannelHandler } from './types'; +import { assert } from './utils/assert'; +import { WebSocketOrdersChannel } from './ws_orders_channel'; + +export const ordersChannelFactory = { + /** + * Instantiates a new WebSocketOrdersChannel instance + * @param url The relayer API base WS url you would like to interact with + * @param handler An OrderbookChannelHandler instance that responds to various + * channel updates + * @return An OrderbookChannel Promise + */ + async createWebSocketOrdersChannelAsync( + url: string, + handler: OrdersChannelHandler, + ): Promise { + assert.isUri('url', url); + assert.isOrderbookChannelHandler('handler', handler); + return new Promise((resolve, reject) => { + const client = new WebSocket.w3cwebsocket(url); + client.onopen = () => { + const orderbookChannel = new WebSocketOrdersChannel(client, handler); + resolve(orderbookChannel); + }; + client.onerror = err => { + reject(err); + }; + }); + }, +}; diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts index 44ea4abd6..e85a0542c 100644 --- a/packages/connect/src/types.ts +++ b/packages/connect/src/types.ts @@ -11,32 +11,30 @@ export interface Client { submitOrderAsync: (signedOrder: SignedOrder) => Promise; } -export interface OrderbookChannel { - subscribe: (subscriptionOpts: OrderbookChannelSubscriptionOpts) => void; +export interface OrdersChannel { + subscribe: (subscriptionOpts: OrdersChannelSubscriptionOpts) => void; close: () => void; } /** * baseAssetData: The address of assetData designated as the baseToken in the currency pair calculation of price * quoteAssetData: The address of assetData designated as the quoteToken in the currency pair calculation of price - * snapshot: If true, a snapshot of the orderbook will be sent before the updates to the orderbook * limit: Maximum number of bids and asks in orderbook snapshot */ -export interface OrderbookChannelSubscriptionOpts { +export interface OrdersChannelSubscriptionOpts { baseAssetData: string; quoteAssetData: string; - snapshot: boolean; limit: number; } -export interface OrderbookChannelHandler { +export interface OrdersChannelHandler { onUpdate: ( - channel: OrderbookChannel, - subscriptionOpts: OrderbookChannelSubscriptionOpts, + channel: OrdersChannel, + subscriptionOpts: OrdersChannelSubscriptionOpts, order: APIOrder, ) => void; - onError: (channel: OrderbookChannel, err: Error, subscriptionOpts?: OrderbookChannelSubscriptionOpts) => void; - onClose: (channel: OrderbookChannel) => void; + onError: (channel: OrdersChannel, err: Error, subscriptionOpts?: OrdersChannelSubscriptionOpts) => void; + onClose: (channel: OrdersChannel) => void; } export type OrdersChannelMessage = diff --git a/packages/connect/src/utils/assert.ts b/packages/connect/src/utils/assert.ts index 353b7f29f..3d8f1c799 100644 --- a/packages/connect/src/utils/assert.ts +++ b/packages/connect/src/utils/assert.ts @@ -10,14 +10,14 @@ import * as _ from 'lodash'; export const assert = { ...sharedAssert, - isOrderbookChannelSubscriptionOpts(variableName: string, subscriptionOpts: any): void { + isOrdersChannelSubscriptionOpts(variableName: string, subscriptionOpts: any): void { sharedAssert.doesConformToSchema( variableName, subscriptionOpts, schemas.relayerApiOrdersChannelSubscribePayload, ); }, - isOrderbookChannelHandler(variableName: string, handler: any): void { + isOrdersChannelHandler(variableName: string, handler: any): void { sharedAssert.isFunction(`${variableName}.onUpdate`, _.get(handler, 'onUpdate')); sharedAssert.isFunction(`${variableName}.onError`, _.get(handler, 'onError')); sharedAssert.isFunction(`${variableName}.onClose`, _.get(handler, 'onClose')); diff --git a/packages/connect/src/ws_orderbook_channel.ts b/packages/connect/src/ws_orderbook_channel.ts deleted file mode 100644 index 425ba8afb..000000000 --- a/packages/connect/src/ws_orderbook_channel.ts +++ /dev/null @@ -1,101 +0,0 @@ -import * as _ from 'lodash'; -import { v4 as uuid } from 'uuid'; -import * as WebSocket from 'websocket'; - -import { - OrderbookChannel, - OrderbookChannelHandler, - OrderbookChannelSubscriptionOpts, - OrdersChannelMessageTypes, -} from './types'; -import { assert } from './utils/assert'; -import { ordersChannelMessageParser } 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 readonly _client: WebSocket.w3cwebsocket; - private readonly _handler: OrderbookChannelHandler; - private readonly _subscriptionOptsList: OrderbookChannelSubscriptionOpts[] = []; - /** - * Instantiates a new WebSocketOrderbookChannel instance - * @param client A WebSocket client - * @param handler An OrderbookChannelHandler instance that responds to various - * channel updates - * @return An instance of WebSocketOrderbookChannel - */ - constructor(client: WebSocket.w3cwebsocket, handler: OrderbookChannelHandler) { - assert.isOrderbookChannelHandler('handler', handler); - // set private members - this._client = client; - this._handler = handler; - // attach client callbacks - this._client.onerror = err => { - this._handler.onError(this, err); - }; - this._client.onclose = () => { - this._handler.onClose(this); - }; - this._client.onmessage = message => { - this._handleWebSocketMessage(message); - }; - } - /** - * Subscribe to orderbook snapshots and updates from the websocket - * @param subscriptionOpts An OrderbookChannelSubscriptionOpts instance describing which - * assetData pair to subscribe to - */ - public subscribe(subscriptionOpts: OrderbookChannelSubscriptionOpts): void { - assert.isOrderbookChannelSubscriptionOpts('subscriptionOpts', subscriptionOpts); - assert.assert(this._client.readyState === WebSocket.w3cwebsocket.OPEN, 'WebSocket connection is closed'); - this._subscriptionOptsList.push(subscriptionOpts); - const subscribeMessage = { - type: 'subscribe', - channel: 'orderbook', - requestId: uuid(), - payload: subscriptionOpts, - }; - this._client.send(JSON.stringify(subscribeMessage)); - } - /** - * Close the websocket and stop receiving updates - */ - public close(): void { - this._client.close(); - } - private _handleWebSocketMessage(message: any): void { - if (_.isUndefined(message.data)) { - this._handler.onError(this, new Error(`Message does not contain data. Url: ${this._client.url}`)); - return; - } - try { - const data = message.data; - const parserResult = ordersChannelMessageParser.parse(data); - const subscriptionOpts = this._subscriptionOptsList[parserResult.requestId]; - if (_.isUndefined(subscriptionOpts)) { - this._handler.onError( - this, - new Error(`Message has unknown requestId. Url: ${this._client.url} Message: ${data}`), - ); - return; - } - switch (parserResult.type) { - case OrdersChannelMessageTypes.Update: { - this._handler.onUpdate(this, subscriptionOpts, parserResult.payload); - break; - } - default: { - this._handler.onError( - this, - new Error(`Message has unknown type parameter. Url: ${this._client.url} Message: ${data}`), - subscriptionOpts, - ); - } - } - } catch (error) { - this._handler.onError(this, error); - } - } -} diff --git a/packages/connect/src/ws_orders_channel.ts b/packages/connect/src/ws_orders_channel.ts new file mode 100644 index 000000000..9d45b6570 --- /dev/null +++ b/packages/connect/src/ws_orders_channel.ts @@ -0,0 +1,101 @@ +import * as _ from 'lodash'; +import { v4 as uuid } from 'uuid'; +import * as WebSocket from 'websocket'; + +import { + OrdersChannel, + OrdersChannelHandler, + OrdersChannelMessageTypes, + OrdersChannelSubscriptionOpts, +} from './types'; +import { assert } from './utils/assert'; +import { ordersChannelMessageParser } 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 WebSocketOrdersChannel implements OrdersChannel { + private readonly _client: WebSocket.w3cwebsocket; + private readonly _handler: OrdersChannelHandler; + private readonly _subscriptionOptsList: OrdersChannelSubscriptionOpts[] = []; + /** + * Instantiates a new WebSocketOrdersChannel instance + * @param client A WebSocket client + * @param handler An OrdersChannelHandler instance that responds to various + * channel updates + * @return An instance of WebSocketOrdersChannel + */ + constructor(client: WebSocket.w3cwebsocket, handler: OrdersChannelHandler) { + assert.isOrdersChannelHandler('handler', handler); + // set private members + this._client = client; + this._handler = handler; + // attach client callbacks + this._client.onerror = err => { + this._handler.onError(this, err); + }; + this._client.onclose = () => { + this._handler.onClose(this); + }; + this._client.onmessage = message => { + this._handleWebSocketMessage(message); + }; + } + /** + * Subscribe to orderbook snapshots and updates from the websocket + * @param subscriptionOpts An OrdersChannelSubscriptionOpts instance describing which + * assetData pair to subscribe to + */ + public subscribe(subscriptionOpts: OrdersChannelSubscriptionOpts): void { + assert.isOrdersChannelSubscriptionOpts('subscriptionOpts', subscriptionOpts); + assert.assert(this._client.readyState === WebSocket.w3cwebsocket.OPEN, 'WebSocket connection is closed'); + this._subscriptionOptsList.push(subscriptionOpts); + const subscribeMessage = { + type: 'subscribe', + channel: 'orders', + requestId: uuid(), + payload: subscriptionOpts, + }; + this._client.send(JSON.stringify(subscribeMessage)); + } + /** + * Close the websocket and stop receiving updates + */ + public close(): void { + this._client.close(); + } + private _handleWebSocketMessage(message: any): void { + if (_.isUndefined(message.data)) { + this._handler.onError(this, new Error(`Message does not contain data. Url: ${this._client.url}`)); + return; + } + try { + const data = message.data; + const parserResult = ordersChannelMessageParser.parse(data); + const subscriptionOpts = this._subscriptionOptsList[parserResult.requestId]; + if (_.isUndefined(subscriptionOpts)) { + this._handler.onError( + this, + new Error(`Message has unknown requestId. Url: ${this._client.url} Message: ${data}`), + ); + return; + } + switch (parserResult.type) { + case OrdersChannelMessageTypes.Update: { + this._handler.onUpdate(this, subscriptionOpts, parserResult.payload); + break; + } + default: { + this._handler.onError( + this, + new Error(`Message has unknown type parameter. Url: ${this._client.url} Message: ${data}`), + subscriptionOpts, + ); + } + } + } catch (error) { + this._handler.onError(this, error); + } + } +} -- cgit v1.2.3