2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-19 18:12:32 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Iterator;
|
|
|
|
use Amp\Promise;
|
2016-09-19 18:12:32 +02:00
|
|
|
|
2017-05-16 06:14:02 +02:00
|
|
|
class Listener implements Iterator, Operation {
|
|
|
|
/** @var \Amp\Iterator */
|
|
|
|
private $iterator;
|
|
|
|
|
2016-09-19 18:12:32 +02:00
|
|
|
/** @var string */
|
|
|
|
private $channel;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-12-12 02:10:14 +01:00
|
|
|
/** @var callable|null */
|
2016-09-19 18:12:32 +02:00
|
|
|
private $unlisten;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-11-18 05:00:52 +01:00
|
|
|
/** @var \Amp\Postgres\Internal\ReferenceQueue */
|
2017-08-03 07:42:53 +02:00
|
|
|
private $queue;
|
|
|
|
|
2016-09-19 18:12:32 +02:00
|
|
|
/**
|
2017-05-16 06:14:02 +02:00
|
|
|
* @param \Amp\Iterator $iterator Iterator emitting notificatons on the channel.
|
2016-09-19 18:12:32 +02:00
|
|
|
* @param string $channel Channel name.
|
2017-06-05 05:15:32 +02:00
|
|
|
* @param callable(string $channel): \Amp\Promise $unlisten Function invoked to unlisten from the channel.
|
2016-09-19 18:12:32 +02:00
|
|
|
*/
|
2017-05-16 06:14:02 +02:00
|
|
|
public function __construct(Iterator $iterator, string $channel, callable $unlisten) {
|
|
|
|
$this->iterator = $iterator;
|
2016-09-19 18:12:32 +02:00
|
|
|
$this->channel = $channel;
|
|
|
|
$this->unlisten = $unlisten;
|
2017-11-18 05:00:52 +01:00
|
|
|
$this->queue = new Internal\ReferenceQueue;
|
2016-09-19 18:12:32 +02:00
|
|
|
}
|
2017-05-16 06:14:02 +02:00
|
|
|
|
2017-07-29 17:25:06 +02:00
|
|
|
public function __destruct() {
|
|
|
|
if ($this->unlisten) {
|
2017-08-03 07:42:53 +02:00
|
|
|
$this->unlisten(); // Invokes $this->queue->complete().
|
2017-07-29 17:25:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 07:42:53 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-11-18 05:00:52 +01:00
|
|
|
public function onDestruct(callable $onComplete) {
|
|
|
|
$this->queue->onDestruct($onComplete);
|
2017-08-03 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2017-05-16 06:14:02 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function advance(): Promise {
|
|
|
|
return $this->iterator->advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2017-05-26 05:47:49 +02:00
|
|
|
*
|
|
|
|
* @return \Amp\Postgres\Notification
|
2017-05-16 06:14:02 +02:00
|
|
|
*/
|
2017-05-26 05:47:49 +02:00
|
|
|
public function getCurrent(): Notification {
|
2017-05-16 06:14:02 +02:00
|
|
|
return $this->iterator->getCurrent();
|
|
|
|
}
|
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
/**
|
|
|
|
* @return string Channel name.
|
|
|
|
*/
|
|
|
|
public function getChannel(): string {
|
|
|
|
return $this->channel;
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-19 18:12:32 +02:00
|
|
|
/**
|
2017-06-05 05:15:32 +02:00
|
|
|
* Unlistens from the channel. No more values will be emitted from this listener.
|
2016-09-19 18:12:32 +02:00
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
|
2017-07-29 17:25:06 +02:00
|
|
|
*
|
|
|
|
* @throws \Error If this method was previously invoked.
|
2016-09-19 18:12:32 +02:00
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function unlisten(): Promise {
|
2017-07-29 17:25:06 +02:00
|
|
|
if (!$this->unlisten) {
|
|
|
|
throw new \Error("Already unlistened on this channel");
|
|
|
|
}
|
|
|
|
|
2017-03-17 16:17:24 +01:00
|
|
|
/** @var \Amp\Promise $promise */
|
2016-11-15 18:06:21 +01:00
|
|
|
$promise = ($this->unlisten)($this->channel);
|
2017-07-29 17:25:06 +02:00
|
|
|
$this->unlisten = null;
|
2017-11-18 05:00:52 +01:00
|
|
|
$promise->onResolve([$this->queue, "unreference"]);
|
2016-11-15 18:06:21 +01:00
|
|
|
return $promise;
|
2016-09-19 18:12:32 +02:00
|
|
|
}
|
|
|
|
}
|