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 examples by updating to new default loop #24

Merged
merged 1 commit into from
Jul 16, 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
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ Once [installed](#install), you can use the following code to process an example
user lists by sending a (RESTful) HTTP API request for each user record:

```php
$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$concurrency = isset($argv[1]) ? $argv[1] : 3;

Expand All @@ -99,8 +98,7 @@ $transformer = new Transformer($concurrency, function ($user) use ($browser) {
// load a huge number of users to process from NDJSON file
$input = new Clue\React\NDJson\Decoder(
new React\Stream\ReadableResourceStream(
fopen(__DIR__ . '/users.ndjson', 'r'),
$loop
fopen(__DIR__ . '/users.ndjson', 'r')
),
true
);
Expand All @@ -117,7 +115,6 @@ $transformer->on('end', function () {
});
$transformer->on('error', 'printf');

$loop->run();
```

See also the [examples](examples).
Expand Down Expand Up @@ -201,8 +198,7 @@ For demonstration purposes, the examples in this documentation use
Its API can be used like this:

```php
$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$promise = $browser->get($url);
```
Expand All @@ -211,8 +207,7 @@ If you wrap this in a `Transformer` instance as given above, this code will look
like this:

```php
$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$transformer = new Transformer(10, function ($url) use ($browser) {
return $browser->get($url);
Expand Down Expand Up @@ -287,8 +282,8 @@ The resulting code with timeouts applied look something like this:
```php
use React\Promise\Timer;

$transformer = new Transformer(10, function ($uri) use ($browser, $loop) {
return Timer\timeout($browser->get($uri), 2.0, $loop);
$transformer = new Transformer(10, function ($uri) use ($browser) {
return Timer\timeout($browser->get($uri), 2.0);
});

$transformer->write($uri);
Expand Down Expand Up @@ -326,8 +321,7 @@ The following examples use an async (non-blocking) transformation handler as
given above:

```php
$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$transformer = new Transformer(10, function ($url) use ($browser) {
return $browser->get($url);
Expand Down Expand Up @@ -453,8 +447,7 @@ a promise which resolves with the total number of all successful jobs
on success.

```php
$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$promise = Transformer::all($input, 3, function ($data) use ($browser, $url) {
return $browser->post($url, [], json_encode($data));
Expand Down Expand Up @@ -559,8 +552,7 @@ a promise which resolves with the first successful resolution value on
success.

```php
$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$promise = Transformer::any($input, 3, function ($data) use ($browser, $url) {
return $browser->post($url, [], json_encode($data));
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
},
"require": {
"react/promise": "^2.5 || ^1.2.1",
"react/stream": "^1.0 || ^0.7.7"
"react/stream": "^1.2"
},
"require-dev": {
"clue/ndjson-react": "^1.0",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/http": "^1.0"
"react/http": "^1.4"
}
}
7 changes: 2 additions & 5 deletions examples/01-transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$concurrency = isset($argv[1]) ? $argv[1] : 3;

Expand Down Expand Up @@ -35,8 +34,7 @@ function (ResponseInterface $response) use ($user) {
// load a huge number of users to process from NDJSON file
$input = new Clue\React\NDJson\Decoder(
new React\Stream\ReadableResourceStream(
fopen(__DIR__ . '/users.ndjson', 'r'),
$loop
fopen(__DIR__ . '/users.ndjson', 'r')
),
true
);
Expand All @@ -53,4 +51,3 @@ function (ResponseInterface $response) use ($user) {
});
$transformer->on('error', 'printf');

$loop->run();
7 changes: 2 additions & 5 deletions examples/02-transform-all.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$concurrency = isset($argv[1]) ? $argv[1] : 3;
$url = isset($argv[2]) ? $argv[2] : 'http://httpbin.org/post';

// load a huge number of users to process from NDJSON file
$input = new Clue\React\NDJson\Decoder(
new React\Stream\ReadableResourceStream(
fopen(__DIR__ . '/users.ndjson', 'r'),
$loop
fopen(__DIR__ . '/users.ndjson', 'r')
),
true
);
Expand Down Expand Up @@ -49,4 +47,3 @@ function (Exception $e) {
}
);

$loop->run();
7 changes: 2 additions & 5 deletions examples/03-transform-any.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$concurrency = isset($argv[1]) ? $argv[1] : 3;
$url = isset($argv[2]) ? $argv[2] : 'http://httpbin.org/post';

// load a huge number of users to process from NDJSON file
$input = new Clue\React\NDJson\Decoder(
new React\Stream\ReadableResourceStream(
fopen(__DIR__ . '/users.ndjson', 'r'),
$loop
fopen(__DIR__ . '/users.ndjson', 'r')
),
true
);
Expand Down Expand Up @@ -53,4 +51,3 @@ function (Exception $e) {
}
);

$loop->run();
19 changes: 7 additions & 12 deletions src/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
* Its API can be used like this:
*
* ```php
* $loop = React\EventLoop\Factory::create();
* $browser = new React\Http\Browser($loop);
* $browser = new React\Http\Browser();
*
* $promise = $browser->get($url);
* ```
Expand All @@ -55,8 +54,7 @@
* like this:
*
* ```php
* $loop = React\EventLoop\Factory::create();
* $browser = new React\Http\Browser($loop);
* $browser = new React\Http\Browser();
*
* $transformer = new Transformer(10, function ($url) use ($browser) {
* return $browser->get($url);
Expand Down Expand Up @@ -131,8 +129,8 @@
* ```php
* use React\Promise\Timer;
*
* $transformer = new Transformer(10, function ($uri) use ($browser, $loop) {
* return Timer\timeout($browser->get($uri), 2.0, $loop);
* $transformer = new Transformer(10, function ($uri) use ($browser) {
* return Timer\timeout($browser->get($uri), 2.0);
* });
*
* $transformer->write($uri);
Expand Down Expand Up @@ -170,8 +168,7 @@
* given above:
*
* ```php
* $loop = React\EventLoop\Factory::create();
* $browser = new React\Http\Browser($loop);
* $browser = new React\Http\Browser();
*
* $transformer = new Transformer(10, function ($url) use ($browser) {
* return $browser->get($url);
Expand Down Expand Up @@ -310,8 +307,7 @@ final class Transformer extends EventEmitter implements DuplexStreamInterface
* on success.
*
* ```php
* $loop = React\EventLoop\Factory::create();
* $browser = new React\Http\Browser($loop);
* $browser = new React\Http\Browser();
*
* $promise = Transformer::all($input, 3, function ($data) use ($browser, $url) {
* return $browser->post($url, [], json_encode($data));
Expand Down Expand Up @@ -463,8 +459,7 @@ public static function all(ReadableStreamInterface $input, $concurrency, $callba
* success.
*
* ```php
* $loop = React\EventLoop\Factory::create();
* $browser = new React\Http\Browser($loop);
* $browser = new React\Http\Browser();
*
* $promise = Transformer::any($input, 3, function ($data) use ($browser, $url) {
* return $browser->post($url, [], json_encode($data));
Expand Down