Simplified codebase in a major refactor before moving to initial Toalett release
This commit is contained in:
15
src/EndlessTrait.php
Normal file
15
src/EndlessTrait.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\React\Stream;
|
||||
|
||||
trait EndlessTrait
|
||||
{
|
||||
public function close(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function eof(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace JoopSchilder\React\Stream\NonBlockingInput;
|
||||
|
||||
interface NonBlockingInputInterface
|
||||
{
|
||||
|
||||
function open(): void;
|
||||
|
||||
|
||||
function select(): ?object;
|
||||
|
||||
|
||||
function close(): void;
|
||||
|
||||
}
|
||||
5
src/README.md
Normal file
5
src/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
Three files, because there are only three things to do:
|
||||
|
||||
- Create a data source by implementing `Source`.
|
||||
- If it is endless, use the `EndlessTrait` in your implementation.
|
||||
- Create a readable stream with the `StreamAdapter`.
|
||||
@@ -1,140 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace JoopSchilder\React\Stream\NonBlockingInput;
|
||||
|
||||
use Evenement\EventEmitter;
|
||||
use JoopSchilder\React\Stream\NonBlockingInput\ValueObject\PollingInterval;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\EventLoop\TimerInterface;
|
||||
use React\Stream\ReadableResourceStream;
|
||||
use React\Stream\ReadableStreamInterface;
|
||||
use React\Stream\Util;
|
||||
use React\Stream\WritableStreamInterface;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @see ReadableStreamInterface
|
||||
* @see ReadableResourceStream
|
||||
*/
|
||||
final class ReadableNonBlockingInputStream extends EventEmitter implements ReadableStreamInterface
|
||||
{
|
||||
private NonBlockingInputInterface $input;
|
||||
|
||||
private LoopInterface $loop;
|
||||
|
||||
private PollingInterval $interval;
|
||||
|
||||
private ?TimerInterface $periodicTimer = null;
|
||||
|
||||
private bool $isClosed = false;
|
||||
|
||||
private bool $isListening = false;
|
||||
|
||||
|
||||
/**
|
||||
* @see ReadableStreamInterface
|
||||
* @see ReadableResourceStream for an example
|
||||
*/
|
||||
public function __construct(NonBlockingInputInterface $input, LoopInterface $loop, ?PollingInterval $interval = null)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
17
src/Source.php
Normal file
17
src/Source.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\React\Stream;
|
||||
|
||||
interface Source
|
||||
{
|
||||
public function open(): void;
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function select();
|
||||
|
||||
public function close(): void;
|
||||
|
||||
public function eof(): bool;
|
||||
}
|
||||
107
src/StreamAdapter.php
Normal file
107
src/StreamAdapter.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\React\Stream;
|
||||
|
||||
use Evenement\EventEmitterTrait;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\EventLoop\TimerInterface;
|
||||
use React\Stream\ReadableResourceStream;
|
||||
use React\Stream\ReadableStreamInterface;
|
||||
use React\Stream\Util;
|
||||
use React\Stream\WritableStreamInterface;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* @see ReadableResourceStream
|
||||
*/
|
||||
final class StreamAdapter implements ReadableStreamInterface
|
||||
{
|
||||
use EventEmitterTrait;
|
||||
|
||||
private Source $source;
|
||||
private LoopInterface $loop;
|
||||
private float $pollingInterval;
|
||||
private ?TimerInterface $timer = null;
|
||||
private bool $closed = false;
|
||||
private bool $listening = false;
|
||||
|
||||
public function __construct(Source $source, LoopInterface $loop, float $pollingInterval = 0.5)
|
||||
{
|
||||
$this->source = $source;
|
||||
$this->loop = $loop;
|
||||
$this->pollingInterval = $pollingInterval;
|
||||
|
||||
$this->resume();
|
||||
}
|
||||
|
||||
public function isReadable(): bool
|
||||
{
|
||||
return !$this->closed;
|
||||
}
|
||||
|
||||
public function pause(): void
|
||||
{
|
||||
if (!$this->listening) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->source->close();
|
||||
$this->loop->cancelTimer($this->timer);
|
||||
$this->listening = false;
|
||||
}
|
||||
|
||||
public function resume(): void
|
||||
{
|
||||
if ($this->listening || $this->closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->source->open();
|
||||
$this->timer = $this->loop->addPeriodicTimer($this->pollingInterval, function () {
|
||||
while (!is_null($data = $this->read())) {
|
||||
$this->emit('data', [$data]);
|
||||
}
|
||||
if ($this->source->eof()) {
|
||||
$this->emit('end');
|
||||
$this->close();
|
||||
}
|
||||
});
|
||||
$this->listening = true;
|
||||
}
|
||||
|
||||
public function pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface
|
||||
{
|
||||
return Util::pipe($this, $dest, $options);
|
||||
}
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
if ($this->closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->closed = true;
|
||||
|
||||
$this->emit('close');
|
||||
$this->pause();
|
||||
$this->removeAllListeners();
|
||||
|
||||
$this->source->close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
private function read()
|
||||
{
|
||||
try {
|
||||
return $this->source->select();
|
||||
} catch (Throwable $t) {
|
||||
$this->emit('error', [new RuntimeException('Unable to read data from source: ' . $t->getMessage(), 0, $t)]);
|
||||
$this->close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace JoopSchilder\React\Stream\NonBlockingInput\ValueObject;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class PollingInterval
|
||||
{
|
||||
private const DEFAULT_INTERVAL = 0.05;
|
||||
|
||||
private float $interval;
|
||||
|
||||
|
||||
public function __construct(float $interval = self::DEFAULT_INTERVAL)
|
||||
{
|
||||
if ($interval < 0.0) {
|
||||
throw new InvalidArgumentException('Interval must be greater than 0');
|
||||
}
|
||||
|
||||
$this->interval = $interval;
|
||||
}
|
||||
|
||||
|
||||
public function getInterval(): float
|
||||
{
|
||||
return $this->interval;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user