stream instanceof NullStream) { return call(static function (): void { }); } return call(function (): \Generator { yield parent::connect(); $this->run(); }); } /** * @param array|string $body * * @return Promise */ public function publish(string $topic, string | array $body, int $delay = 0): Promise { if (0 < $delay) { return call( function (array $bodies) use ($topic, $delay): \Generator { foreach ($bodies as $body) { yield $this->stream->write(Command::dpub($topic, $body, $delay)); } }, (array) $body, ); } $command = \is_array($body) ? Command::mpub($topic, $body) : Command::pub($topic, $body); return $this->stream->write($command); } private function run(): void { $buffer = new Buffer(); asyncCall(function () use ($buffer): \Generator { while (null !== $chunk = yield $this->stream->read()) { $buffer->append($chunk); while ($frame = Parser::parse($buffer)) { switch (true) { case $frame instanceof Frame\Response: if ($frame->isHeartBeat()) { yield $this->stream->write(Command::nop()); } // Ok received break; case $frame instanceof Frame\Error: $this->handleError($frame); break; default: throw new NsqException('Unreachable statement.'); } } } $this->stream = new NullStream(); }); } }