Skip to content

Commit

Permalink
Attempt to fix jakubkulhan#14
Browse files Browse the repository at this point in the history
  • Loading branch information
slava-vishnyakov committed May 4, 2018
1 parent 7692585 commit 06d4d9d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/ChromeDevtoolsProtocol/DevtoolsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 06d4d9d

Please sign in to comment.