1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/lib/TimeoutCancellationToken.php
2018-06-18 20:00:01 +02:00

70 lines
1.4 KiB
PHP

<?php
namespace Amp;
/**
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
*/
final class TimeoutCancellationToken implements CancellationToken
{
/** @var string */
private $watcher;
/** @var \Amp\CancellationToken */
private $token;
/**
* @param int $timeout Milliseconds until cancellation is requested.
*/
public function __construct(int $timeout)
{
$source = new CancellationTokenSource;
$this->token = $source->getToken();
$this->watcher = Loop::delay($timeout, static function () use ($source) {
$source->cancel(new TimeoutException);
});
Loop::unreference($this->watcher);
}
/**
* Cancels the delay watcher.
*/
public function __destruct()
{
Loop::cancel($this->watcher);
}
/**
* {@inheritdoc}
*/
public function subscribe(callable $callback): string
{
return $this->token->subscribe($callback);
}
/**
* {@inheritdoc}
*/
public function unsubscribe(string $id)
{
$this->token->unsubscribe($id);
}
/**
* {@inheritdoc}
*/
public function isRequested(): bool
{
return $this->token->isRequested();
}
/**
* {@inheritdoc}
*/
public function throwIfRequested()
{
$this->token->throwIfRequested();
}
}