register channel listeners instead of callbacks.
makes for a little less black magic and more reasonable use of nurseries and less unnecessary pseudo-requests.
This commit is contained in:
@@ -8,8 +8,8 @@ core_app: Blueprint = Blueprint(
|
||||
|
||||
from .views.api import * # noqa
|
||||
from .views.generic import * # noqa
|
||||
from .tasks import on_invoice_paid
|
||||
from .tasks import register_listeners
|
||||
|
||||
from lnbits.tasks import register_invoice_listener
|
||||
from lnbits.tasks import record_async
|
||||
|
||||
register_invoice_listener("core", on_invoice_paid)
|
||||
core_app.record(record_async(register_listeners))
|
||||
|
||||
@@ -70,6 +70,7 @@ class Payment(NamedTuple):
|
||||
preimage: str
|
||||
payment_hash: str
|
||||
extra: Dict
|
||||
wallet_id: str
|
||||
|
||||
@classmethod
|
||||
def from_row(cls, row: Row):
|
||||
@@ -84,6 +85,7 @@ class Payment(NamedTuple):
|
||||
fee=row["fee"],
|
||||
memo=row["memo"],
|
||||
time=row["time"],
|
||||
wallet_id=row["wallet"],
|
||||
)
|
||||
|
||||
@property
|
||||
|
||||
+15
-8
@@ -1,15 +1,22 @@
|
||||
import trio # type: ignore
|
||||
from typing import List
|
||||
|
||||
from .models import Payment
|
||||
from lnbits.tasks import register_invoice_listener
|
||||
|
||||
sse_listeners: List[trio.MemorySendChannel] = []
|
||||
|
||||
|
||||
async def on_invoice_paid(payment: Payment):
|
||||
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)
|
||||
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)
|
||||
|
||||
@@ -128,6 +128,7 @@ async def api_payment(payment_hash):
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_payments_sse():
|
||||
g.db.close()
|
||||
this_wallet_id = g.wallet.id
|
||||
|
||||
send_payment, receive_payment = trio.open_memory_channel(0)
|
||||
|
||||
@@ -138,7 +139,8 @@ async def api_payments_sse():
|
||||
|
||||
async def payment_received() -> None:
|
||||
async for payment in receive_payment:
|
||||
await send_event.send(("payment", payment))
|
||||
if payment.wallet_id == this_wallet_id:
|
||||
await send_event.send(("payment", payment))
|
||||
|
||||
async def repeat_keepalive():
|
||||
await trio.sleep(1)
|
||||
@@ -160,7 +162,6 @@ async def api_payments_sse():
|
||||
|
||||
yield b"\n".join(message) + b"\r\n\r\n"
|
||||
except trio.Cancelled:
|
||||
print("canceled!")
|
||||
return
|
||||
|
||||
response = await make_response(
|
||||
|
||||
Reference in New Issue
Block a user