1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 10:28:01 +01:00
amp/example/emitter.php

46 lines
1.3 KiB
PHP
Raw Normal View History

2016-05-24 18:47:14 +02:00
#!/usr/bin/env php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
2016-05-27 01:20:05 +02:00
use Amp\Coroutine;
use Amp\Observable;
2016-05-24 18:47:14 +02:00
use Amp\Pause;
2016-05-27 01:20:05 +02:00
use Amp\Postponed;
2016-05-24 18:47:14 +02:00
use Amp\Loop\NativeLoop;
2016-05-27 01:20:05 +02:00
use Interop\Async\Loop;
2016-05-24 18:47:14 +02:00
Loop::execute(Amp\coroutine(function () {
try {
2016-05-27 01:20:05 +02:00
$postponed = new Postponed;
$postponed->emit(new Pause(500, 1));
$postponed->emit(new Pause(1500, 2));
$postponed->emit(new Pause(1000, 3));
$postponed->emit(new Pause(2000, 4));
$postponed->emit(5);
$postponed->emit(6);
$postponed->emit(7);
$postponed->emit(new Pause(2000, 8));
$postponed->emit(9);
$postponed->emit(10);
$postponed->complete(11);
$generator = function (Observable $observable) {
$observer = $observable->getObserver();
while (yield $observer->isValid()) {
printf("Observable emitted %d\n", $observer->getCurrent());
yield new Pause(500); // Artificial back-pressure on observer.
}
printf("Observable result %d\n", $observer->getReturn());
};
yield new Coroutine($generator($postponed->getObservable()));
} catch (\Exception $exception) {
2016-05-24 18:47:14 +02:00
printf("Exception: %s\n", $exception);
}
}), $loop = new NativeLoop());