Skip to content

Commit bf3086c

Browse files
committed
Initial implementations of series() and parallel()
0 parents  commit bf3086c

13 files changed

Lines changed: 455 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
7+
before_script:
8+
- curl -s http://getcomposer.org/installer | php
9+
- php composer.phar install
10+
11+
script: phpunit --coverage-text

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2012 Igor Wiedler
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# React-Async
2+
3+
Async utilities for React.
4+
5+
It is heavily based on [async.js](https://github.com/caolan/async).
6+
7+
[![Build Status](https://secure.travis-ci.org/react-php/async.png?branch=master)](http://travis-ci.org/react-php/zmq)
8+
9+
## Install
10+
11+
The recommended way to install react/async is [through composer](http://getcomposer.org).
12+
13+
```JSON
14+
{
15+
"require": {
16+
"react/zmq": "dev-master"
17+
}
18+
}
19+
```
20+
21+
## Example
22+
23+
24+
25+
## Tests
26+
27+
To run the test suite, you need PHPUnit.
28+
29+
$ phpunit
30+
31+
## License
32+
33+
MIT, see LICENSE.

composer.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "react/async",
3+
"description": "Async utilities for React.",
4+
"keywords": ["async"],
5+
"license": "MIT",
6+
"require": {
7+
"php": ">=5.3.2"
8+
},
9+
"require-dev": {
10+
"react/event-loop": "dev-master"
11+
},
12+
"autoload": {
13+
"psr-0": { "React\\Async": "src" }
14+
}
15+
}

composer.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="tests/bootstrap.php"
13+
>
14+
<testsuites>
15+
<testsuite name="React-Async Test Suite">
16+
<directory>./tests/React/</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<filter>
21+
<whitelist>
22+
<directory>./src/</directory>
23+
</whitelist>
24+
</filter>
25+
</phpunit>

src/React/Async/Util.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace React\Async;
4+
5+
class Util
6+
{
7+
public static function series($tasks, $callback, $errback)
8+
{
9+
$results = array();
10+
11+
$taskCallback = function ($result) use (&$results, &$next) {
12+
$results[] = $result;
13+
$next();
14+
};
15+
16+
$done = function () use (&$results, $callback) {
17+
call_user_func($callback, $results);
18+
};
19+
20+
$next = function () use (&$tasks, $taskCallback, $errback, $done) {
21+
if (0 === count($tasks)) {
22+
$done();
23+
return;
24+
}
25+
26+
$task = array_shift($tasks);
27+
call_user_func($task, $taskCallback, $errback);
28+
};
29+
30+
$next();
31+
}
32+
33+
public static function parallel($tasks, $callback, $errback)
34+
{
35+
$results = array();
36+
$errors = array();
37+
38+
$taskErrback = function ($error) use (&$errors, &$checkDone) {
39+
$errors[] = $error;
40+
$checkDone();
41+
};
42+
43+
$done = function () use (&$results, &$errors, $callback, $errback) {
44+
if (count($errors)) {
45+
$errback(array_shift($errors));
46+
return;
47+
}
48+
49+
$callback($results);
50+
};
51+
52+
$numTasks = count($tasks);
53+
54+
if (0 === $numTasks) {
55+
$done();
56+
return;
57+
}
58+
59+
$checkDone = function () use (&$results, &$errors, $numTasks, $done) {
60+
if ($numTasks === count($results) + count($errors)) {
61+
$done();
62+
}
63+
};
64+
65+
foreach ($tasks as $i => $task) {
66+
$taskCallback = function ($result) use (&$results, $i, $checkDone) {
67+
$results[$i] = $result;
68+
$checkDone();
69+
};
70+
71+
call_user_func($task, $taskCallback, $taskErrback);
72+
}
73+
}
74+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace React\Tests\Async;
4+
5+
class TestCase extends \PHPUnit_Framework_TestCase
6+
{
7+
protected function createCallableMock($expects, $with = null)
8+
{
9+
$callable = $this->getMock('React\Tests\Async\CallableStub');
10+
11+
$method = $callable
12+
->expects($expects)
13+
->method('__invoke');
14+
15+
if ($with) {
16+
$method->with($with);
17+
}
18+
19+
return $callable;
20+
}
21+
}
22+
23+
class CallableStub
24+
{
25+
public function __invoke()
26+
{
27+
}
28+
}

tests/React/Tests/Async/Timer.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace React\Tests\Async;
4+
5+
class Timer
6+
{
7+
private $testCase;
8+
private $start;
9+
private $stop;
10+
11+
public function __construct(TestCase $testCase)
12+
{
13+
$this->testCase = $testCase;
14+
}
15+
16+
public function start()
17+
{
18+
$this->start = microtime(true);
19+
}
20+
21+
public function stop()
22+
{
23+
$this->stop = microtime(true);
24+
}
25+
26+
public function getInterval()
27+
{
28+
return $this->stop - $this->start;
29+
}
30+
31+
public function assertLessThan($milliseconds)
32+
{
33+
$this->testCase->assertLessThan($milliseconds, $this->getInterval());
34+
}
35+
36+
public function assertGreaterThan($milliseconds)
37+
{
38+
$this->testCase->assertGreaterThan($milliseconds, $this->getInterval());
39+
}
40+
41+
public function assertInRange($minMs, $maxMs)
42+
{
43+
$this->assertGreaterThan($minMs);
44+
$this->assertLessThan($maxMs);
45+
}
46+
}

0 commit comments

Comments
 (0)