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 1 commit
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 $watcher;
trowski marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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->watcher = EventLoop::repeat($interval, $closure);

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

public function __destruct()
{
EventLoop::cancel($this->watcher);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there also be a manual cancel method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense for the object to represent a valid repeating timer. If we allow external cancellation, then the object needs to expose an isCancelled() method so the state isn't hidden.


/**
* @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;
}
}
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