forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_wait_helper.py
128 lines (107 loc) · 4.13 KB
/
_wait_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import uuid
from asyncio.tasks import Task
from typing import Any, Callable, List, Tuple
from pyee import EventEmitter
from playwright._impl._api_types import Error, TimeoutError
from playwright._impl._connection import ChannelOwner
class WaitHelper:
def __init__(self, channel_owner: ChannelOwner, event: str) -> None:
self._result: asyncio.Future = asyncio.Future()
self._wait_id = uuid.uuid4().hex
self._loop = channel_owner._loop
self._pending_tasks: List[Task] = []
self._channel = channel_owner._channel
self._registered_listeners: List[Tuple[EventEmitter, str, Callable]] = []
self._wait_for_event_info_before(self._wait_id, event)
def _wait_for_event_info_before(self, wait_id: str, event: str) -> None:
self._channel.send_no_reply(
"waitForEventInfo",
{
"info": {
"waitId": wait_id,
"phase": "before",
"event": event,
}
},
)
def _wait_for_event_info_after(self, wait_id: str, error: Exception = None) -> None:
try:
self._channel.send_no_reply(
"waitForEventInfo",
{
"info": {
"waitId": wait_id,
"phase": "after",
"error": str(error) if error else None,
}
},
)
except Exception:
pass
def reject_on_event(
self,
emitter: EventEmitter,
event: str,
error: Error,
predicate: Callable = None,
) -> None:
def listener(event_data: Any = None) -> None:
if not predicate or predicate(event_data):
self._reject(error)
emitter.on(event, listener)
self._registered_listeners.append((emitter, event, listener))
def reject_on_timeout(self, timeout: float, message: str) -> None:
if timeout == 0:
return
async def reject() -> None:
await asyncio.sleep(timeout / 1000)
self._reject(TimeoutError(message))
self._pending_tasks.append(self._loop.create_task(reject()))
def _cleanup(self) -> None:
for task in self._pending_tasks:
if not task.done():
task.cancel()
for listener in self._registered_listeners:
listener[0].remove_listener(listener[1], listener[2])
def _fulfill(self, result: Any) -> None:
self._cleanup()
if not self._result.done():
self._result.set_result(result)
self._wait_for_event_info_after(self._wait_id)
def _reject(self, exception: Exception) -> None:
self._cleanup()
if not self._result.done():
self._result.set_exception(exception)
self._wait_for_event_info_after(self._wait_id, exception)
def wait_for_event(
self,
emitter: EventEmitter,
event: str,
predicate: Callable = None,
) -> None:
def listener(event_data: Any = None) -> None:
if not predicate or predicate(event_data):
self._fulfill(event_data)
emitter.on(event, listener)
self._registered_listeners.append((emitter, event, listener))
def result(self) -> asyncio.Future:
return self._result
def throw_on_timeout(timeout: float, exception: Exception) -> asyncio.Task:
async def throw() -> None:
await asyncio.sleep(timeout / 1000)
raise exception
return asyncio.create_task(throw())