1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/example/emitter.php

48 lines
1.3 KiB
PHP
Raw Normal View History

2016-05-24 11:47:14 -05:00
#!/usr/bin/env php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\Pause;
2016-05-26 18:20:05 -05:00
use Amp\Postponed;
2016-05-24 11:47:14 -05:00
use Amp\Loop\NativeLoop;
2016-05-26 18:20:05 -05:00
use Interop\Async\Loop;
2016-05-24 11:47:14 -05:00
2016-05-27 15:44:01 -05:00
Loop::execute(function () {
2016-05-24 11:47:14 -05:00
try {
2016-05-26 18:20:05 -05:00
$postponed = new Postponed;
2016-05-27 15:44:01 -05:00
Loop::defer(function () use ($postponed) {
2016-05-29 12:07:21 -05:00
$postponed->emit(1);
$postponed->emit(2);
$postponed->emit(3);
$postponed->emit(4);
2016-05-27 15:44:01 -05:00
$postponed->emit(5);
$postponed->emit(6);
$postponed->emit(7);
2016-05-29 12:07:21 -05:00
$postponed->emit(8);
2016-05-27 15:44:01 -05:00
$postponed->emit(9);
$postponed->emit(10);
2016-05-29 11:35:09 -05:00
$postponed->resolve(11);
2016-05-27 15:44:01 -05:00
});
$observable = $postponed->getObservable();
$disposable = $observable->subscribe(function ($value) {
printf("Observable emitted %d\n", $value);
return new Pause(500); // Artificial back-pressure on observable, but is ignored.
});
$disposable->when(function ($exception, $value) {
if ($exception) {
printf("Exception: %s\n", $exception->getMessage());
return;
2016-05-26 18:20:05 -05:00
}
2016-05-27 15:44:01 -05:00
printf("Observable result %d\n", $value);
});
2016-05-26 18:20:05 -05:00
} catch (\Exception $exception) {
2016-05-24 11:47:14 -05:00
printf("Exception: %s\n", $exception);
}
2016-05-27 15:44:01 -05:00
}, $loop = new NativeLoop());