v2.4.0-beta.1. remove circleci, cleaning in ws client
This commit is contained in:
@@ -1,16 +1,35 @@
|
||||
import WebSocket from 'isomorphic-ws';
|
||||
|
||||
import { WsConnectionState } from '../websocket-client';
|
||||
import { DefaultLogger } from './logger';
|
||||
|
||||
export enum WsConnectionStateEnum {
|
||||
INITIAL = 0,
|
||||
CONNECTING = 1,
|
||||
CONNECTED = 2,
|
||||
CLOSING = 3,
|
||||
RECONNECTING = 4,
|
||||
}
|
||||
/** A "topic" is always a string */
|
||||
type WsTopic = string;
|
||||
|
||||
/**
|
||||
* A "Set" is used to ensure we only subscribe to a topic once (tracking a list of unique topics we're expected to be connected to)
|
||||
* TODO: do any WS topics allow parameters? If so, we need a way to track those (see FTX implementation)
|
||||
*/
|
||||
type WsTopicList = Set<WsTopic>;
|
||||
|
||||
interface WsStoredState {
|
||||
/** The currently active websocket connection */
|
||||
ws?: WebSocket;
|
||||
connectionState?: WsConnectionState;
|
||||
/** The current lifecycle state of the connection (enum) */
|
||||
connectionState?: WsConnectionStateEnum;
|
||||
/** A timer that will send an upstream heartbeat (ping) when it expires */
|
||||
activePingTimer?: ReturnType<typeof setTimeout> | undefined;
|
||||
/** A timer tracking that an upstream heartbeat was sent, expecting a reply before it expires */
|
||||
activePongTimer?: ReturnType<typeof setTimeout> | undefined;
|
||||
/**
|
||||
* All the topics we are expected to be subscribed to (and we automatically resubscribe to if the connection drops)
|
||||
*/
|
||||
subscribedTopics: WsTopicList;
|
||||
}
|
||||
|
||||
@@ -23,6 +42,9 @@ export default class WsStore {
|
||||
this.wsState = {};
|
||||
}
|
||||
|
||||
/** 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 {
|
||||
if (this.wsState[key]) {
|
||||
return this.wsState[key];
|
||||
@@ -46,7 +68,7 @@ export default class WsStore {
|
||||
}
|
||||
this.wsState[key] = {
|
||||
subscribedTopics: new Set(),
|
||||
connectionState: WsConnectionState.READY_STATE_INITIAL,
|
||||
connectionState: WsConnectionStateEnum.INITIAL,
|
||||
};
|
||||
return this.get(key);
|
||||
}
|
||||
@@ -94,22 +116,22 @@ export default class WsStore {
|
||||
);
|
||||
}
|
||||
|
||||
getConnectionState(key: string): WsConnectionState {
|
||||
getConnectionState(key: string): WsConnectionStateEnum {
|
||||
return this.get(key, true)!.connectionState!;
|
||||
}
|
||||
|
||||
setConnectionState(key: string, state: WsConnectionState) {
|
||||
setConnectionState(key: string, state: WsConnectionStateEnum) {
|
||||
this.get(key, true)!.connectionState = state;
|
||||
}
|
||||
|
||||
isConnectionState(key: string, state: WsConnectionState): boolean {
|
||||
isConnectionState(key: string, state: WsConnectionStateEnum): boolean {
|
||||
return this.getConnectionState(key) === state;
|
||||
}
|
||||
|
||||
/* subscribed topics */
|
||||
|
||||
getTopics(key: string): WsTopicList {
|
||||
return this.get(key, true)!.subscribedTopics;
|
||||
return this.get(key, true).subscribedTopics;
|
||||
}
|
||||
|
||||
getTopicsByKey(): Record<string, WsTopicList> {
|
||||
|
||||
@@ -2,3 +2,4 @@ export * from './BaseRestClient';
|
||||
export * from './requestUtils';
|
||||
export * from './WsStore';
|
||||
export * from './logger';
|
||||
export * from './websocket-util';
|
||||
|
||||
40
src/util/websocket-util.ts
Normal file
40
src/util/websocket-util.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { WsKey } from '../types';
|
||||
|
||||
export const wsKeyInverse = 'inverse';
|
||||
export const wsKeyLinearPrivate = 'linearPrivate';
|
||||
export const wsKeyLinearPublic = 'linearPublic';
|
||||
export const wsKeySpotPrivate = 'spotPrivate';
|
||||
export const wsKeySpotPublic = 'spotPublic';
|
||||
|
||||
export function getLinearWsKeyForTopic(topic: string): WsKey {
|
||||
const privateLinearTopics = [
|
||||
'position',
|
||||
'execution',
|
||||
'order',
|
||||
'stop_order',
|
||||
'wallet',
|
||||
];
|
||||
if (privateLinearTopics.includes(topic)) {
|
||||
return wsKeyLinearPrivate;
|
||||
}
|
||||
|
||||
return wsKeyLinearPublic;
|
||||
}
|
||||
|
||||
export function getSpotWsKeyForTopic(topic: string): WsKey {
|
||||
const privateLinearTopics = [
|
||||
'position',
|
||||
'execution',
|
||||
'order',
|
||||
'stop_order',
|
||||
'outboundAccountInfo',
|
||||
'executionReport',
|
||||
'ticketInfo',
|
||||
];
|
||||
|
||||
if (privateLinearTopics.includes(topic)) {
|
||||
return wsKeySpotPrivate;
|
||||
}
|
||||
|
||||
return wsKeySpotPublic;
|
||||
}
|
||||
Reference in New Issue
Block a user