minor changes/typo fixes
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
"test:circleci": "yarn test:coverage",
|
||||
"lint": "tslint --project . --format stylish"
|
||||
},
|
||||
"author": "",
|
||||
"author": "Jacob Evans",
|
||||
"license": "Apache-2.0",
|
||||
"devDependencies": {
|
||||
"@0x/tslint-config": "^3.0.1",
|
||||
|
||||
@@ -7,7 +7,7 @@ import { utils } from '../utils';
|
||||
|
||||
import { BaseOrderProvider } from './base_order_provider';
|
||||
|
||||
export class ProvidedOrdersOrderProvider extends BaseOrderProvider {
|
||||
export class CustomOrderProvider extends BaseOrderProvider {
|
||||
constructor(orders: SignedOrder[], orderStore: OrderStore) {
|
||||
super(orderStore);
|
||||
void this.addOrdersAsync(orders);
|
||||
@@ -81,7 +81,7 @@ export class MeshOrderProvider extends BaseOrderProvider {
|
||||
* @param takerAssetData the Taker Asset Data
|
||||
*/
|
||||
public async createSubscriptionForAssetPairAsync(_makerAssetData: string, _takerAssetData: string): Promise<void> {
|
||||
// Create the subscription first to get any updates during waiting for the request
|
||||
// Create the subscription first to get any updates while waiting for the request
|
||||
await this._initializeIfRequiredAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { assert } from '@0x/assert';
|
||||
import { intervalUtils } from '@0x/utils';
|
||||
|
||||
import { OrderSet } from '../order_set';
|
||||
import { OrderStore } from '../order_store';
|
||||
@@ -6,10 +7,10 @@ import { SRAPollingOrderProviderOpts } from '../types';
|
||||
|
||||
import { BaseSRAOrderProvider } from './base_sra_order_provider';
|
||||
|
||||
const RECORD_COUNT = 100;
|
||||
const PER_PAGE_DEFAULT = 100;
|
||||
|
||||
export class SRAPollingOrderProvider extends BaseSRAOrderProvider {
|
||||
private readonly _httpPollingIntervalId: Map<string, number> = new Map();
|
||||
private readonly _assetPairKeyToPollingIntervalId: Map<string, number> = new Map();
|
||||
private readonly _pollingIntervalMs: number;
|
||||
|
||||
/**
|
||||
@@ -19,7 +20,7 @@ export class SRAPollingOrderProvider extends BaseSRAOrderProvider {
|
||||
* @param orderStore The `OrderStore` where orders are added and removed from
|
||||
*/
|
||||
constructor(opts: SRAPollingOrderProviderOpts, orderStore: OrderStore) {
|
||||
super(orderStore, opts.httpEndpoint, opts.perPage || RECORD_COUNT, opts.networkId);
|
||||
super(orderStore, opts.httpEndpoint, opts.perPage || PER_PAGE_DEFAULT, opts.networkId);
|
||||
assert.isNumber('pollingIntervalMs', opts.pollingIntervalMs);
|
||||
this._pollingIntervalMs = opts.pollingIntervalMs;
|
||||
}
|
||||
@@ -32,7 +33,7 @@ export class SRAPollingOrderProvider extends BaseSRAOrderProvider {
|
||||
public async createSubscriptionForAssetPairAsync(makerAssetData: string, takerAssetData: string): Promise<void> {
|
||||
const assetPairKey = OrderStore.getKeyForAssetPair(makerAssetData, takerAssetData);
|
||||
// Do nothing if we already have a polling interval or websocket created for this asset pair
|
||||
if (this._httpPollingIntervalId.has(assetPairKey)) {
|
||||
if (this._assetPairKeyToPollingIntervalId.has(assetPairKey)) {
|
||||
return;
|
||||
}
|
||||
await this._fetchAndCreatePollingAsync(makerAssetData, takerAssetData);
|
||||
@@ -42,14 +43,14 @@ export class SRAPollingOrderProvider extends BaseSRAOrderProvider {
|
||||
* Destroys the order provider, removing any subscriptions
|
||||
*/
|
||||
public async destroyAsync(): Promise<void> {
|
||||
for (const [assetPairKey, id] of this._httpPollingIntervalId) {
|
||||
for (const [assetPairKey, id] of this._assetPairKeyToPollingIntervalId) {
|
||||
clearInterval(id);
|
||||
this._httpPollingIntervalId.delete(assetPairKey);
|
||||
this._assetPairKeyToPollingIntervalId.delete(assetPairKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all of the orders for both sides of the orderbook and stores. A polling subscription
|
||||
* Fetches all of the orders for both sides of the orderbook and stores them. A polling subscription
|
||||
* is created performing this action every pollingIntervalMs
|
||||
*/
|
||||
private async _fetchAndCreatePollingAsync(makerAssetData: string, takerAssetData: string): Promise<void> {
|
||||
@@ -59,21 +60,28 @@ export class SRAPollingOrderProvider extends BaseSRAOrderProvider {
|
||||
// Set the OrderSet for the polling to diff against
|
||||
this._updateStore({ added: orders, removed: [], assetPairKey });
|
||||
// Create a HTTP polling subscription
|
||||
const pollingIntervalId = this._createPollingSubscription(makerAssetData, takerAssetData);
|
||||
this._httpPollingIntervalId.set(assetPairKey, pollingIntervalId);
|
||||
const pollingIntervalId = (this._createPollingSubscription(makerAssetData, takerAssetData) as any) as number;
|
||||
this._assetPairKeyToPollingIntervalId.set(assetPairKey, pollingIntervalId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the polling interval fetching the orders, calculating the diff and updating the store
|
||||
*/
|
||||
private _createPollingSubscription(makerAssetData: string, takerAssetData: string): any {
|
||||
private _createPollingSubscription(makerAssetData: string, takerAssetData: string): NodeJS.Timer {
|
||||
const assetPairKey = OrderStore.getKeyForAssetPair(makerAssetData, takerAssetData);
|
||||
const pollingIntervalId = setInterval(async () => {
|
||||
const previousOrderSet = this._orderStore.getOrderSetForAssetPair(assetPairKey);
|
||||
const orders = await this._fetchLatestOrdersAsync(makerAssetData, takerAssetData);
|
||||
const diff = previousOrderSet.diff(new OrderSet(orders));
|
||||
this._updateStore({ ...diff, assetPairKey });
|
||||
}, this._pollingIntervalMs);
|
||||
const pollingIntervalId = intervalUtils.setAsyncExcludingInterval(
|
||||
async () => {
|
||||
const previousOrderSet = this._orderStore.getOrderSetForAssetPair(assetPairKey);
|
||||
const orders = await this._fetchLatestOrdersAsync(makerAssetData, takerAssetData);
|
||||
const diff = previousOrderSet.diff(new OrderSet(orders));
|
||||
this._updateStore({ ...diff, assetPairKey });
|
||||
},
|
||||
this._pollingIntervalMs,
|
||||
(_: Error) => {
|
||||
// TODO(dave4506) Add richer errors
|
||||
throw new Error(`Fetching latest orders for asset pair ${makerAssetData}/${takerAssetData}`);
|
||||
},
|
||||
);
|
||||
return pollingIntervalId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import { utils } from '../utils';
|
||||
|
||||
import { BaseSRAOrderProvider } from './base_sra_order_provider';
|
||||
|
||||
const RECORD_COUNT = 100;
|
||||
const PER_PAGE_DEFAULT = 100;
|
||||
|
||||
export class SRAWebsocketOrderProvider extends BaseSRAOrderProvider {
|
||||
private readonly _websocketEndpoint: string;
|
||||
@@ -30,7 +30,7 @@ export class SRAWebsocketOrderProvider extends BaseSRAOrderProvider {
|
||||
* @param orderStore The `OrderStore` where orders are added and removed from
|
||||
*/
|
||||
constructor(opts: SRAWebsocketOrderProviderOpts, orderStore: OrderStore) {
|
||||
super(orderStore, opts.httpEndpoint, RECORD_COUNT, opts.networkId);
|
||||
super(orderStore, opts.httpEndpoint, PER_PAGE_DEFAULT, opts.networkId);
|
||||
assert.isUri('websocketEndpoint', opts.websocketEndpoint);
|
||||
this._websocketEndpoint = opts.websocketEndpoint;
|
||||
}
|
||||
@@ -83,23 +83,23 @@ export class SRAWebsocketOrderProvider extends BaseSRAOrderProvider {
|
||||
}
|
||||
}
|
||||
const assetPairKey = OrderStore.getKeyForAssetPair(makerAssetData, takerAssetData);
|
||||
const susbcriptionOpts = {
|
||||
const subcriptionOpts = {
|
||||
baseAssetData: makerAssetData,
|
||||
quoteAssetData: takerAssetData,
|
||||
limit: RECORD_COUNT,
|
||||
limit: PER_PAGE_DEFAULT,
|
||||
};
|
||||
this._wsSubscriptions.set(assetPairKey, susbcriptionOpts);
|
||||
this._wsSubscriptions.set(assetPairKey, subcriptionOpts);
|
||||
// Subscribe to both sides of the book
|
||||
this._ordersChannel.subscribe(susbcriptionOpts);
|
||||
this._ordersChannel.subscribe(subcriptionOpts);
|
||||
this._ordersChannel.subscribe({
|
||||
...susbcriptionOpts,
|
||||
...subcriptionOpts,
|
||||
baseAssetData: takerAssetData,
|
||||
quoteAssetData: makerAssetData,
|
||||
});
|
||||
}
|
||||
|
||||
private async _fetchAndCreateSubscriptionAsync(makerAssetData: string, takerAssetData: string): Promise<void> {
|
||||
// Create the subscription first to get any updates during waiting for the request
|
||||
// Create the subscription first to get any updates while waiting for the request
|
||||
await this._createWebsocketSubscriptionAsync(makerAssetData, takerAssetData);
|
||||
// first time we have had this request, preload the local storage
|
||||
const orders = await this._fetchLatestOrdersAsync(makerAssetData, takerAssetData);
|
||||
@@ -148,7 +148,7 @@ export class SRAWebsocketOrderProvider extends BaseSRAOrderProvider {
|
||||
ordersChannelHandler,
|
||||
);
|
||||
} catch (e) {
|
||||
// Provide a more informative error
|
||||
// TODO(dave4506) Provide a more informative error
|
||||
throw new Error(`Creating websocket connection to ${this._websocketEndpoint}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ import { APIOrder } from '@0x/connect';
|
||||
import { AssetPairsItem, SignedOrder } from '@0x/types';
|
||||
|
||||
import { BaseOrderProvider } from './order_provider/base_order_provider';
|
||||
import { CustomOrderProvider } from './order_provider/custom_order_provider';
|
||||
import { MeshOrderProvider } from './order_provider/mesh_order_provider';
|
||||
import { ProvidedOrdersOrderProvider } from './order_provider/provider_orders_order_provider';
|
||||
import { SRAPollingOrderProvider } from './order_provider/sra_polling_order_provider';
|
||||
import { SRAWebsocketOrderProvider } from './order_provider/sra_websocket_order_provider';
|
||||
import { OrderStore } from './order_store';
|
||||
@@ -25,7 +25,7 @@ export class Orderbook {
|
||||
*/
|
||||
public static getOrderbookForProvidedOrders(orders: SignedOrder[]): Orderbook {
|
||||
const orderStore = new OrderStore();
|
||||
return new Orderbook(new ProvidedOrdersOrderProvider(orders, orderStore), orderStore);
|
||||
return new Orderbook(new CustomOrderProvider(orders, orderStore), orderStore);
|
||||
}
|
||||
/**
|
||||
* Creates an Orderbook with the SRA Websocket Provider. This Provider fetches orders via
|
||||
|
||||
Reference in New Issue
Block a user