mirror of
https://github.com/danog/amp.git
synced 2024-12-03 09:57:51 +01:00
fa31b4b3d5
Allows pipelines to be used directly with foreach.
41 lines
1021 B
PHP
41 lines
1021 B
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
use Amp\Loop;
|
|
use Amp\Pipeline;
|
|
use Amp\PipelineSource;
|
|
use function Amp\delay;
|
|
|
|
try {
|
|
/** @psalm-var PipelineSource<int> $source */
|
|
$source = new PipelineSource;
|
|
|
|
Loop::defer(function () use ($source): void {
|
|
// Source emits all values at once without awaiting back-pressure.
|
|
$source->emit(1);
|
|
$source->emit(2);
|
|
$source->emit(3);
|
|
$source->emit(4);
|
|
$source->emit(5);
|
|
$source->emit(6);
|
|
$source->emit(7);
|
|
$source->emit(8);
|
|
$source->emit(9);
|
|
$source->emit(10);
|
|
$source->complete();
|
|
});
|
|
|
|
$pipeline = $source->pipe();
|
|
|
|
// Use Amp\Pipeline\toIterator() to use a pipeline with foreach.
|
|
foreach ($pipeline as $value) {
|
|
\printf("Pipeline source yielded %d\n", $value);
|
|
delay(100); // Listener consumption takes 100 ms.
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
\printf("Exception: %s\n", (string) $exception);
|
|
}
|
|
|