1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00
file/lib/Internal/UvPoll.php
Niklas Keller b7a54c1691 Don't store a loop reference in poll objects
Now we no longer have a circular reference there.
2017-06-21 22:35:08 +02:00

59 lines
1.2 KiB
PHP

<?php
namespace Amp\File\Internal;
use Amp\CallableMaker;
use Amp\Loop;
use Amp\Promise;
class UvPoll {
use CallableMaker;
/** @var string */
private $watcher;
/** @var int */
private $requests = 0;
/** @var callable */
private $onDone;
public function __construct() {
$this->onDone = $this->callableFromInstanceMethod("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;
public function __construct(string $watcher) {
$this->watcher = $watcher;
}
public function __destruct() {
Loop::cancel($this->watcher);
}
});
}
public function listen(Promise $promise) {
if ($this->requests++ === 0) {
Loop::enable($this->watcher);
}
$promise->onResolve($this->onDone);
}
private function done() {
if (--$this->requests === 0) {
Loop::disable($this->watcher);
}
\assert($this->requests >= 0);
}
}