Skip to content

Commit 19811f5

Browse files
committed
fix(listeners): fix removing listeners (microsoft#272)
1 parent b5f8151 commit 19811f5

File tree

9 files changed

+62
-28
lines changed

9 files changed

+62
-28
lines changed

playwright/async_api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,11 +3267,6 @@ def video(self) -> typing.Union["Video", NoneType]:
32673267
"""
32683268
return mapping.from_impl_nullable(self._impl_obj.video)
32693269

3270-
def remove_listener(self, event: str, f: typing.Any) -> NoneType:
3271-
return mapping.from_maybe_impl(
3272-
self._impl_obj.remove_listener(event=event, f=mapping.to_impl(f))
3273-
)
3274-
32753270
async def opener(self) -> typing.Union["Page", NoneType]:
32763271
"""Page.opener
32773272

playwright/async_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ def once(self, event_name: str, handler: Any) -> None:
7171
self._impl_obj.once(event_name, self._wrap_handler(handler))
7272

7373
def remove_listener(self, event_name: str, handler: Any) -> None:
74-
self._impl_obj.remove_listener(event_name, handler)
74+
self._impl_obj.remove_listener(event_name, self._wrap_handler(handler))

playwright/page.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -741,22 +741,6 @@ async def waitForFunction(
741741
def workers(self) -> List["Worker"]:
742742
return self._workers.copy()
743743

744-
# on(event: str | symbol, listener: Listener): self {
745-
# if (event === Page.Events.FileChooser) {
746-
# if (!self.listenerCount(event))
747-
# self._channel.setFileChooserInterceptedNoReply({ intercepted: True });
748-
# }
749-
# super.on(event, listener);
750-
# return self;
751-
# }
752-
753-
# removeListener(event: str | symbol, listener: Listener): self {
754-
# super.removeListener(event, listener);
755-
# if (event === Page.Events.FileChooser && !self.listenerCount(event))
756-
# self._channel.setFileChooserInterceptedNoReply({ intercepted: False });
757-
# return self;
758-
# }
759-
760744
async def pdf(
761745
self,
762746
scale: int = None,

playwright/sync_api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,11 +3395,6 @@ def video(self) -> typing.Union["Video", NoneType]:
33953395
"""
33963396
return mapping.from_impl_nullable(self._impl_obj.video)
33973397

3398-
def remove_listener(self, event: str, f: typing.Any) -> NoneType:
3399-
return mapping.from_maybe_impl(
3400-
self._impl_obj.remove_listener(event=event, f=mapping.to_impl(f))
3401-
)
3402-
34033398
def opener(self) -> typing.Union["Page", NoneType]:
34043399
"""Page.opener
34053400

playwright/sync_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def once(self, event_name: str, handler: Any) -> None:
118118
self._impl_obj.once(event_name, self._wrap_handler(handler))
119119

120120
def remove_listener(self, event_name: str, handler: Any) -> None:
121-
self._impl_obj.remove_listener(event_name, handler)
121+
self._impl_obj.remove_listener(event_name, self._wrap_handler(handler))
122122

123123
def _gather(self, *actions: Callable) -> List[Any]:
124124
g_self = greenlet.getcurrent()

scripts/generate_async_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def generate(t: Any) -> None:
7878
not name.startswith("_")
7979
and isinstance(value, FunctionType)
8080
and "expect_" not in name
81+
and "remove_listener" != name
8182
):
8283
print("")
8384
if inspect.iscoroutinefunction(value):

scripts/generate_sync_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def generate(t: Any) -> None:
7878
not name.startswith("_")
7979
and isinstance(value, FunctionType)
8080
and "expect_" not in name
81+
and "remove_listener" != name
8182
):
8283
print("")
8384
print(

tests/async/test_listeners.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
async def test_listeners(page, server):
17+
log = []
18+
19+
def print_response(response):
20+
log.append(response)
21+
22+
page.on("response", print_response)
23+
await page.goto(f"{server.PREFIX}/input/textarea.html")
24+
assert len(log) > 0
25+
page.remove_listener("response", print_response)
26+
27+
log = []
28+
await page.goto(f"{server.PREFIX}/input/textarea.html")
29+
assert len(log) == 0

tests/sync/test_listeners.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def test_listeners(page, server):
17+
log = []
18+
19+
def print_response(response):
20+
log.append(response)
21+
22+
page.on("response", print_response)
23+
page.goto(f"{server.PREFIX}/input/textarea.html")
24+
assert len(log) > 0
25+
page.remove_listener("response", print_response)
26+
27+
log = []
28+
page.goto(f"{server.PREFIX}/input/textarea.html")
29+
assert len(log) == 0

0 commit comments

Comments
 (0)