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

48 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';
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
2016-05-27 22:44:01 +02:00
Loop::execute(function () {
2016-05-24 18:47:14 +02:00
try {
2016-05-27 01:20:05 +02:00
$postponed = new Postponed;
2016-05-27 22:44:01 +02:00
Loop::defer(function () use ($postponed) {
2016-05-29 19:07:21 +02:00
$postponed->emit(1);
$postponed->emit(2);
$postponed->emit(3);
$postponed->emit(4);
2016-05-27 22:44:01 +02:00
$postponed->emit(5);
$postponed->emit(6);
$postponed->emit(7);
2016-05-29 19:07:21 +02:00
$postponed->emit(8);
2016-05-27 22:44:01 +02:00
$postponed->emit(9);
$postponed->emit(10);
2016-05-29 18:35:09 +02:00
$postponed->resolve(11);
2016-05-27 22:44:01 +02: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-27 01:20:05 +02:00
}
2016-05-27 22:44:01 +02:00
printf("Observable result %d\n", $value);
});
2016-05-27 01:20:05 +02:00
} catch (\Exception $exception) {
2016-05-24 18:47:14 +02:00
printf("Exception: %s\n", $exception);
}
2016-05-27 22:44:01 +02:00
}, $loop = new NativeLoop());