From 742512a5c86cfe8e1a0a22c4c57e8fd18b556012 Mon Sep 17 00:00:00 2001 From: Jur Balledux Date: Fri, 27 May 2016 10:02:36 +0200 Subject: [PATCH] If tick is called, wait for next like the run() loop does --- src/StreamSelectLoop.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index e51a27f8..36fcac21 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -159,7 +159,29 @@ public function tick() $this->timers->tick(); - $this->waitForStreamActivity(0); + // Next-tick or future-tick queues have pending callbacks ... + if (!$this->nextTickQueue->isEmpty() || !$this->futureTickQueue->isEmpty()) { + $timeout = 0; + + // There is a pending timer, only block until it is due ... + } elseif ($scheduledAt = $this->timers->getFirst()) { + $timeout = $scheduledAt - $this->timers->getTime(); + if ($timeout < 0) { + $timeout = 0; + } else { + $timeout *= self::MICROSECONDS_PER_SECOND; + } + + // The only possible event is stream activity, so wait forever ... + } elseif ($this->readStreams || $this->writeStreams) { + $timeout = null; + + // There's nothing left to do ... + } else { + return; + } + + $this->waitForStreamActivity($timeout); } /**