Remove Subscriber

This commit is contained in:
2021-01-30 18:22:34 +03:00
parent 1a24efacfb
commit e1cca2d3eb
4 changed files with 26 additions and 50 deletions

View File

@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Nsq;
use Generator;
use Nsq\Config\ClientConfig;
use Nsq\Exception\NsqError;
use Nsq\Exception\NsqException;
@@ -34,6 +35,24 @@ final class Consumer extends Connection
$this->command('SUB', [$this->topic, $this->channel])->checkIsOK();
}
/**
* @psalm-return Generator<int, Message|float|null, int|null, void>
*/
public function generator(): Generator
{
while (true) {
$this->rdy(1);
$command = yield $this->readMessage();
if (0 === $command) {
break;
}
}
$this->disconnect();
}
public function readMessage(): ?Message
{
$frame = $this->readFrame();

View File

@@ -1,38 +0,0 @@
<?php
declare(strict_types=1);
namespace Nsq;
use Generator;
use Nsq\Protocol\Message;
final class Subscriber
{
public const STOP = 0;
private Consumer $reader;
public function __construct(Consumer $reader)
{
$this->reader = $reader;
}
/**
* @psalm-return Generator<int, Message|float|null, int|null, void>
*/
public function run(): Generator
{
while (true) {
$this->reader->rdy(1);
$command = yield $this->reader->readMessage();
if (self::STOP === $command) {
break;
}
}
$this->reader->disconnect();
}
}