rx/await

为 RxPHP 提供await功能

2.1.0 2018-04-18 02:34 UTC

This package is auto-updated.

Last update: 2024-09-05 23:25:12 UTC


README

此库允许观察者阻塞直到完成。用于将观察者与命令式代码结合使用。

它使用Voryx 事件循环,其行为类似于JavaScript事件循环。也就是说,您无需启动它。

基本示例

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;
}

超时示例

由于观察者可以返回1个到无限多个结果,您需要确保您限制您要获取的项目数量或使用超时,否则它可能会永远阻塞。

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";