Skip to content

Change to use functional API instead of static Util class #6

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

Merged
merged 2 commits into from
Oct 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 78 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,52 @@ whatsoever!*
**Table of Contents**

* [Usage](#usage)
* [Parallel](#parallel)
* [Waterfall](#waterfall)
* [parallel()](#parallel)
* [series()](#series)
* [waterfall()](#waterfall)
* [Todo](#todo)
* [Install](#install)
* [Tests](#tests)
* [License](#license)

## Usage

### Parallel
This lightweight library consists only of a few simple functions.
All functions reside under the `React\Async` namespace.

The below examples refer to all functions with their fully-qualified names like this:

```php
React\Async\parallel(…);
```

As of PHP 5.6+ you can also import each required function into your code like this:

```php
use function React\Async\parallel;

parallel(…);
```

Alternatively, you can also use an import statement similar to this:

```php
use React\Async;

Async\parallel(…);
```

### parallel()

The `parallel(array<callable> $tasks, ?callable $callback = null, ?callable $errback = null): void` function can be used
like this:

```php
<?php

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

Async::parallel(
React\Async\parallel(
array(
function ($callback, $errback) {
Loop::addTimer(1, function () use ($callback) {
Expand All @@ -65,18 +93,59 @@ Async::parallel(
var_dump($result);
}
},
function (\Exception $e) {
function (Exception $e) {
throw $e;
}
);
```

### Waterfall
### series()

The `series(array<callable> $tasks, ?callable $callback = null, ?callable $errback = null): void` function can be used
like this:

```php
<?php

use React\EventLoop\Loop;

React\Async\series(
array(
function ($callback, $errback) {
Loop::addTimer(1, function () use ($callback) {
$callback('Slept for a whole second');
});
},
function ($callback, $errback) {
Loop::addTimer(1, function () use ($callback) {
$callback('Slept for another whole second');
});
},
function ($callback, $errback) {
Loop::addTimer(1, function () use ($callback) {
$callback('Slept for yet another whole second');
});
},
),
function (array $results) {
foreach ($results as $result) {
var_dump($result);
}
},
function (Exception $e) {
throw $e;
}
);
```

### waterfall()

The `waterfall(array<callable> $tasks, ?callable $callback = null, ?callable $errback = null): void` function can be used
like this:

```php
<?php

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

$addOne = function ($prev, $callback = null) {
Expand All @@ -90,7 +159,7 @@ $addOne = function ($prev, $callback = null) {
});
};

Async::waterfall(array(
React\Async\waterfall(array(
$addOne,
$addOne,
$addOne,
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"react/event-loop": "You need an event loop for this to make sense."
},
"autoload": {
"psr-4": { "React\\Async\\": "src/" }
"files": [
"src/functions_include.php"
]
},
"autoload-dev": {
"psr-4": { "React\\Tests\\Async\\": "tests/" }
Expand Down
106 changes: 0 additions & 106 deletions src/Util.php

This file was deleted.

122 changes: 122 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace React\Async;

/**
* @param array<callable> $tasks
* @param ?callable $callback
* @param ?callable $errback
* @return void
*/
function parallel(array $tasks, $callback = null, $errback = null)
{
$results = array();
$errors = array();

$done = function () use (&$results, &$errors, $callback, $errback) {
if (!$callback) {
return;
}

if (count($errors)) {
$errback(array_shift($errors));
return;
}

$callback($results);
};

$numTasks = count($tasks);

if (0 === $numTasks) {
$done();
return;
}

$checkDone = function () use (&$results, &$errors, $numTasks, $done) {
if ($numTasks === count($results) + count($errors)) {
$done();
}
};

$taskErrback = function ($error) use (&$errors, $checkDone) {
$errors[] = $error;
$checkDone();
};

foreach ($tasks as $i => $task) {
$taskCallback = function ($result) use (&$results, $i, $checkDone) {
$results[$i] = $result;
$checkDone();
};

call_user_func($task, $taskCallback, $taskErrback);
}
}

/**
* @param array<callable> $tasks
* @param ?callable $callback
* @param ?callable $errback
* @return void
*/
function series(array $tasks, $callback = null, $errback = null)
{
$results = array();

/** @var callable():void $next */
$taskCallback = function ($result) use (&$results, &$next) {
$results[] = $result;
$next();
};

$done = function () use (&$results, $callback) {
if ($callback) {
call_user_func($callback, $results);
}
};

$next = function () use (&$tasks, $taskCallback, $errback, $done) {
if (0 === count($tasks)) {
$done();
return;
}

$task = array_shift($tasks);
call_user_func($task, $taskCallback, $errback);
};

$next();
}

/**
* @param array<callable> $tasks
* @param ?callable $callback
* @param ?callable $errback
* @return void
*/
function waterfall(array $tasks, $callback = null, $errback = null)
{
$taskCallback = function () use (&$next) {
call_user_func_array($next, func_get_args());
};

$done = function () use ($callback) {
if ($callback) {
call_user_func_array($callback, func_get_args());
}
};

$next = function () use (&$tasks, $taskCallback, $errback, $done) {
if (0 === count($tasks)) {
call_user_func_array($done, func_get_args());
return;
}

$task = array_shift($tasks);
$args = array_merge(func_get_args(), array($taskCallback, $errback));
call_user_func_array($task, $args);
};

$next();
}
8 changes: 8 additions & 0 deletions src/functions_include.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace React\Async;

// @codeCoverageIgnoreStart
if (!function_exists(__NAMESPACE__ . '\\parallel')) {
require __DIR__ . '/functions.php';
}
Loading