/** * Handle the command. * * @param ConnectToPersistentSubscription $command * * @return object */ function request($command) { return new Emitter(function (callable $emit) use($command) { while ($this->running) { try { $response = (yield from $this->send(new Request('GET', sprintf('/subscriptions/%s/%s/%s?embed=body', $command->getEventStreamId(), $command->getSubscriptionId(), $command->getAllowedInFlightMessages()), ['Accept' => 'application/vnd.eventstore.competingatom+json']))); $data = json_decode($response->getBody()->getContents(), true); if (count($data['entries'])) { foreach (array_reverse($data['entries']) as $entry) { try { $this->assertEvent($entry); (yield $emit(new PersistentSubsciptionStreamEventAppeared($this->buildEvent($entry)))); } catch (AssertionFailedException $e) { continue; } } } else { (yield Awaitable\resolve()->delay(0.5)); } } catch (RequestException $error) { if (404 === $error->getResponse()->getStatusCode()) { throw new NonExistentSubscriptionException(sprintf('Subscription %s-%s does not exist: %s', $command->getEventStreamId(), $command->getSubscriptionId(), $error->getMessage()), $error->getCode(), $error); } throw $error; } } }); }
/** * Input heartbeat loop */ public function inputHeartbeatLoop() { while ($this->isOpen()) { (yield Awaitable\resolve()->delay($this->heartbeat * 2)); if (!$this->something_received_between_heartbeat_checks) { (yield $this->close()); } else { $this->something_received_between_heartbeat_checks = false; } } }
/** * @coroutine * * @param float $time Time to sleep in seconds. * * @return \Generator * * @resolve float Actual time slept in seconds. */ function sleep(float $time) : \Generator { $start = (yield Awaitable\resolve(microtime(true))->delay($time)); return microtime(true) - $start; }