Skip to content

Commit 6bc5afc

Browse files
authored
chore: cleanup route handling (microsoft#973)
1 parent 0be6efe commit 6bc5afc

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

playwright/_impl/_browser_context.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import asyncio
16-
import inspect
1716
import json
1817
from pathlib import Path
1918
from types import SimpleNamespace
@@ -154,20 +153,14 @@ def _on_page(self, page: Page) -> None:
154153
page._opener.emit(Page.Events.Popup, page)
155154

156155
def _on_route(self, route: Route, request: Request) -> None:
157-
handled = False
158156
for handler_entry in self._routes:
159157
if handler_entry.matches(request.url):
160-
result = handler_entry.handle(route, request)
161-
if inspect.iscoroutine(result):
162-
asyncio.create_task(result)
163-
handled = True
158+
if handler_entry.handle(route, request):
159+
self._routes.remove(handler_entry)
160+
if not len(self._routes) == 0:
161+
asyncio.create_task(self._disable_interception())
164162
break
165-
if not handled:
166-
asyncio.create_task(route.continue_())
167-
else:
168-
self._routes = list(
169-
filter(lambda route: route.expired() is False, self._routes)
170-
)
163+
route._internal_continue()
171164

172165
def _on_binding(self, binding_call: BindingCall) -> None:
173166
func = self._bindings.get(binding_call._initializer["name"])
@@ -279,9 +272,10 @@ async def unroute(
279272
)
280273
)
281274
if len(self._routes) == 0:
282-
await self._channel.send(
283-
"setNetworkInterceptionEnabled", dict(enabled=False)
284-
)
275+
await self._disable_interception()
276+
277+
async def _disable_interception(self) -> None:
278+
await self._channel.send("setNetworkInterceptionEnabled", dict(enabled=False))
285279

286280
def expect_event(
287281
self,

playwright/_impl/_helper.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import asyncio
1515
import fnmatch
16+
import inspect
1617
import math
1718
import os
1819
import re
@@ -207,25 +208,26 @@ def __init__(
207208
self,
208209
matcher: URLMatcher,
209210
handler: RouteHandlerCallback,
210-
times: Optional[int],
211+
times: Optional[int] = None,
211212
):
212213
self.matcher = matcher
213214
self.handler = handler
214-
self._times = times
215+
self._times = times if times else 2 ** 32
215216
self._handled_count = 0
216217

217-
def expired(self) -> bool:
218-
return self._times is not None and self._handled_count >= self._times
219-
220218
def matches(self, request_url: str) -> bool:
221219
return self.matcher.matches(request_url)
222220

223-
def handle(self, route: "Route", request: "Request") -> Union[Coroutine, Any]:
224-
if self._times:
221+
def handle(self, route: "Route", request: "Request") -> bool:
222+
try:
223+
result = cast(
224+
Callable[["Route", "Request"], Union[Coroutine, Any]], self.handler
225+
)(route, request)
226+
if inspect.iscoroutine(result):
227+
asyncio.create_task(result)
228+
finally:
225229
self._handled_count += 1
226-
return cast(
227-
Callable[["Route", "Request"], Union[Coroutine, Any]], self.handler
228-
)(route, request)
230+
return self._handled_count >= self._times
229231

230232

231233
def is_safe_close_error(error: Exception) -> bool:

playwright/_impl/_network.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ async def continue_(
238238
overrides["postData"] = base64.b64encode(postData).decode()
239239
await self._channel.send("continue", cast(Any, overrides))
240240

241+
def _internal_continue(self) -> None:
242+
async def continue_route() -> None:
243+
try:
244+
await self.continue_()
245+
except Exception:
246+
pass
247+
248+
asyncio.create_task(continue_route())
249+
241250

242251
class Response(ChannelOwner):
243252
def __init__(

playwright/_impl/_page.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,14 @@ def _on_frame_detached(self, frame: Frame) -> None:
212212
self.emit(Page.Events.FrameDetached, frame)
213213

214214
def _on_route(self, route: Route, request: Request) -> None:
215-
handled = False
216215
for handler_entry in self._routes:
217216
if handler_entry.matches(request.url):
218-
result = handler_entry.handle(route, request)
219-
if inspect.iscoroutine(result):
220-
asyncio.create_task(result)
221-
handled = True
222-
break
223-
if not handled:
224-
self._browser_context._on_route(route, request)
225-
else:
226-
self._routes = list(
227-
filter(lambda route: route.expired() is False, self._routes)
228-
)
217+
if handler_entry.handle(route, request):
218+
self._routes.remove(handler_entry)
219+
if len(self._routes) == 0:
220+
asyncio.create_task(self._disable_interception())
221+
return
222+
self._browser_context._on_route(route, request)
229223

230224
def _on_binding(self, binding_call: "BindingCall") -> None:
231225
func = self._bindings.get(binding_call._initializer["name"])
@@ -575,9 +569,10 @@ async def unroute(
575569
)
576570
)
577571
if len(self._routes) == 0:
578-
await self._channel.send(
579-
"setNetworkInterceptionEnabled", dict(enabled=False)
580-
)
572+
await self._disable_interception()
573+
574+
async def _disable_interception(self) -> None:
575+
await self._channel.send("setNetworkInterceptionEnabled", dict(enabled=False))
581576

582577
async def screenshot(
583578
self,

0 commit comments

Comments
 (0)