Skip to content

Commit

Permalink
Update observable example
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Nov 19, 2015
1 parent 86d5373 commit b1b9c68
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions examples/observable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
$observable
->each(function ($value) {
printf("Base: %d\n", $value);
})->done(
})
->done(
function () {
printf("Base observable finished\n");
},
Expand All @@ -37,30 +38,31 @@ function (Exception $exception) {
$iter = $observable->getIterator();
while (yield $iter->wait()) {
printf("Coroutine: %d\n", $iter->getCurrent());
yield Coroutine\sleep(0.5);
yield Coroutine\sleep(0.5); // Artificial back-pressure on observable.
}
});
$coroutine->done();

$observable = $observable
->filter(function ($value) {
return $value % 2 === 0;
return $value % 2 === 0; // Only emit if $value is even.
})
->map(function ($value) {
return pow(2, $value);
return pow(2, $value); // Emit 2 ^ $value.
});


$observable->each(
function ($value) {
$observable
->each(function ($value) {
printf("Filtered and mapped: %d\n", $value);
},
function (Exception $exception) {
printf("Filtered and mapped error: %s\n", $exception->getMessage());
},
function () {
printf("Filtered and mapped observable finished\n");
}
);
})
->done(
function () {
printf("Filtered and mapped observable finished\n");
},
function (Exception $exception) {
printf("Filtered and mapped error: %s\n", $exception->getMessage());
}
);

Loop\run();

0 comments on commit b1b9c68

Please sign in to comment.