def __init__( self, sync_base: "SyncBase", event: str, predicate: Callable[[T], bool] = None, timeout: int = None, ) -> None: self._value: Optional[T] = None self._exception = None self._loop = sync_base._loop wait_helper = WaitHelper(sync_base._loop) wait_helper.reject_on_timeout( timeout or 30000, f'Timeout while waiting for event "${event}"') self._future = sync_base._loop.create_task( wait_helper.wait_for_event(sync_base._impl_obj, event, predicate)) g_self = greenlet.getcurrent() def done_callback(task: Any) -> None: try: self._value = mapping.from_maybe_impl(self._future.result()) except Exception as e: self._exception = e finally: g_self.switch() self._future.add_done_callback(done_callback)
async def waitForEvent(self, event: str, predicate: Callable[[Any], bool] = None, timeout: int = None) -> Any: if timeout is None: timeout = self._timeout_settings.timeout() wait_helper = WaitHelper(self._loop) wait_helper.reject_on_timeout( timeout, f'Timeout while waiting for event "${event}"') if event != BrowserContext.Events.Close: wait_helper.reject_on_event(self, BrowserContext.Events.Close, Error("Context closed")) return await wait_helper.wait_for_event(self, event, predicate)
def __init__( self, channel_owner: ChannelOwner, event: str, predicate: Callable[[T], bool] = None, timeout: int = None, ) -> None: self._value: Optional[T] = None wait_helper = WaitHelper(channel_owner._loop) wait_helper.reject_on_timeout( timeout or 30000, f'Timeout while waiting for event "${event}"' ) self._future = asyncio.get_event_loop().create_task( wait_helper.wait_for_event(channel_owner, event, predicate) )
def _setup_navigation_wait_helper(self, timeout: int = None) -> WaitHelper: wait_helper = WaitHelper(self._loop) wait_helper.reject_on_event( self._page, "close", Error("Navigation failed because page was closed!") ) wait_helper.reject_on_event( self._page, "crash", Error("Navigation failed because page crashed!") ) wait_helper.reject_on_event( self._page, "framedetached", Error("Navigating frame was detached!"), lambda frame: frame == self, ) if timeout is None: timeout = self._page._timeout_settings.navigation_timeout() wait_helper.reject_on_timeout(timeout, f"Timeout {timeout}ms exceeded.") return wait_helper
async def waitForEvent(self, event: str, predicate: Callable[[Any], bool] = None, timeout: int = None) -> Any: if timeout is None: timeout = cast(Any, self._parent)._timeout_settings.timeout() wait_helper = WaitHelper(self._loop) wait_helper.reject_on_timeout( timeout, f'Timeout while waiting for event "${event}"') if event != WebSocket.Events.Close: wait_helper.reject_on_event(self, WebSocket.Events.Close, Error("Socket closed")) if event != WebSocket.Events.Error: wait_helper.reject_on_event(self, WebSocket.Events.Error, Error("Socket error")) wait_helper.reject_on_event(self._parent, "close", Error("Page closed")) return await wait_helper.wait_for_event(self, event, predicate)