rx / await
Provides await functionality for RxPHP
Installs: 2 083
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- reactivex/rxphp: ^2.0
- voryx/event-loop: ^2.0 || ^3.0
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-11-05 23:57:24 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";