-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: clean-up * refactor: extra logs plus try-catch * refactor: do not use bare `except` * refactor: clean-up redundant fields * chore: pass code checks * chore: code format * refactor: code clean-up * fix: refactoring stuff * refactor: remove un-used file * chore: code clean-up * chore: code clean-up * chore: code-format fix * refactor: remove nostr.client wrapper * refactor: code clean-up * chore: code format * refactor: remove `RelayList` class * refactor: extract smaller methods with try-catch * fix: better exception handling * fix: remove redundant filters * fix: simplify event * chore: code format * fix: code check * fix: code check * fix: simplify `REQ` * fix: more clean-ups * refactor: use simpler method * refactor: re-order and rename * fix: stop logic * fix: subscription close before disconnect * chore: play commit
- Loading branch information
Showing
20 changed files
with
525 additions
and
720 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,73 @@ | ||
import asyncio | ||
from typing import List | ||
|
||
from loguru import logger | ||
|
||
from ..relay_manager import RelayManager | ||
|
||
|
||
class NostrClient: | ||
relays = [ ] | ||
relay_manager = RelayManager() | ||
|
||
def __init__(self, relays: List[str] = [], connect=True): | ||
if len(relays): | ||
self.relays = relays | ||
if connect: | ||
self.connect() | ||
def __init__(self): | ||
self.running = True | ||
|
||
def connect(self, relays): | ||
for relay in relays: | ||
try: | ||
self.relay_manager.add_relay(relay) | ||
except Exception as e: | ||
logger.debug(e) | ||
self.running = True | ||
|
||
async def connect(self): | ||
for relay in self.relays: | ||
self.relay_manager.add_relay(relay) | ||
def reconnect(self, relays): | ||
self.relay_manager.remove_relays() | ||
self.connect(relays) | ||
|
||
def close(self): | ||
self.relay_manager.close_connections() | ||
try: | ||
self.relay_manager.close_all_subscriptions() | ||
self.relay_manager.close_connections() | ||
|
||
self.running = False | ||
except Exception as e: | ||
logger.error(e) | ||
|
||
async def subscribe( | ||
self, | ||
callback_events_func=None, | ||
callback_notices_func=None, | ||
callback_eosenotices_func=None, | ||
): | ||
while True: | ||
while self.running: | ||
self._check_events(callback_events_func) | ||
self._check_notices(callback_notices_func) | ||
self._check_eos_notices(callback_eosenotices_func) | ||
|
||
await asyncio.sleep(0.2) | ||
|
||
def _check_events(self, callback_events_func=None): | ||
try: | ||
while self.relay_manager.message_pool.has_events(): | ||
event_msg = self.relay_manager.message_pool.get_event() | ||
if callback_events_func: | ||
callback_events_func(event_msg) | ||
except Exception as e: | ||
logger.debug(e) | ||
|
||
def _check_notices(self, callback_notices_func=None): | ||
try: | ||
while self.relay_manager.message_pool.has_notices(): | ||
event_msg = self.relay_manager.message_pool.get_notice() | ||
if callback_notices_func: | ||
callback_notices_func(event_msg) | ||
except Exception as e: | ||
logger.debug(e) | ||
|
||
def _check_eos_notices(self, callback_eosenotices_func=None): | ||
try: | ||
while self.relay_manager.message_pool.has_eose_notices(): | ||
event_msg = self.relay_manager.message_pool.get_eose_notice() | ||
if callback_eosenotices_func: | ||
callback_eosenotices_func(event_msg) | ||
|
||
await asyncio.sleep(0.5) | ||
except Exception as e: | ||
logger.debug(e) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.