input = $input; $this->loop = $loop; $this->interval = $interval ?? new PollingInterval(); $this->resume(); } public function isReadable() { return !$this->isClosed; } /** * @see ReadableResourceStream::pause() * Pause consumption of the AMQP queue but do not mark the stream as closed */ public function pause() { if (!$this->isListening) { return; } $this->input->close(); $this->loop->cancelTimer($this->periodicTimer); $this->isListening = false; } /** * @see ReadableResourceStream::resume() * Register the consumer with the broker and add consumer again */ public function resume() { if ($this->isListening || $this->isClosed) { return; } $this->input->open(); $this->periodicTimer = $this->loop->addPeriodicTimer( $this->interval->getInterval(), function () { if ($data = $this->read()) { $this->emit('data', [$data]); } } ); $this->isListening = true; } /** * @param WritableStreamInterface $dest * @param array $options * @return WritableStreamInterface * @see ReadableResourceStream::pipe() */ public function pipe(WritableStreamInterface $dest, array $options = []) { return Util::pipe($this, $dest, $options); } /** * @see ReadableResourceStream::close() */ public function close() { if ($this->isClosed) { return; } $this->isClosed = true; $this->emit('close'); $this->pause(); $this->removeAllListeners(); $this->input->close(); } private function read(): ?object { try { return $this->input->select(); } catch (Throwable $t) { $this->emit('error', [ new RuntimeException('Unable to read data from input: ' . $t->getMessage(), 0, $t), ]); $this->close(); } return null; } }