-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move connect to json pipe (#1580)
- Loading branch information
1 parent
d2dad56
commit bddee13
Showing
8 changed files
with
106 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# 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 | ||
from typing import Dict, Optional, cast | ||
|
||
from pyee import AsyncIOEventEmitter | ||
|
||
from playwright._impl._api_types import Error | ||
from playwright._impl._connection import Channel | ||
from playwright._impl._helper import ParsedMessagePayload, parse_error | ||
from playwright._impl._transport import Transport | ||
|
||
|
||
class JsonPipeTransport(AsyncIOEventEmitter, Transport): | ||
def __init__( | ||
self, | ||
loop: asyncio.AbstractEventLoop, | ||
pipe_channel: Channel, | ||
) -> None: | ||
super().__init__(loop) | ||
Transport.__init__(self, loop) | ||
self._stop_requested = False | ||
self._pipe_channel = pipe_channel | ||
|
||
def request_stop(self) -> None: | ||
self._stop_requested = True | ||
self._loop.create_task(self._pipe_channel.send("close", {})) | ||
|
||
def dispose(self) -> None: | ||
self.on_error_future.cancel() | ||
self._stopped_future.cancel() | ||
|
||
async def wait_until_stopped(self) -> None: | ||
await self._stopped_future | ||
|
||
async def connect(self) -> None: | ||
self._stopped_future: asyncio.Future = asyncio.Future() | ||
|
||
def handle_message(message: Dict) -> None: | ||
if not self._stop_requested: | ||
self.on_message(cast(ParsedMessagePayload, message)) | ||
|
||
def handle_closed(error: Optional[Dict]) -> None: | ||
self.emit("close") | ||
self.on_error_future.set_exception( | ||
parse_error(error["error"]) | ||
if error | ||
else Error("Playwright connection closed") | ||
) | ||
self._stopped_future.set_result(None) | ||
|
||
self._pipe_channel.on( | ||
"message", | ||
lambda params: handle_message(params["message"]), | ||
) | ||
self._pipe_channel.on( | ||
"closed", | ||
lambda params: handle_closed(params.get("error")), | ||
) | ||
|
||
async def run(self) -> None: | ||
await self._stopped_future | ||
|
||
def send(self, message: Dict) -> None: | ||
if self._stop_requested: | ||
raise Error("Playwright connection closed") | ||
self._loop.create_task(self._pipe_channel.send("send", {"message": message})) |
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