mirror of
https://github.com/danog/postgres.git
synced 2024-12-15 19:07:26 +01:00
32369079e6
Replaced with CompletionQueue.
88 lines
2.2 KiB
PHP
88 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
use Amp\Iterator;
|
|
use Amp\Promise;
|
|
|
|
class Listener implements Iterator, Operation {
|
|
/** @var \Amp\Iterator */
|
|
private $iterator;
|
|
|
|
/** @var string */
|
|
private $channel;
|
|
|
|
/** @var callable */
|
|
private $unlisten;
|
|
|
|
/** @var \Amp\Postgres\Internal\CompletionQueue */
|
|
private $queue;
|
|
|
|
/**
|
|
* @param \Amp\Iterator $iterator Iterator emitting notificatons on the channel.
|
|
* @param string $channel Channel name.
|
|
* @param callable(string $channel): \Amp\Promise $unlisten Function invoked to unlisten from the channel.
|
|
*/
|
|
public function __construct(Iterator $iterator, string $channel, callable $unlisten) {
|
|
$this->iterator = $iterator;
|
|
$this->channel = $channel;
|
|
$this->unlisten = $unlisten;
|
|
$this->queue = new Internal\CompletionQueue;
|
|
}
|
|
|
|
public function __destruct() {
|
|
if ($this->unlisten) {
|
|
$this->unlisten(); // Invokes $this->queue->complete().
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function onComplete(callable $onComplete) {
|
|
$this->queue->onComplete($onComplete);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function advance(): Promise {
|
|
return $this->iterator->advance();
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*
|
|
* @return \Amp\Postgres\Notification
|
|
*/
|
|
public function getCurrent(): Notification {
|
|
return $this->iterator->getCurrent();
|
|
}
|
|
|
|
/**
|
|
* @return string Channel name.
|
|
*/
|
|
public function getChannel(): string {
|
|
return $this->channel;
|
|
}
|
|
|
|
/**
|
|
* Unlistens from the channel. No more values will be emitted from this listener.
|
|
*
|
|
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
|
|
*
|
|
* @throws \Error If this method was previously invoked.
|
|
*/
|
|
public function unlisten(): Promise {
|
|
if (!$this->unlisten) {
|
|
throw new \Error("Already unlistened on this channel");
|
|
}
|
|
|
|
/** @var \Amp\Promise $promise */
|
|
$promise = ($this->unlisten)($this->channel);
|
|
$this->unlisten = null;
|
|
$promise->onResolve([$this->queue, "complete"]);
|
|
return $promise;
|
|
}
|
|
}
|