cleaning around websocket client
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import WebSocket from 'isomorphic-ws';
|
||||
import { WsKey } from '../types';
|
||||
|
||||
import { DefaultLogger } from './logger';
|
||||
|
||||
@@ -44,9 +45,9 @@ export default class WsStore {
|
||||
}
|
||||
|
||||
/** Get WS stored state for key, optionally create if missing */
|
||||
get(key: string, createIfMissing?: true): WsStoredState;
|
||||
get(key: string, createIfMissing?: false): WsStoredState | undefined;
|
||||
get(key: string, createIfMissing?: boolean): WsStoredState | undefined {
|
||||
get(key: WsKey, createIfMissing?: true): WsStoredState;
|
||||
get(key: WsKey, createIfMissing?: false): WsStoredState | undefined;
|
||||
get(key: WsKey, createIfMissing?: boolean): WsStoredState | undefined {
|
||||
if (this.wsState[key]) {
|
||||
return this.wsState[key];
|
||||
}
|
||||
@@ -56,11 +57,11 @@ export default class WsStore {
|
||||
}
|
||||
}
|
||||
|
||||
getKeys(): string[] {
|
||||
return Object.keys(this.wsState);
|
||||
getKeys(): WsKey[] {
|
||||
return Object.keys(this.wsState) as WsKey[];
|
||||
}
|
||||
|
||||
create(key: string): WsStoredState | undefined {
|
||||
create(key: WsKey): WsStoredState | undefined {
|
||||
if (this.hasExistingActiveConnection(key)) {
|
||||
this.logger.warning(
|
||||
'WsStore setConnection() overwriting existing open connection: ',
|
||||
@@ -74,7 +75,7 @@ export default class WsStore {
|
||||
return this.get(key);
|
||||
}
|
||||
|
||||
delete(key: string) {
|
||||
delete(key: WsKey) {
|
||||
if (this.hasExistingActiveConnection(key)) {
|
||||
const ws = this.getWs(key);
|
||||
this.logger.warning(
|
||||
@@ -88,15 +89,15 @@ export default class WsStore {
|
||||
|
||||
/* connection websocket */
|
||||
|
||||
hasExistingActiveConnection(key: string) {
|
||||
hasExistingActiveConnection(key: WsKey) {
|
||||
return this.get(key) && this.isWsOpen(key);
|
||||
}
|
||||
|
||||
getWs(key: string): WebSocket | undefined {
|
||||
getWs(key: WsKey): WebSocket | undefined {
|
||||
return this.get(key)?.ws;
|
||||
}
|
||||
|
||||
setWs(key: string, wsConnection: WebSocket): WebSocket {
|
||||
setWs(key: WsKey, wsConnection: WebSocket): WebSocket {
|
||||
if (this.isWsOpen(key)) {
|
||||
this.logger.warning(
|
||||
'WsStore setConnection() overwriting existing open connection: ',
|
||||
@@ -109,7 +110,7 @@ export default class WsStore {
|
||||
|
||||
/* connection state */
|
||||
|
||||
isWsOpen(key: string): boolean {
|
||||
isWsOpen(key: WsKey): boolean {
|
||||
const existingConnection = this.getWs(key);
|
||||
return (
|
||||
!!existingConnection &&
|
||||
@@ -117,37 +118,37 @@ export default class WsStore {
|
||||
);
|
||||
}
|
||||
|
||||
getConnectionState(key: string): WsConnectionStateEnum {
|
||||
getConnectionState(key: WsKey): WsConnectionStateEnum {
|
||||
return this.get(key, true)!.connectionState!;
|
||||
}
|
||||
|
||||
setConnectionState(key: string, state: WsConnectionStateEnum) {
|
||||
setConnectionState(key: WsKey, state: WsConnectionStateEnum) {
|
||||
this.get(key, true)!.connectionState = state;
|
||||
}
|
||||
|
||||
isConnectionState(key: string, state: WsConnectionStateEnum): boolean {
|
||||
isConnectionState(key: WsKey, state: WsConnectionStateEnum): boolean {
|
||||
return this.getConnectionState(key) === state;
|
||||
}
|
||||
|
||||
/* subscribed topics */
|
||||
|
||||
getTopics(key: string): WsTopicList {
|
||||
getTopics(key: WsKey): WsTopicList {
|
||||
return this.get(key, true).subscribedTopics;
|
||||
}
|
||||
|
||||
getTopicsByKey(): Record<string, WsTopicList> {
|
||||
const result = {};
|
||||
for (const refKey in this.wsState) {
|
||||
result[refKey] = this.getTopics(refKey);
|
||||
result[refKey] = this.getTopics(refKey as WsKey);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
addTopic(key: string, topic: WsTopic) {
|
||||
addTopic(key: WsKey, topic: WsTopic) {
|
||||
return this.getTopics(key).add(topic);
|
||||
}
|
||||
|
||||
deleteTopic(key: string, topic: WsTopic) {
|
||||
deleteTopic(key: WsKey, topic: WsTopic) {
|
||||
return this.getTopics(key).delete(topic);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,84 @@
|
||||
import { WsKey } from '../types';
|
||||
|
||||
export const wsKeyInverse = 'inverse';
|
||||
export const wsKeyLinearPrivate = 'linearPrivate';
|
||||
export const wsKeyLinearPublic = 'linearPublic';
|
||||
export const wsKeySpotPrivate = 'spotPrivate';
|
||||
export const wsKeySpotPublic = 'spotPublic';
|
||||
interface NetworkMapV3 {
|
||||
livenet: string;
|
||||
livenet2?: string;
|
||||
testnet: string;
|
||||
testnet2?: string;
|
||||
}
|
||||
|
||||
export const WS_KEY_MAP = {
|
||||
inverse: wsKeyInverse,
|
||||
linearPrivate: wsKeyLinearPrivate,
|
||||
linearPublic: wsKeyLinearPublic,
|
||||
spotPrivate: wsKeySpotPrivate,
|
||||
spotPublic: wsKeySpotPublic,
|
||||
type PublicPrivateNetwork = 'public' | 'private';
|
||||
|
||||
export const WS_BASE_URL_MAP: Record<
|
||||
string,
|
||||
Record<PublicPrivateNetwork, NetworkMapV3>
|
||||
> = {
|
||||
inverse: {
|
||||
private: {
|
||||
livenet: 'wss://stream.bybit.com/realtime',
|
||||
testnet: 'wss://stream-testnet.bybit.com/realtime',
|
||||
},
|
||||
public: {
|
||||
livenet: 'wss://stream.bybit.com/realtime',
|
||||
testnet: 'wss://stream-testnet.bybit.com/realtime',
|
||||
},
|
||||
},
|
||||
linear: {
|
||||
private: {
|
||||
livenet: 'wss://stream.bybit.com/realtime_private',
|
||||
livenet2: 'wss://stream.bytick.com/realtime_private',
|
||||
testnet: 'wss://stream-testnet.bybit.com/realtime_private',
|
||||
},
|
||||
public: {
|
||||
livenet: 'wss://stream.bybit.com/realtime_public',
|
||||
livenet2: 'wss://stream.bytick.com/realtime_public',
|
||||
testnet: 'wss://stream-testnet.bybit.com/realtime_public',
|
||||
},
|
||||
},
|
||||
spot: {
|
||||
private: {
|
||||
livenet: 'wss://stream.bybit.com/spot/ws',
|
||||
testnet: 'wss://stream-testnet.bybit.com/spot/ws',
|
||||
},
|
||||
public: {
|
||||
livenet: 'wss://stream.bybit.com/spot/quote/ws/v1',
|
||||
livenet2: 'wss://stream.bybit.com/spot/quote/ws/v2',
|
||||
testnet: 'wss://stream-testnet.bybit.com/spot/quote/ws/v1',
|
||||
testnet2: 'wss://stream-testnet.bybit.com/spot/quote/ws/v2',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const PUBLIC_WS_KEYS = [WS_KEY_MAP.linearPublic, WS_KEY_MAP.spotPublic];
|
||||
export const WS_KEY_MAP = {
|
||||
inverse: 'inverse',
|
||||
linearPrivate: 'linearPrivate',
|
||||
linearPublic: 'linearPublic',
|
||||
spotPrivate: 'spotPrivate',
|
||||
spotPublic: 'spotPublic',
|
||||
} as const;
|
||||
|
||||
export const PUBLIC_WS_KEYS = [
|
||||
WS_KEY_MAP.linearPublic,
|
||||
WS_KEY_MAP.spotPublic,
|
||||
] as string[];
|
||||
|
||||
export function getLinearWsKeyForTopic(topic: string): WsKey {
|
||||
const privateLinearTopics = [
|
||||
const privateTopics = [
|
||||
'position',
|
||||
'execution',
|
||||
'order',
|
||||
'stop_order',
|
||||
'wallet',
|
||||
];
|
||||
if (privateLinearTopics.includes(topic)) {
|
||||
return wsKeyLinearPrivate;
|
||||
if (privateTopics.includes(topic)) {
|
||||
return WS_KEY_MAP.linearPrivate;
|
||||
}
|
||||
|
||||
return wsKeyLinearPublic;
|
||||
return WS_KEY_MAP.linearPublic;
|
||||
}
|
||||
|
||||
export function getSpotWsKeyForTopic(topic: string): WsKey {
|
||||
const privateLinearTopics = [
|
||||
const privateTopics = [
|
||||
'position',
|
||||
'execution',
|
||||
'order',
|
||||
@@ -42,9 +88,8 @@ export function getSpotWsKeyForTopic(topic: string): WsKey {
|
||||
'ticketInfo',
|
||||
];
|
||||
|
||||
if (privateLinearTopics.includes(topic)) {
|
||||
return wsKeySpotPrivate;
|
||||
if (privateTopics.includes(topic)) {
|
||||
return WS_KEY_MAP.spotPrivate;
|
||||
}
|
||||
|
||||
return wsKeySpotPublic;
|
||||
return WS_KEY_MAP.spotPublic;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user