2020-05-13 17:15:21 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
|
|
|
|
use Amp\Delayed;
|
2020-08-23 16:18:28 +02:00
|
|
|
use Amp\PipelineSource;
|
2020-09-25 05:14:58 +02:00
|
|
|
use Amp\Promise;
|
|
|
|
use function Amp\async;
|
|
|
|
use function Amp\await;
|
2020-10-07 06:40:14 +02:00
|
|
|
use function Amp\delay;
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-09-25 05:14:58 +02:00
|
|
|
try {
|
|
|
|
/** @psalm-var PipelineSource<int> $source */
|
|
|
|
$source = new PipelineSource;
|
|
|
|
$pipeline = $source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-09-25 05:14:58 +02:00
|
|
|
Promise\rethrow(async(function (PipelineSource $source): void {
|
2020-09-25 19:32:37 +02:00
|
|
|
$source->yield(await(new Delayed(500, 1)));
|
|
|
|
$source->yield(await(new Delayed(1500, 2)));
|
|
|
|
$source->yield(await(new Delayed(1000, 3)));
|
|
|
|
$source->yield(await(new Delayed(2000, 4)));
|
|
|
|
$source->yield(5);
|
|
|
|
$source->yield(6);
|
|
|
|
$source->yield(7);
|
|
|
|
$source->yield(await(new Delayed(2000, 8)));
|
|
|
|
$source->yield(9);
|
|
|
|
$source->yield(10);
|
2020-09-25 05:14:58 +02:00
|
|
|
$source->complete();
|
|
|
|
}, $source));
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-11-10 19:05:47 +01:00
|
|
|
foreach ($pipeline as $value) {
|
2020-09-25 05:14:58 +02:00
|
|
|
\printf("Pipeline source yielded %d\n", $value);
|
2020-10-07 06:40:14 +02:00
|
|
|
delay(500); // Listener consumption takes 500 ms.
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
2020-09-25 05:14:58 +02:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
\printf("Exception: %s\n", (string) $exception);
|
|
|
|
}
|