From 556bb1451b99f0f32246349e8f29375842f06b10 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Wed, 17 Jul 2024 23:02:25 -0500 Subject: [PATCH 1/2] Add Interval --- src/Interval.php | 98 +++++++++++++++++++++++++++++++++++++++++++ test/IntervalTest.php | 49 ++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/Interval.php create mode 100644 test/IntervalTest.php diff --git a/src/Interval.php b/src/Interval.php new file mode 100644 index 00000000..9877598c --- /dev/null +++ b/src/Interval.php @@ -0,0 +1,98 @@ +watcher = EventLoop::repeat($interval, $closure); + + if (!$reference) { + EventLoop::unreference($this->watcher); + } + } + + public function __destruct() + { + EventLoop::cancel($this->watcher); + } + + /** + * @return bool True if the internal watcher is referenced. + */ + public function isReferenced(): bool + { + return EventLoop::isReferenced($this->watcher); + } + + /** + * References the internal watcher in the event loop, keeping the loop running while the repeat loop is enabled. + * + * @return $this + */ + public function reference(): self + { + EventLoop::reference($this->watcher); + + return $this; + } + + /** + * Unreferences the internal watcher in the event loop, allowing the loop to stop while the repeat loop is enabled. + * + * @return $this + */ + public function unreference(): self + { + EventLoop::unreference($this->watcher); + + return $this; + } + + /** + * @return bool True if the repeating timer is enabled. + */ + public function isEnabled(): bool + { + return EventLoop::isEnabled($this->watcher); + } + + /** + * Restart the repeating timer if previously stopped with {@see self::disable()}. + * + * @return $this + */ + public function enable(): self + { + EventLoop::enable($this->watcher); + + return $this; + } + + /** + * Stop the repeating timer. Restart it with {@see self::enable()}. + * + * @return $this + */ + public function disable(): self + { + EventLoop::disable($this->watcher); + + return $this; + } +} diff --git a/test/IntervalTest.php b/test/IntervalTest.php new file mode 100644 index 00000000..1f163a30 --- /dev/null +++ b/test/IntervalTest.php @@ -0,0 +1,49 @@ +disable(); + self::assertFalse($interval->isEnabled()); + + delay($timeout * 2); + + self::assertSame(0, $invocationCount); + + $interval->enable(); + self::assertTrue($interval->isEnabled()); + + delay($timeout * 1.5); + + self::assertSame(1, $invocationCount); + } +} From e5d135cc8051fee7741ad3926e3e56f096ef9da4 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Wed, 24 Jul 2024 18:21:20 -0500 Subject: [PATCH 2/2] Rename property --- src/Interval.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Interval.php b/src/Interval.php index 9877598c..7618a31a 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -10,7 +10,7 @@ */ final class Interval { - private readonly string $watcher; + private readonly string $callbackId; /** * @param float $interval Invoke the function every $interval seconds. @@ -20,16 +20,16 @@ final class Interval */ public function __construct(float $interval, \Closure $closure, bool $reference = true) { - $this->watcher = EventLoop::repeat($interval, $closure); + $this->callbackId = EventLoop::repeat($interval, $closure); if (!$reference) { - EventLoop::unreference($this->watcher); + EventLoop::unreference($this->callbackId); } } public function __destruct() { - EventLoop::cancel($this->watcher); + EventLoop::cancel($this->callbackId); } /** @@ -37,7 +37,7 @@ public function __destruct() */ public function isReferenced(): bool { - return EventLoop::isReferenced($this->watcher); + return EventLoop::isReferenced($this->callbackId); } /** @@ -47,7 +47,7 @@ public function isReferenced(): bool */ public function reference(): self { - EventLoop::reference($this->watcher); + EventLoop::reference($this->callbackId); return $this; } @@ -59,7 +59,7 @@ public function reference(): self */ public function unreference(): self { - EventLoop::unreference($this->watcher); + EventLoop::unreference($this->callbackId); return $this; } @@ -69,7 +69,7 @@ public function unreference(): self */ public function isEnabled(): bool { - return EventLoop::isEnabled($this->watcher); + return EventLoop::isEnabled($this->callbackId); } /** @@ -79,7 +79,7 @@ public function isEnabled(): bool */ public function enable(): self { - EventLoop::enable($this->watcher); + EventLoop::enable($this->callbackId); return $this; } @@ -91,7 +91,7 @@ public function enable(): self */ public function disable(): self { - EventLoop::disable($this->watcher); + EventLoop::disable($this->callbackId); return $this; }