Skip to content

Commit

Permalink
[remote] Return remove_listener function from wdspec bidi client BiDi…
Browse files Browse the repository at this point in the history
…Session::add_event_listener

Depends on D122109

Differential Revision: https://phabricator.services.mozilla.com/D122577

bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1694143
gecko-commit: 798401a59feecba5ae7d6ca54dd9536949a3d2b3
gecko-reviewers: webdriver-reviewers, whimboo
  • Loading branch information
juliandescottes authored and jgraham committed Sep 10, 2021
1 parent f375321 commit 7e50457
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tools/webdriver/webdriver/bidi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ class BidiSession:
interface for running commands in the session, and for attaching
event handlers to the session. For example:
async def on_log(data):
async def on_log(method, data):
print(data)
session = BidiSession("ws://localhost:4445", capabilities)
session.add_event_listener("log.entryAdded", on_log)
remove_listener = session.add_event_listener("log.entryAdded", on_log)
await session.start()
await session.subscribe("log.entryAdded")
# Do some stuff with the session
remove_listener()
session.end()
If the session id is provided it's assumed that the underlying
Expand Down Expand Up @@ -201,17 +204,22 @@ async def end(self) -> None:

def add_event_listener(self,
name: Optional[str],
fn: Callable[[str, Mapping[str, Any]], Awaitable[Any]]) -> None:
fn: Callable[[str, Mapping[str, Any]],
Awaitable[Any]]) -> Callable[[], None]:
"""Add a listener for the event with a given name.
If name is None, the listener is called for all messages that are not otherwise
handled.
:param name: Name of event to listen for or None to register a default handler
:param fn: Async callback function that receives event data
:return: Function to remove the added listener
"""
self.event_listeners[name].append(fn)

return lambda: self.event_listeners[name].remove(fn)


class Transport:
"""Low level message handler for the WebSockets connection"""
Expand Down

0 comments on commit 7e50457

Please sign in to comment.