Skip to content

Commit

Permalink
fixes jakubkulhan#14 - buffer all events and check buffer always
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerk committed Jul 3, 2019
1 parent 96a5d5b commit 808853e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
24 changes: 7 additions & 17 deletions src/ChromeDevtoolsProtocol/DevtoolsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,21 @@ public function awaitEvent(ContextInterface $ctx, string $method)
$this->eventBuffers = [];

for (; ;) {
$eventMessage = null;

$this->getWsClient()->setDeadline($ctx->getDeadline());
foreach ($this->getWsClient()->receive() ?: [] as $payload) {
/** @var Payload $payload */
$message = json_decode($payload->getPayload());

$nextEventMessage = $this->handleMessage($message, $eventMessage === null ? $method : null);

if ($nextEventMessage !== null) {
$eventMessage = $nextEventMessage;
}
$this->handleMessage($message);
}

if ($eventMessage !== null) {
return $eventMessage->params;
if (!empty($this->eventBuffers[$method])) {
return array_shift($this->eventBuffers[$method])->params;
}
}
}

private function handleMessage($message, ?string $returnIfEventMethod = null)
private function handleMessage($message)
{
if (isset($message->error)) {
throw new ErrorException($message->error->message, $message->error->code);
Expand All @@ -141,14 +135,10 @@ private function handleMessage($message, ?string $returnIfEventMethod = null)
}
}

if ($returnIfEventMethod !== null && $message->method === $returnIfEventMethod) {
return $message;
} else {
if (!isset($this->eventBuffers[$message->method])) {
$this->eventBuffers[$message->method] = [];
}
array_push($this->eventBuffers[$message->method], $message);
if (!isset($this->eventBuffers[$message->method])) {
$this->eventBuffers[$message->method] = [];
}
array_push($this->eventBuffers[$message->method], $message);

} else if (isset($message->id)) {
$this->commandResults[$message->id] = $message->result ?? new \stdClass();
Expand Down
24 changes: 7 additions & 17 deletions src/ChromeDevtoolsProtocol/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,23 @@ public function awaitEvent(ContextInterface $ctx, string $method)
}

for (; ;) {
$eventMessage = null;

$receivedMessage = $this->browser->target()->awaitReceivedMessageFromTarget($ctx);

if ($receivedMessage->targetId === $this->targetId && $receivedMessage->sessionId === $this->sessionId) {

$nextEventMessage = $this->handleMessage(json_decode($receivedMessage->message), $method);

if ($eventMessage === null && $nextEventMessage !== null) {
$eventMessage = $nextEventMessage;
}
$this->handleMessage(json_decode($receivedMessage->message));
}

if ($eventMessage !== null) {
return $eventMessage->params;
if (!empty($this->eventBuffers[$method])) {
return array_shift($this->eventBuffers[$method])->params;
}
}
}

/**
* @internal
*/
private function handleMessage($message, ?string $returnIfEventMethod = null)
private function handleMessage($message)
{
if (isset($message->error)) {
throw new ErrorException($message->error->message, $message->error->code);
Expand All @@ -148,14 +142,10 @@ private function handleMessage($message, ?string $returnIfEventMethod = null)
}
}

if ($returnIfEventMethod !== null && $message->method === $returnIfEventMethod) {
return $message;
} else {
if (!isset($this->eventBuffers[$message->method])) {
$this->eventBuffers[$message->method] = [];
}
array_push($this->eventBuffers[$message->method], $message);
if (!isset($this->eventBuffers[$message->method])) {
$this->eventBuffers[$message->method] = [];
}
array_push($this->eventBuffers[$message->method], $message);

} else if (isset($message->id)) {
$this->commandResults[$message->id] = $message->result ?? new \stdClass();
Expand Down

0 comments on commit 808853e

Please sign in to comment.