diff options
Diffstat (limited to 'packages/connect/src/orders_channel_factory.ts')
-rw-r--r-- | packages/connect/src/orders_channel_factory.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/connect/src/orders_channel_factory.ts b/packages/connect/src/orders_channel_factory.ts new file mode 100644 index 000000000..5e8e625e0 --- /dev/null +++ b/packages/connect/src/orders_channel_factory.ts @@ -0,0 +1,29 @@ +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 OrdersChannel Promise + */ + async createWebSocketOrdersChannelAsync(url: string, handler: OrdersChannelHandler): Promise<OrdersChannel> { + assert.isUri('url', url); + assert.isOrdersChannelHandler('handler', handler); + return new Promise<OrdersChannel>((resolve, reject) => { + const client = new WebSocket.w3cwebsocket(url); + client.onopen = () => { + const orderbookChannel = new WebSocketOrdersChannel(client, handler); + resolve(orderbookChannel); + }; + client.onerror = err => { + reject(err); + }; + }); + }, +}; |