Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Interval #441

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/Interval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php declare(strict_types=1);

namespace Amp;

use Revolt\EventLoop;

/**
* This object invokes the given callback within a new coroutine every $interval seconds until either the
* {@see self::disable()} method is called or the object is destroyed.
*/
final class Interval
{
private readonly string $callbackId;

/**
* @param float $interval Invoke the function every $interval seconds.
* @param \Closure():void $closure Use {@see weakClosure()} to avoid a circular reference if storing this object
* as a property of another object.
* @param bool $reference If false, unreference the underlying watcher.
*/
public function __construct(float $interval, \Closure $closure, bool $reference = true)
{
$this->callbackId = EventLoop::repeat($interval, $closure);

if (!$reference) {
EventLoop::unreference($this->callbackId);
}
}

public function __destruct()
{
EventLoop::cancel($this->callbackId);
}

/**
* @return bool True if the internal watcher is referenced.
*/
public function isReferenced(): bool
{
return EventLoop::isReferenced($this->callbackId);
}

/**
* 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->callbackId);

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->callbackId);

return $this;
}

/**
* @return bool True if the repeating timer is enabled.
*/
public function isEnabled(): bool
{
return EventLoop::isEnabled($this->callbackId);
}

/**
* Restart the repeating timer if previously stopped with {@see self::disable()}.
*
* @return $this
*/
public function enable(): self
{
EventLoop::enable($this->callbackId);

return $this;
}

/**
* Stop the repeating timer. Restart it with {@see self::enable()}.
*
* @return $this
*/
public function disable(): self
{
EventLoop::disable($this->callbackId);

return $this;
}
}
49 changes: 49 additions & 0 deletions test/IntervalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace Amp;

class IntervalTest extends TestCase
{
public function testCancelWhenDestroyed(): void
{
$timeout = 0.01;
$invocationCount = 0;
$interval = new Interval($timeout, function () use (&$invocationCount): void {
++$invocationCount;
});

delay($timeout * 1.5);

self::assertGreaterThan(0, $invocationCount);
$originalCount = $invocationCount;

unset($interval);

delay($timeout * 10);

self::assertSame($originalCount, $invocationCount);
}

public function testEnableAndDisable(): void
{
$timeout = 0.01;
$invocationCount = 0;
$interval = new Interval($timeout, function () use (&$invocationCount): void {
++$invocationCount;
});

$interval->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);
}
}
Loading