Skip to content

Commit

Permalink
Refactor read watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jun 22, 2017
1 parent 60d57ad commit 8e37f2a
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/PgSqlExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public function __construct($handle, $socket) {
$listeners = &$this->listeners;

$this->poll = Loop::onReadable($socket, static function ($watcher) use (&$deferred, &$listeners, $handle) {
$status = \pg_consume_input($handle);
if (!\pg_consume_input($handle)) {
if ($deferred !== null) {
$deferred->fail(new FailureException(\pg_last_error($handle)));
}
return;
}

while ($result = \pg_get_notify($handle, \PGSQL_ASSOC)) {
$channel = $result["message"];
Expand All @@ -66,13 +71,13 @@ public function __construct($handle, $socket) {
return; // No active query, only notification listeners.
}

if (!$status) {
$deferred->fail(new FailureException(\pg_last_error($handle)));
} elseif (!\pg_connection_busy($handle)) {
$deferred->resolve(\pg_get_result($handle));
if (\pg_connection_busy($handle)) {
return;
}

if (!\pg_connection_busy($handle) && empty($listeners)) {
$deferred->resolve(\pg_get_result($handle));

if (empty($listeners)) {
Loop::disable($watcher);
}
});
Expand Down

0 comments on commit 8e37f2a

Please sign in to comment.