1
0
mirror of https://github.com/danog/file.git synced 2024-12-02 09:17:57 +01:00
file/lib/Internal/UvPoll.php

63 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace Amp\File\Internal;
use Amp\Loop;
2018-07-27 21:00:26 +02:00
use Concurrent\Awaitable;
use Concurrent\Deferred;
2018-07-27 21:00:26 +02:00
class UvPoll
{
/** @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"]);
$this->watcher = Loop::repeat(\PHP_INT_MAX / 2, function () {
// do nothing, it's a dummy watcher
});
Loop::disable($this->watcher);
Loop::setState(self::class, new class($this->watcher) {
private $watcher;
2018-07-27 21:00:26 +02:00
public function __construct(string $watcher)
{
$this->watcher = $watcher;
}
2018-07-27 21:00:26 +02:00
public function __destruct()
{
Loop::cancel($this->watcher);
}
});
}
2018-07-27 21:00:26 +02:00
public function listen(Awaitable $awaitable): void
{
if ($this->requests++ === 0) {
Loop::enable($this->watcher);
}
2018-07-27 21:00:26 +02:00
Deferred::transform($awaitable, $this->onDone);
}
2018-07-27 21:00:26 +02:00
private function done(): void
{
if (--$this->requests === 0) {
Loop::disable($this->watcher);
}
\assert($this->requests >= 0);
}
}