lnbits / nostrclient

MIT License
23 stars 10 forks source link

Review WS changes #3

Closed motorina0 closed 1 year ago

motorina0 commented 1 year ago

Post-merge review. See comments to code also.

Is the code inside/nostr still needed?

Refactor suggestions:

class NostrRouter:

    def __init__(self, websocket):
        self.subscriptions: List[str] = []
        self.connected: bool = True

        self.websocket = websocket

        self.tasks: List[asyncio.Task] = []

    async def client_to_nostr(self):
        ...

    async def nostr_to_client(self):
        ...

    async def start(self):
        self.tasks.append(asyncio.create_task(self.client_to_nostr()))
        self.tasks.append(asyncio.create_task(self.nostr_to_client()))

    async def stop(self):
        for t in self.tasks:
            t.cancel()
@nostrclient_ext.websocket("/api/v1/relay")
async def ws_relay(websocket: WebSocket):
    """Relay multiplexer: one client (per endpoint) <-> multiple relays"""
    await websocket.accept()

    router = NostrRouter(websocket)
    router.start()

    while True:
        await asyncio.sleep(10)
        if not router.connected:
            for s in router.subscriptions:
                client.relay_manager.close_subscription(s)
            router.stop()
            break