From 628d8cbab1d48a5cdcd027dfc60db71ee19e0da3 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Thu, 20 Apr 2017 11:13:17 -0500 Subject: [PATCH] Set next "now" at the beginning of watcher activation --- lib/Loop/EventDriver.php | 6 ++++-- lib/Loop/NativeDriver.php | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/Loop/EventDriver.php b/lib/Loop/EventDriver.php index aafc444..08975bb 100644 --- a/lib/Loop/EventDriver.php +++ b/lib/Loop/EventDriver.php @@ -179,6 +179,8 @@ class EventDriver extends Driver { * {@inheritdoc} */ protected function activate(array $watchers) { + $now = (int) (\microtime(true) * self::MILLISEC_PER_SEC); + foreach ($watchers as $watcher) { if (!isset($this->events[$id = $watcher->id])) { switch ($watcher->type) { @@ -233,7 +235,7 @@ class EventDriver extends Driver { switch ($watcher->type) { case Watcher::DELAY: case Watcher::REPEAT: - $interval = $watcher->value - ($diff ?? ($diff = (int) (\microtime(true) * self::MILLISEC_PER_SEC) - $this->now)); + $interval = $watcher->value - ($now - $this->now); $this->events[$id]->add($interval > 0 ? $interval / self::MILLISEC_PER_SEC : 0); break; @@ -247,7 +249,7 @@ class EventDriver extends Driver { } } - $this->now = (int) (\microtime(true) * self::MILLISEC_PER_SEC); + $this->now = $now; } /** diff --git a/lib/Loop/NativeDriver.php b/lib/Loop/NativeDriver.php index 9e65b41..a5626c5 100644 --- a/lib/Loop/NativeDriver.php +++ b/lib/Loop/NativeDriver.php @@ -239,6 +239,8 @@ class NativeDriver extends Driver { * {@inheritdoc} */ protected function activate(array $watchers) { + $now = (int) (\microtime(true) * self::MILLISEC_PER_SEC); + foreach ($watchers as $watcher) { switch ($watcher->type) { case Watcher::READABLE: @@ -281,7 +283,7 @@ class NativeDriver extends Driver { } } - $this->now = (int) (\microtime(true) * self::MILLISEC_PER_SEC); + $this->now = $now; } /**