1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-14 02:17:27 +01:00
postgres/lib/Listener.php
2017-05-17 18:14:12 +02:00

65 lines
1.5 KiB
PHP

<?php
namespace Amp\Postgres;
use Amp\{ Iterator, Promise };
class Listener implements Iterator, Operation {
use Internal\Operation;
/** @var \Amp\Iterator */
private $iterator;
/** @var string */
private $channel;
/** @var callable */
private $unlisten;
/**
* @param \Amp\Iterator $iterator Iterator emitting notificatons on the channel.
* @param string $channel Channel name.
* @param callable(string $channel): void $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;
}
/**
* {@inheritdoc}
*/
public function advance(): Promise {
return $this->iterator->advance();
}
/**
* {@inheritdoc}
*/
public function getCurrent() {
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 on theis channel.
*
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
*/
public function unlisten(): Promise {
/** @var \Amp\Promise $promise */
$promise = ($this->unlisten)($this->channel);
$promise->onResolve(function () {
$this->complete();
});
return $promise;
}
}