mirror of
https://github.com/danog/amp.git
synced 2024-11-27 04:24:42 +01:00
aa7b76e842
These functions mirror the standard loop methods, but run returned Generators as coroutines and invoke rethrow() on returned awaitables.
51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use Amp\Coroutine;
|
|
use Amp\Pause;
|
|
use Amp\Postponed;
|
|
use Amp\Loop\NativeLoop;
|
|
|
|
Amp\execute(function () {
|
|
try {
|
|
$postponed = new Postponed;
|
|
|
|
$observable = $postponed->getObservable();
|
|
|
|
$observable->subscribe(function ($value) {
|
|
printf("Observable emitted %d\n", $value);
|
|
return new Pause(500); // Artificial back-pressure on observable.
|
|
});
|
|
|
|
$observable->when(function ($exception, $value) {
|
|
if ($exception) {
|
|
printf("Observable failed: %s\n", $exception->getMessage());
|
|
return;
|
|
}
|
|
|
|
printf("Observable result %d\n", $value);
|
|
});
|
|
|
|
$generator = function (Postponed $postponed) {
|
|
yield $postponed->emit(new Pause(500, 1));
|
|
yield $postponed->emit(new Pause(1500, 2));
|
|
yield $postponed->emit(new Pause(1000, 3));
|
|
yield $postponed->emit(new Pause(2000, 4));
|
|
yield $postponed->emit(5);
|
|
yield $postponed->emit(6);
|
|
yield $postponed->emit(7);
|
|
yield $postponed->emit(new Pause(2000, 8));
|
|
yield $postponed->emit(9);
|
|
yield $postponed->emit(10);
|
|
$postponed->resolve(11);
|
|
};
|
|
|
|
yield new Coroutine($generator($postponed));
|
|
|
|
} catch (\Exception $exception) {
|
|
printf("Exception: %s\n", $exception);
|
|
}
|
|
}, $loop = new NativeLoop());
|