Skip to content

Commit 2d7131d

Browse files
committed
Implement wait_until_ready method.
1 parent 8a3b310 commit 2d7131d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

twitchio/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ def __init__(
151151
# Websockets for EventSub
152152
self._websockets: dict[str, dict[str, Websocket]] = defaultdict(dict)
153153

154+
self._ready_event: asyncio.Event = asyncio.Event()
155+
self._ready_event.clear()
156+
154157
self.__waiter: asyncio.Event = asyncio.Event()
155158

156159
@property
@@ -339,10 +342,12 @@ async def main() -> None:
339342

340343
# Dispatch ready event... May change places in the future.
341344
self.dispatch("ready")
345+
self._ready_event.set()
342346

343347
try:
344348
await self.__waiter.wait()
345349
finally:
350+
self._ready_event.clear()
346351
await self.close()
347352

348353
def run(self, token: str | None = None, *, with_adapter: bool = True) -> None:
@@ -430,6 +435,23 @@ async def close(self) -> None:
430435
self._http.cleanup()
431436
self.__waiter.set()
432437

438+
async def wait_until_ready(self) -> None:
439+
"""|coro|
440+
441+
Method which suspends the current coroutine and waits for "event_ready" to be dispatched.
442+
443+
If "event_ready" has previously been dispatched, this method returns immediately.
444+
445+
"event_ready" is dispatched after the HTTP Client has successfully logged in, tokens have sucessfully been loaded,
446+
and :meth:`.setup_hook` has completed execution.
447+
448+
.. warning::
449+
450+
Since this method directly relies on :meth:`.setup_hook` completing, using it in :meth:`.setup_hook` or in any
451+
call :meth:`.setup_hook` is waiting for execution to complete, will completely deadlock the Client.
452+
"""
453+
await self._ready_event.wait()
454+
433455
async def wait_for(self, event: str, *, timeout: float | None = None, predicate: WaitPredicateT | None = None) -> Any:
434456
"""Method which waits for any known dispatched event and returns the payload associated with the event.
435457

0 commit comments

Comments
 (0)