Added errorCallback

This commit is contained in:
Aleksandr Kraiz
2023-03-07 20:17:23 +04:00
parent 56cd1356f0
commit b21eaaf163
6 changed files with 65 additions and 35 deletions

View File

@@ -5,33 +5,7 @@ import priceFeedSubscriptions from './priceFeedSubscriptions';
import { tickerInfoSchema, candleSchema } from './schemas';
import priceSchema from './schemas/priceSchema';
import type { AnyJSON } from '../../../types';
type TickerInfo = {
pairName: string
lastPrice: string
openPrice: string
highPrice: string
lowPrice: string
volume24h: string
}
const allTickersSchema = z.unknown().array()
.transform((tickers) => {
const data = [...tickers];
data.shift();
const parsedData = tickerInfoSchema.array().parse(data);
return parsedData.reduce<
Partial<
Record<
string,
TickerInfo
>
>
>((prev, pairData) => ({
...prev,
[pairData.pairName]: pairData,
}), {});
});
import allTickersSchema from './schemas/allTickersSchema';
export const subscriptions = {
[priceFeedSubscriptions.ALL_TICKERS]: {
@@ -59,9 +33,11 @@ export type Subscription<
> = typeof subscriptions[T] extends { payload: true }
? {
callback: (data: Schema) => void
errorCallback?: (error: Error) => void
payload: string
} : {
callback: (data: Schema) => void
errorCallback?: (error: Error) => void
}
export default class PriceFeedSubscription<T extends SubscriptionType = SubscriptionType> {
@@ -69,6 +45,8 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
private readonly callback: Subscription<T>['callback'];
private readonly errorCallback?: Subscription<T>['errorCallback'];
private readonly payload?: string;
private ws?: WebSocket;
@@ -100,6 +78,7 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
this.payload = params.payload;
}
this.callback = params.callback;
this.errorCallback = params.errorCallback;
this.onOpen = onOpen;
this.init();
}
@@ -146,9 +125,13 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
const parseResult = subscription.schema.safeParse(json);
if (!parseResult.success) {
const errorsMessage = parseResult.error.errors.map((error) => `[${error.path.join('.')}] ${error.message}`).join(', ');
throw new Error(`Can't recognize PriceFeed "${type}" subscription message "${dataString}": ${errorsMessage}`);
const error = new Error(`Can't recognize PriceFeed "${type}" subscription message "${dataString}": ${errorsMessage}`);
if (this.errorCallback !== undefined) {
this.errorCallback(error);
} else throw error;
} else {
this.callback(parseResult.data);
}
this.callback(parseResult.data);
};
this.ws.onclose = () => {

View File

@@ -0,0 +1,24 @@
import { z } from 'zod';
import tickerInfoSchema from './tickerInfoSchema';
type TickerInfo = z.infer<typeof tickerInfoSchema>
const allTickersSchema = z.unknown().array()
.transform((tickers) => {
const data = [...tickers];
data.shift();
const parsedData = tickerInfoSchema.array().parse(data);
return parsedData.reduce<
Partial<
Record<
string,
TickerInfo
>
>
>((prev, pairData) => ({
...prev,
[pairData.pairName]: pairData,
}), {});
});
export default allTickersSchema;

View File

@@ -1,2 +1,4 @@
export { default as tickerInfoSchema } from './tickerInfoSchema';
export { default as candleSchema } from './candleSchema';
export { default as priceSchema } from './priceSchema';
export { default as allTickersSchema } from './allTickersSchema';