aboutsummaryrefslogtreecommitdiffstats
path: root/packages/connect/src/orders_channel_factory.ts
blob: 5986d2a7727e502233b6d9ecfcd075315b1c6914 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 OrdersChannelHandler 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 ordersChannel = new WebSocketOrdersChannel(client, handler);
                resolve(ordersChannel);
            };
            client.onerror = err => {
                reject(err);
            };
        });
    },
};