diff --git a/bfxapi/websocket/client/bfx_websocket_client.py b/bfxapi/websocket/client/bfx_websocket_client.py index 60445b3..7fb2661 100644 --- a/bfxapi/websocket/client/bfx_websocket_client.py +++ b/bfxapi/websocket/client/bfx_websocket_client.py @@ -55,8 +55,10 @@ class BfxWebsocketClient: MAXIMUM_CONNECTIONS_AMOUNT = 20 + ONCE_EVENTS = [ "open", "authenticated", "disconnection" ] + EVENTS = [ - "open", "subscribed", "authenticated", "disconnection", "wss-error", + *ONCE_EVENTS, "subscribed", "wss-error", *PublicChannelsHandler.EVENTS, *AuthenticatedChannelsHandler.EVENTS ] @@ -110,9 +112,6 @@ class BfxWebsocketClient: def _on_wss_timeout(): on_timeout_event.set() - raise ReconnectionTimeoutError("Connection has been offline for too long " \ - f"without being able to reconnect (wss_timeout: {self.wss_timeout}s).") - async def _connection(): nonlocal reconnection, timer, tasks @@ -173,7 +172,8 @@ class BfxWebsocketClient: await asyncio.sleep(delay.next()) if on_timeout_event.is_set(): - break + raise ReconnectionTimeoutError("Connection has been offline for too long " \ + f"without being able to reconnect (wss_timeout: {self.wss_timeout}s).") try: await _connection() @@ -259,30 +259,18 @@ class BfxWebsocketClient: raise EventNotSupported(f"Event <{event}> is not supported. To get a list " \ "of available events print BfxWebsocketClient.EVENTS") + def _register_event(event, function): + if event in BfxWebsocketClient.ONCE_EVENTS: + self.event_emitter.once(event, function) + else: self.event_emitter.on(event, function) + if callback is not None: for event in events: - self.event_emitter.on(event, callback) + _register_event(event, callback) if callback is None: def handler(function): for event in events: - self.event_emitter.on(event, function) - - return handler - - def once(self, *events, callback = None): - for event in events: - if event not in BfxWebsocketClient.EVENTS: - raise EventNotSupported(f"Event <{event}> is not supported. To get a list " \ - "of available events print BfxWebsocketClient.EVENTS") - - if callback is not None: - for event in events: - self.event_emitter.once(event, callback) - - if callback is None: - def handler(function): - for event in events: - self.event_emitter.once(event, function) + _register_event(event, function) return handler diff --git a/examples/websocket/public/derivatives_status.py b/examples/websocket/public/derivatives_status.py index 3609cff..9212909 100644 --- a/examples/websocket/public/derivatives_status.py +++ b/examples/websocket/public/derivatives_status.py @@ -16,7 +16,7 @@ def on_derivatives_status_update(subscription: subscriptions.Status, data: Deriv def on_wss_error(code: Error, msg: str): print(code, msg) -@bfx.wss.once("open") +@bfx.wss.on("open") async def on_open(): await bfx.wss.subscribe(Channel.STATUS, key="deriv:tBTCF0:USTF0") diff --git a/examples/websocket/public/ticker.py b/examples/websocket/public/ticker.py index c446cd0..8a55aa4 100644 --- a/examples/websocket/public/ticker.py +++ b/examples/websocket/public/ticker.py @@ -14,7 +14,7 @@ def on_t_ticker_update(subscription: subscriptions.Ticker, data: TradingPairTick print(f"Data: {data}") -@bfx.wss.once("open") +@bfx.wss.on("open") async def on_open(): await bfx.wss.subscribe(Channel.TICKER, symbol="tBTCUSD") diff --git a/examples/websocket/public/trades.py b/examples/websocket/public/trades.py index 9edd20e..0495ba0 100644 --- a/examples/websocket/public/trades.py +++ b/examples/websocket/public/trades.py @@ -20,7 +20,7 @@ def on_t_trade_executed(_sub: subscriptions.Trades, trade: TradingPairTrade): def on_wss_error(code: Error, msg: str): print(code, msg) -@bfx.wss.once("open") +@bfx.wss.on("open") async def on_open(): await bfx.wss.subscribe(Channel.CANDLES, key="trade:1m:tBTCUSD")