rx/await

Provides await functionality for RxPHP

2.1.0 2018-04-18 02:34 UTC

This package is auto-updated.

Last update: 2024-04-05 22:22:04 UTC


README

This library will allow observables to block until complete. For use when combining observables with imperative code.

It uses the Voryx event-loop which behaves like the Javascript event-loop. ie. You don't need to start it.

Basic Example

require __DIR__ . '/../vendor/autoload.php';

//Do some aysnc craziness with observables
$observable = \Rx\Observable::interval(1000);

//Returns a `Generator` with the results of the observable
$generator = \Rx\await($observable);

//You can now use the results like a regular `Iterator`
foreach ($generator as $item) {

    //Will block here until the observable completes
    echo $item, PHP_EOL;
}

Timeout Example

Since observables can return 1 to an infinite number of results, you'll need to make sure that you either limit the number of items you take or use a timeout or it could block forever.

require __DIR__ . '/../vendor/autoload.php';

$source = \Rx\Observable::interval(1000)
    ->takeUntil(\Rx\Observable::timer(10000)); //timeout after 10 seconds

$generator = \Rx\await($source);

foreach ($generator as $item) {
    echo $item, PHP_EOL;
}

echo "DONE";
$source = \Rx\Observable::interval(1000)
    ->take(5); //Limit items to 5

$generator = \Rx\await($source);

foreach ($generator as $item) {
    echo $item, PHP_EOL;
}

echo "DONE";