ARTICLE AD BOX
def get_contract_listener(access_token: str, callback: Callable[[int | None], Awaitable[None]]) -> None | Watch:
logger.debug('get contract listener')
contract = db.collection('Contract').where(filter=FieldFilter('access_token', '==', access_token)).get()
async def wrap(document: DocumentSnapshot):
await callback(123)
return cast(DocumentReference, contract[0].reference).on_snapshot(lambda doc, _, __: run(wrap(doc[0])))
async def api_websocket_wait_update(socket: WebSocket):
await socket.accept()
global first
first = True
token = socket.cookies.get('access_token')
async def callback(balance: int | None) -> None:
assert listener is not None
global first
if first:
first = False
return
await socket.send_json({'message': 'updated', 'balance': balance})
await socket.close(1000)
listener.unsubscribe()
# return ??
listener = get_contract_listener(token, callback)
await socket.send_json({'message': 'accepted'})
while True:
sleep(5)
How to break function api_websocket_wait_update from callback? If I just return, i will break only callback.
Similar question for JS (but they were ran function from original, but in this situation I am running it from listener callback)
p.s some code checks removed (like is contract exists or is access token provided), because stackoverflow says "your question is mostly code"
Explore related questions
See similar questions with these tags.
