mirror of
https://github.com/danog/amp.git
synced 2024-11-27 12:35:02 +01:00
50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
#!/usr/bin/env php
|
|
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use Amp\Observable;
|
|
use Amp\Observer;
|
|
use Amp\Pause;
|
|
use Amp\Postponed;
|
|
use Amp\Loop\NativeLoop;
|
|
use Interop\Async\Loop;
|
|
|
|
Loop::execute(Amp\coroutine(function () {
|
|
try {
|
|
$postponed = new Postponed;
|
|
|
|
Loop::defer(function () use ($postponed) {
|
|
$postponed->emit(1);
|
|
$postponed->emit(2);
|
|
$postponed->emit(3);
|
|
$postponed->emit(4);
|
|
$postponed->emit(5);
|
|
$postponed->emit(6);
|
|
$postponed->emit(7);
|
|
$postponed->emit(8);
|
|
$postponed->emit(9);
|
|
$postponed->emit(10);
|
|
$postponed->resolve(11);
|
|
});
|
|
|
|
$observable = $postponed->getObservable();
|
|
|
|
$generator = function (Observable $observable) {
|
|
$observer = new Observer($observable);
|
|
|
|
while (yield $observer->isValid()) {
|
|
printf("Observable emitted %d\n", $observer->getCurrent());
|
|
yield new Pause(100);
|
|
}
|
|
|
|
printf("Observable result %d\n", $observer->getReturn());
|
|
};
|
|
|
|
yield new \Amp\Coroutine($generator($observable));
|
|
|
|
} catch (\Exception $exception) {
|
|
printf("Exception: %s\n", $exception);
|
|
}
|
|
}), $loop = new NativeLoop());
|