mirror of
https://github.com/danog/amp.git
synced 2024-12-11 17:09:40 +01:00
fa31b4b3d5
Allows pipelines to be used directly with foreach.
39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
use Amp\Delayed;
|
|
use Amp\PipelineSource;
|
|
use Amp\Promise;
|
|
use function Amp\async;
|
|
use function Amp\await;
|
|
use function Amp\delay;
|
|
|
|
try {
|
|
/** @psalm-var PipelineSource<int> $source */
|
|
$source = new PipelineSource;
|
|
$pipeline = $source->pipe();
|
|
|
|
Promise\rethrow(async(function (PipelineSource $source): void {
|
|
$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);
|
|
$source->complete();
|
|
}, $source));
|
|
|
|
foreach ($pipeline as $value) {
|
|
\printf("Pipeline source yielded %d\n", $value);
|
|
delay(500); // Listener consumption takes 500 ms.
|
|
}
|
|
} catch (\Exception $exception) {
|
|
\printf("Exception: %s\n", (string) $exception);
|
|
}
|