Remove PayloadInterface, add README
This commit is contained in:
@@ -4,12 +4,8 @@ use JoopSchilder\React\Stream\NonBlockingInput\ReadableNonBlockingInputStream;
|
||||
use React\EventLoop\Factory;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
require_once __DIR__ . '/basic_example_classes.php';
|
||||
require_once __DIR__ . '/demo_classes.php';
|
||||
|
||||
/**
|
||||
* The input will have data available every second.
|
||||
* After 4 seconds have passed, it will generate an error.
|
||||
*/
|
||||
$input = new DemoNonBlockingInput();
|
||||
|
||||
$loop = Factory::create();
|
||||
@@ -19,4 +15,3 @@ $stream->on('error', fn() => print('e'));
|
||||
$stream->on('close', fn() => print('c'));
|
||||
$loop->addPeriodicTimer(0.2, fn() => print('.'));
|
||||
$loop->run();
|
||||
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
use JoopSchilder\React\Stream\NonBlockingInput\NonBlockingInputInterface;
|
||||
use JoopSchilder\React\Stream\NonBlockingInput\PayloadInterface;
|
||||
|
||||
final class DemoEmptyPayload implements PayloadInterface
|
||||
{
|
||||
}
|
||||
|
||||
final class DemoNonBlockingInput implements NonBlockingInputInterface
|
||||
{
|
||||
private const DATA_AVAILABLE_INTERVAL_S = 1.0;
|
||||
private const ERROR_AFTER_S = 4.0;
|
||||
|
||||
private float $lastEmission = 0;
|
||||
|
||||
private float $initialEmission = 0;
|
||||
|
||||
|
||||
public function open(): void
|
||||
{
|
||||
$this->initialEmission = microtime(true);
|
||||
}
|
||||
|
||||
|
||||
public function select(): ?PayloadInterface
|
||||
{
|
||||
$now = microtime(true);
|
||||
if ($now - $this->initialEmission > self::ERROR_AFTER_S) {
|
||||
throw new Exception('Oh no!');
|
||||
}
|
||||
if ($now - $this->lastEmission > self::DATA_AVAILABLE_INTERVAL_S) {
|
||||
$this->lastEmission = $now;
|
||||
|
||||
return new DemoEmptyPayload();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
124
examples/demo_classes.php
Normal file
124
examples/demo_classes.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
use JoopSchilder\React\Stream\NonBlockingInput\NonBlockingInputInterface;
|
||||
|
||||
/**
|
||||
* Class DemoNonBlockingInput
|
||||
* Provides an implementation of a non-blocking input that generates
|
||||
* some data every second. After a lifetime of 4 seconds, an exception
|
||||
* is thrown to show how the stream handles exceptions from the input.
|
||||
*/
|
||||
final class DemoNonBlockingInput implements NonBlockingInputInterface
|
||||
{
|
||||
private const DATA_AVAILABLE_INTERVAL_S = 1.0;
|
||||
private const ERROR_AFTER_S = 4.0;
|
||||
|
||||
private float $lastEmission = 0;
|
||||
|
||||
private float $initialEmission = 0;
|
||||
|
||||
|
||||
public function open(): void
|
||||
{
|
||||
$this->initialEmission = microtime(true);
|
||||
}
|
||||
|
||||
|
||||
public function select(): ?object
|
||||
{
|
||||
$now = microtime(true);
|
||||
if ($now - $this->initialEmission > self::ERROR_AFTER_S) {
|
||||
throw new Exception('Oh no!');
|
||||
}
|
||||
if ($now - $this->lastEmission > self::DATA_AVAILABLE_INTERVAL_S) {
|
||||
$this->lastEmission = $now;
|
||||
|
||||
return new stdClass();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Joke
|
||||
* Used as a payload for the DemoJokeGenerator.
|
||||
* @see DemoJokeGenerator
|
||||
*/
|
||||
final class Joke
|
||||
{
|
||||
private string $joke;
|
||||
|
||||
|
||||
public function __construct(string $joke)
|
||||
{
|
||||
$this->joke = $joke;
|
||||
}
|
||||
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->joke;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Class DemoJokeGenerator
|
||||
* Generates a random joke at a random interval (0 - 2 seconds).
|
||||
*/
|
||||
final class DemoJokeGenerator implements NonBlockingInputInterface
|
||||
{
|
||||
private float $lastEmission = 0;
|
||||
|
||||
private float $deadline = 0;
|
||||
|
||||
private array $jokes = [
|
||||
'What did the Buddhist ask the hot dog vendor? - Make me one with everything.',
|
||||
'You know why you never see elephants hiding up in trees? - Because they’re really good at it.',
|
||||
'What is red and smells like blue paint? - Red paint.',
|
||||
'A dyslexic man walks into a bra.',
|
||||
'Where does the General keep his armies? - In his sleevies!',
|
||||
'What do you call bears with no ears? - B',
|
||||
'Why dont blind people skydive? - Because it scares the crap out of their dogs.',
|
||||
];
|
||||
|
||||
|
||||
private function scheduleNextJoke()
|
||||
{
|
||||
$this->lastEmission = microtime(true);
|
||||
$delay = mt_rand() / mt_getrandmax();
|
||||
$this->deadline = $this->lastEmission + (2.0 * $delay);
|
||||
}
|
||||
|
||||
|
||||
public function open(): void
|
||||
{
|
||||
$this->scheduleNextJoke();
|
||||
}
|
||||
|
||||
|
||||
public function select(): ?object
|
||||
{
|
||||
$now = microtime(true);
|
||||
if ($now > $this->deadline) {
|
||||
$this->scheduleNextJoke();
|
||||
|
||||
return new Joke($this->jokes[array_rand($this->jokes)]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
16
examples/jokes_example.php
Normal file
16
examples/jokes_example.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
use JoopSchilder\React\Stream\NonBlockingInput\ReadableNonBlockingInputStream;
|
||||
use React\EventLoop\Factory;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
require_once __DIR__ . '/demo_classes.php';
|
||||
|
||||
$input = new DemoJokeGenerator();
|
||||
|
||||
$loop = Factory::create();
|
||||
$stream = new ReadableNonBlockingInputStream($input, $loop);
|
||||
$stream->on('data', fn(Joke $joke) => print($joke . PHP_EOL));
|
||||
$loop->addPeriodicTimer(0.2, fn() => print('.' . PHP_EOL));
|
||||
$loop->run();
|
||||
|
||||
Reference in New Issue
Block a user