-
-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace check_interval for JavaScript requests with asyncio event (see …
- Loading branch information
1 parent
91524aa
commit ea59b94
Showing
9 changed files
with
45 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from typing import Any, Dict | ||
|
||
|
||
class JavaScriptRequest: | ||
_instances: Dict[str, JavaScriptRequest] = {} | ||
|
||
def __init__(self, request_id: str, *, timeout: float) -> None: | ||
self.request_id = request_id | ||
self._instances[request_id] = self | ||
self.timeout = timeout | ||
self._event = asyncio.Event() | ||
self._result: Any = None | ||
|
||
@classmethod | ||
def resolve(cls, request_id: str, result: Any) -> None: | ||
"""Store the result of a JavaScript request and unblock the awaiter.""" | ||
request = cls._instances[request_id] | ||
request._result = result # pylint: disable=protected-access | ||
request._event.set() # pylint: disable=protected-access | ||
|
||
def __await__(self) -> Any: | ||
try: | ||
yield from asyncio.wait_for(self._event.wait(), self.timeout).__await__() | ||
except asyncio.TimeoutError as e: | ||
raise TimeoutError(f'JavaScript did not respond within {self.timeout:.1f} s') from e | ||
else: | ||
return self._result | ||
finally: | ||
self._instances.pop(self.request_id) |