Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage by supporting new default loop #4

Merged
merged 1 commit into from Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 10 additions & 16 deletions README.md
Expand Up @@ -45,23 +45,22 @@ composer](http://getcomposer.org).
<?php

use React\Async\Util as Async;

$loop = React\EventLoop\Factory::create();
use React\EventLoop\Loop;

Async::parallel(
array(
function ($callback, $errback) use ($loop) {
$loop->addTimer(1, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(1, function () use ($callback) {
$callback('Slept for a whole second');
});
},
function ($callback, $errback) use ($loop) {
$loop->addTimer(1, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(1, function () use ($callback) {
$callback('Slept for another whole second');
});
},
function ($callback, $errback) use ($loop) {
$loop->addTimer(1, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(1, function () use ($callback) {
$callback('Slept for yet another whole second');
});
},
Expand All @@ -75,8 +74,6 @@ Async::parallel(
throw $e;
}
);

$loop->run();
```

### Waterfall
Expand All @@ -85,16 +82,15 @@ $loop->run();
<?php

use React\Async\Util as Async;
use React\EventLoop\Loop;

$loop = React\EventLoop\Factory::create();

$addOne = function ($prev, $callback = null) use ($loop) {
$addOne = function ($prev, $callback = null) {
if (!$callback) {
$callback = $prev;
$prev = 0;
}

$loop->addTimer(1, function () use ($prev, $callback) {
Loop::addTimer(1, function () use ($prev, $callback) {
$callback($prev + 1);
});
};
Expand All @@ -108,8 +104,6 @@ Async::waterfall(array(
$callback();
},
));

$loop->run();
```

## Todo
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -30,10 +30,10 @@
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^4.8.35",
"react/event-loop": "0.2.*"
"react/event-loop": "^1.2"
},
"suggest": {
"react/event-loop": "You need some kind of event loop for this to make sense."
"react/event-loop": "You need an event loop for this to make sense."
},
"autoload": {
"psr-4": { "React\\Async\\": "src/" }
Expand Down
21 changes: 9 additions & 12 deletions tests/UtilParallelTest.php
Expand Up @@ -3,6 +3,7 @@
namespace React\Tests\Async;

use React\Async\Util;
use React\EventLoop\Loop;

class UtilParallelTest extends TestCase
{
Expand All @@ -18,16 +19,14 @@ public function testParallelWithoutTasks()

public function testParallelWithTasks()
{
$loop = new \React\EventLoop\StreamSelectLoop();

$tasks = array(
function ($callback, $errback) use ($loop) {
$loop->addTimer(0.1, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(0.1, function () use ($callback) {
$callback('foo');
});
},
function ($callback, $errback) use ($loop) {
$loop->addTimer(0.1, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(0.1, function () use ($callback) {
$callback('bar');
});
},
Expand All @@ -41,7 +40,7 @@ function ($callback, $errback) use ($loop) {
$timer = new Timer($this);
$timer->start();

$loop->run();
Loop::run();

$timer->stop();
$timer->assertInRange(0.1, 0.2);
Expand Down Expand Up @@ -78,15 +77,13 @@ public function testParallelWithDelayedError()
{
$called = 0;

$loop = new \React\EventLoop\StreamSelectLoop();

$tasks = array(
function ($callback, $errback) use (&$called) {
$callback('foo');
$called++;
},
function ($callback, $errback) use ($loop) {
$loop->addTimer(0.001, function () use ($errback) {
function ($callback, $errback) {
Loop::addTimer(0.001, function () use ($errback) {
$e = new \RuntimeException('whoops');
$errback($e);
});
Expand All @@ -102,7 +99,7 @@ function ($callback, $errback) use (&$called) {

Util::parallel($tasks, $callback, $errback);

$loop->run();
Loop::run();

$this->assertSame(2, $called);
}
Expand Down
13 changes: 6 additions & 7 deletions tests/UtilSeriesTest.php
Expand Up @@ -3,6 +3,7 @@
namespace React\Tests\Async;

use React\Async\Util;
use React\EventLoop\Loop;

class UtilSeriesTest extends TestCase
{
Expand All @@ -18,16 +19,14 @@ public function testSeriesWithoutTasks()

public function testSeriesWithTasks()
{
$loop = new \React\EventLoop\StreamSelectLoop();

$tasks = array(
function ($callback, $errback) use ($loop) {
$loop->addTimer(0.05, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(0.05, function () use ($callback) {
$callback('foo');
});
},
function ($callback, $errback) use ($loop) {
$loop->addTimer(0.05, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(0.05, function () use ($callback) {
$callback('bar');
});
},
Expand All @@ -41,7 +40,7 @@ function ($callback, $errback) use ($loop) {
$timer = new Timer($this);
$timer->start();

$loop->run();
Loop::run();

$timer->stop();
$timer->assertInRange(0.10, 0.20);
Expand Down
17 changes: 8 additions & 9 deletions tests/UtilWaterfallTest.php
Expand Up @@ -3,6 +3,7 @@
namespace React\Tests\Async;

use React\Async\Util;
use React\EventLoop\Loop;

class UtilWaterfallTest extends TestCase
{
Expand All @@ -18,21 +19,19 @@ public function testWaterfallWithoutTasks()

public function testWaterfallWithTasks()
{
$loop = new \React\EventLoop\StreamSelectLoop();

$tasks = array(
function ($callback, $errback) use ($loop) {
$loop->addTimer(0.05, function () use ($callback) {
function ($callback, $errback) {
Loop::addTimer(0.05, function () use ($callback) {
$callback('foo');
});
},
function ($foo, $callback, $errback) use ($loop) {
$loop->addTimer(0.05, function () use ($callback, $foo) {
function ($foo, $callback, $errback) {
Loop::addTimer(0.05, function () use ($callback, $foo) {
$callback($foo.'bar');
});
},
function ($bar, $callback, $errback) use ($loop) {
$loop->addTimer(0.05, function () use ($callback, $bar) {
function ($bar, $callback, $errback) {
Loop::addTimer(0.05, function () use ($callback, $bar) {
$callback($bar.'baz');
});
},
Expand All @@ -46,7 +45,7 @@ function ($bar, $callback, $errback) use ($loop) {
$timer = new Timer($this);
$timer->start();

$loop->run();
Loop::run();

$timer->stop();
$timer->assertInRange(0.15, 0.30);
Expand Down