2020-10-03 21:57:14 -03:00
|
|
|
import trio # type: ignore
|
2020-10-06 00:39:54 -03:00
|
|
|
from typing import List
|
2020-09-27 23:12:55 -03:00
|
|
|
|
2020-10-06 01:50:55 -03:00
|
|
|
from lnbits.tasks import register_invoice_listener
|
2020-09-27 23:12:55 -03:00
|
|
|
|
2020-10-06 00:39:54 -03:00
|
|
|
sse_listeners: List[trio.MemorySendChannel] = []
|
2020-09-27 23:12:55 -03:00
|
|
|
|
|
|
|
|
2020-10-06 01:50:55 -03:00
|
|
|
async def register_listeners():
|
|
|
|
invoice_paid_chan_send, invoice_paid_chan_recv = trio.open_memory_channel(5)
|
|
|
|
register_invoice_listener(invoice_paid_chan_send)
|
|
|
|
await wait_for_paid_invoices(invoice_paid_chan_recv)
|
|
|
|
|
|
|
|
|
|
|
|
async def wait_for_paid_invoices(invoice_paid_chan: trio.MemoryReceiveChannel):
|
|
|
|
async for payment in invoice_paid_chan:
|
|
|
|
for send_channel in sse_listeners:
|
|
|
|
try:
|
|
|
|
send_channel.send_nowait(payment)
|
|
|
|
except trio.WouldBlock:
|
|
|
|
print("removing sse listener", send_channel)
|
|
|
|
sse_listeners.remove(send_channel)
|