2017-06-13 12:41:47 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
2021-03-26 22:34:32 +01:00
|
|
|
use Revolt\EventLoop\Loop;
|
|
|
|
|
2017-06-13 12:41:47 -05:00
|
|
|
/**
|
|
|
|
* A TimeoutCancellationToken automatically requests cancellation after the timeout has elapsed.
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
final class TimeoutCancellationToken implements CancellationToken
|
|
|
|
{
|
2020-09-24 22:17:13 -05:00
|
|
|
private string $watcher;
|
2017-06-13 12:41:47 -05:00
|
|
|
|
2020-09-24 22:17:13 -05:00
|
|
|
private CancellationToken $token;
|
2017-06-13 12:41:47 -05:00
|
|
|
|
|
|
|
/**
|
2021-08-29 12:18:24 -05:00
|
|
|
* @param float $timeout Seconds until cancellation is requested.
|
2020-02-28 13:35:37 -06:00
|
|
|
* @param string $message Message for TimeoutException. Default is "Operation timed out".
|
2017-06-13 12:41:47 -05:00
|
|
|
*/
|
2021-08-29 12:18:24 -05:00
|
|
|
public function __construct(float $timeout, string $message = "Operation timed out")
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2021-08-30 00:07:10 -05:00
|
|
|
$this->token = $source = new Internal\CancellableToken;
|
|
|
|
|
|
|
|
$trace = null; // Defined in case assertions are disabled.
|
|
|
|
\assert($trace = \debug_backtrace());
|
2017-06-13 12:41:47 -05:00
|
|
|
|
2020-10-02 13:55:46 -05:00
|
|
|
$this->watcher = Loop::delay($timeout, static function () use ($source, $message, $trace): void {
|
2021-08-30 00:07:10 -05:00
|
|
|
if ($trace) {
|
|
|
|
$message .= \sprintf("\r\n%s was created here: %s", self::class, Internal\formatStacktrace($trace));
|
|
|
|
} else {
|
|
|
|
$message .= \sprintf(" (Enable assertions for a backtrace of the %s creation)", self::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
$source->cancel(new TimeoutException($message));
|
2017-06-13 12:41:47 -05:00
|
|
|
});
|
2020-04-04 17:05:26 +02:00
|
|
|
|
2017-06-13 12:41:47 -05:00
|
|
|
Loop::unreference($this->watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels the delay watcher.
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2017-06-13 12:41:47 -05:00
|
|
|
Loop::cancel($this->watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function subscribe(callable $callback): string
|
|
|
|
{
|
2017-06-13 12:41:47 -05:00
|
|
|
return $this->token->subscribe($callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2020-09-24 22:17:13 -05:00
|
|
|
public function unsubscribe(string $id): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-06-13 12:41:47 -05:00
|
|
|
$this->token->unsubscribe($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function isRequested(): bool
|
|
|
|
{
|
2017-06-13 12:41:47 -05:00
|
|
|
return $this->token->isRequested();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2020-09-24 22:17:13 -05:00
|
|
|
public function throwIfRequested(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-06-13 12:41:47 -05:00
|
|
|
$this->token->throwIfRequested();
|
|
|
|
}
|
|
|
|
}
|