2017-06-20 23:39:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\File\Internal;
|
|
|
|
|
|
|
|
use Amp\Loop;
|
2018-07-27 21:00:26 +02:00
|
|
|
use Concurrent\Awaitable;
|
|
|
|
use Concurrent\Deferred;
|
2017-06-20 23:39:14 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
class UvPoll
|
|
|
|
{
|
2017-06-20 23:39:14 +02:00
|
|
|
/** @var string */
|
|
|
|
private $watcher;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $requests = 0;
|
|
|
|
|
|
|
|
/** @var callable */
|
|
|
|
private $onDone;
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->onDone = \Closure::fromCallable([$this, "done"]);
|
2017-06-20 23:39:14 +02:00
|
|
|
|
|
|
|
$this->watcher = Loop::repeat(\PHP_INT_MAX / 2, function () {
|
|
|
|
// do nothing, it's a dummy watcher
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop::disable($this->watcher);
|
|
|
|
|
2017-06-21 10:18:14 +02:00
|
|
|
Loop::setState(self::class, new class($this->watcher) {
|
2017-06-20 23:39:14 +02:00
|
|
|
private $watcher;
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
public function __construct(string $watcher)
|
|
|
|
{
|
2017-06-20 23:39:14 +02:00
|
|
|
$this->watcher = $watcher;
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2017-06-21 22:35:08 +02:00
|
|
|
Loop::cancel($this->watcher);
|
2017-06-20 23:39:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
public function listen(Awaitable $awaitable): void
|
|
|
|
{
|
2017-06-20 23:39:14 +02:00
|
|
|
if ($this->requests++ === 0) {
|
|
|
|
Loop::enable($this->watcher);
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
Deferred::transform($awaitable, $this->onDone);
|
2017-06-20 23:39:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
private function done(): void
|
|
|
|
{
|
2017-06-20 23:39:14 +02:00
|
|
|
if (--$this->requests === 0) {
|
|
|
|
Loop::disable($this->watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
\assert($this->requests >= 0);
|
|
|
|
}
|
|
|
|
}
|