Skip to content

Commit

Permalink
feat(ws): connect after markets and tickers
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4x committed Mar 31, 2023
1 parent 6732ca7 commit 4aae18c
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 37 deletions.
4 changes: 4 additions & 0 deletions src/exchanges/binance/binance.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export class Binance extends BaseExchange {
this.store.tickers = tickers;
this.store.loaded.tickers = true;

// start websocket streams
this.publicWebsocket.connectAndSubscribe();
this.privateWebsocket.connectAndSubscribe();

// fetch current position mode (Hedge/One-way)
this.store.options.isHedged = await this.fetchPositionMode();

Expand Down
5 changes: 0 additions & 5 deletions src/exchanges/binance/binance.ws-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import {
} from './binance.types';

export class BinancePrivateWebsocket extends BaseWebSocket<Binance> {
constructor(parent: Binance) {
super(parent);
this.connectAndSubscribe();
}

connectAndSubscribe = async () => {
const listenKey = await this.fetchListenKey();

Expand Down
5 changes: 0 additions & 5 deletions src/exchanges/binance/binance.ws-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import type { Binance } from './binance.exchange';
import { BASE_WS_URL } from './binance.types';

export class BinancePublicWebsocket extends BaseWebSocket<Binance> {
constructor(parent: Binance) {
super(parent);
this.connectAndSubscribe();
}

connectAndSubscribe = () => {
this.ws = new WebSocket(
BASE_WS_URL.public[this.parent.options.testnet ? 'testnet' : 'livenet']
Expand Down
4 changes: 4 additions & 0 deletions src/exchanges/bybit/bybit.exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export class Bybit extends BaseExchange {
this.store.tickers = tickers;
this.store.loaded.tickers = true;

// start websocket streams
this.publicWebsocket.connectAndSubscribe();
this.privateWebsocket.connectAndSubscribe();

// start ticking live data
// balance, tickers, positions
await this.tick();
Expand Down
5 changes: 0 additions & 5 deletions src/exchanges/bybit/bybit.ws-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import type { Bybit } from './bybit.exchange';
import { BASE_WS_URL } from './bybit.types';

export class BybitPrivateWebsocket extends BaseWebSocket<Bybit> {
constructor(parent: Bybit) {
super(parent);
this.connectAndSubscribe();
}

connectAndSubscribe = () => {
this.ws = new WebSocket(
BASE_WS_URL.private[this.parent.options.testnet ? 'testnet' : 'livenet']
Expand Down
22 changes: 0 additions & 22 deletions src/exchanges/bybit/bybit.ws-public.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import { subscribe } from 'valtio/vanilla';

import { BaseWebSocket } from '../base.ws';

import type { Bybit } from './bybit.exchange';
import { BASE_WS_URL } from './bybit.types';

export class BybitPublicWebsocket extends BaseWebSocket<Bybit> {
constructor(parent: Bybit) {
super(parent);

// we use this little trick to make sure we connect and subscribe
// after loaded the markets and tickers first with xhr API
const unsubscribe = subscribe(this.parent.store.loaded, () => {
if (
this.parent.store.loaded.markets &&
this.parent.store.loaded.tickers
) {
unsubscribe();
this.connectAndSubscribe();
}
});

if (this.parent.isDisposed) {
unsubscribe();
}
}

connectAndSubscribe = () => {
if (!this.parent.isDisposed) {
this.ws = new WebSocket(
Expand Down

0 comments on commit 4aae18c

Please sign in to comment.