From 06d4d9d94d4e64bdebc14de922a860044e002a7a Mon Sep 17 00:00:00 2001 From: Slava Vishnyakov Date: Fri, 4 May 2018 14:30:38 +0300 Subject: [PATCH] Attempt to fix https://github.com/jakubkulhan/chrome-devtools-protocol/issues/14 --- src/ChromeDevtoolsProtocol/DevtoolsClient.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/ChromeDevtoolsProtocol/DevtoolsClient.php b/src/ChromeDevtoolsProtocol/DevtoolsClient.php index 5abc842e..863ef387 100644 --- a/src/ChromeDevtoolsProtocol/DevtoolsClient.php +++ b/src/ChromeDevtoolsProtocol/DevtoolsClient.php @@ -34,6 +34,12 @@ class DevtoolsClient implements DevtoolsClientInterface, InternalClientInterface /** @var object[][] */ private $eventBuffers = []; + /** @var array method => waitersCount */ + private $awaitMethods = []; + + /** @var array method => message to bubble upstream */ + private $awaitMessages = []; + public function __construct(string $wsUrl) { $this->wsClient = new WebSocketClient($wsUrl, "http://" . parse_url($wsUrl, PHP_URL_HOST)); @@ -129,10 +135,31 @@ private function handleMessage($message, ?string $returnIfEventMethod = null) throw new ErrorException($message->error->message, $message->error->code); } else if (isset($message->method)) { + if (isset($this->awaitMethods[$message->method]) && $this->awaitMethods[$message->method] > 0) { + $this->awaitMessages[$message->method] [] = $message; + + return null; + } + if (isset($this->listeners[$message->method])) { + if ($returnIfEventMethod !== null) { + if (!isset($this->awaitMethods[$returnIfEventMethod])) { + $this->awaitMethods[$returnIfEventMethod] = 1; + } else { + $this->awaitMethods[$returnIfEventMethod]++; + } + } + foreach ($this->listeners[$message->method] as $callback) { $callback($message->params); } + + if ($returnIfEventMethod !== null && count($this->awaitMessages[$returnIfEventMethod]) > 0) { + $message = array_shift($this->awaitMessages[$returnIfEventMethod]); + $this->awaitMethods[$returnIfEventMethod]--; + + return $message; + } } if ($returnIfEventMethod !== null && $message->method === $returnIfEventMethod) {