1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-27 04:24:45 +01:00
postgres/lib/Listener.php
2016-11-15 11:06:21 -06:00

48 lines
1.3 KiB
PHP

<?php declare(strict_types = 1);
namespace Amp\Postgres;
use Amp\{ Observable, Observer };
use Interop\Async\Promise;
class Listener extends Observer implements Operation {
use Internal\Operation;
/** @var string */
private $channel;
/** @var callable */
private $unlisten;
/**
* @param \Amp\Observable $observable Observable 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(Observable $observable, string $channel, callable $unlisten) {
parent::__construct($observable);
$this->channel = $channel;
$this->unlisten = $unlisten;
}
/**
* @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 \Interop\Async\Promise<\Amp\Postgres\CommandResult>
*/
public function unlisten(): Promise {
$promise = ($this->unlisten)($this->channel);
$promise->when(function () {
$this->complete();
});
return $promise;
}
}