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 #29

Merged
merged 1 commit into from
Jul 26, 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
30 changes: 11 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ Once [installed](#install), you can use the following code to access an
HTTP webserver and send a large number of HTTP GET requests:

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

// load a huge array of URLs to fetch
$urls = file('urls.txt');
Expand All @@ -83,7 +82,6 @@ foreach ($urls as $url) {
});
}

$loop->run();
```

See also the [examples](examples).
Expand Down Expand Up @@ -162,8 +160,7 @@ The demonstration purposes, the examples in this documentation use
may use any Promise-based API with this project. 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 @@ -172,8 +169,7 @@ If you wrap this in a `Queue` 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();

$q = new Queue(10, null, function ($url) use ($browser) {
return $browser->get($url);
Expand Down Expand Up @@ -226,7 +222,7 @@ underlying resources.
```php
$promise = $q($url);

$loop->addTimer(2.0, function () use ($promise) {
Loop::addTimer(2.0, function () use ($promise) {
$promise->cancel();
});
```
Expand All @@ -250,8 +246,8 @@ The resulting code with timeouts applied look something like this:
```php
use React\Promise\Timer;

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

$promise = $q($uri);
Expand All @@ -266,7 +262,7 @@ executing this operation can not take longer than the given timeout:

```php
// usually not recommended
$promise = Timer\timeout($q($url), 2.0, $loop);
$promise = Timer\timeout($q($url), 2.0);
```

Please refer to [react/promise-timer](https://github.com/reactphp/promise-timer)
Expand All @@ -283,8 +279,7 @@ schedule all jobs while limiting concurrency to ensure no more than
resolves with the results of all jobs on success.

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

$promise = Queue::all(3, $urls, function ($url) use ($browser) {
return $browser->get($url);
Expand Down Expand Up @@ -360,8 +355,7 @@ resolves with the result of the first job on success and will then try
to `cancel()` all outstanding jobs.

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

$promise = Queue::any(3, $urls, function ($url) use ($browser) {
return $browser->get($url);
Expand Down Expand Up @@ -434,8 +428,7 @@ could look something like this:
```php
use Clue\React\Block;

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

$promise = Queue::all(3, $urls, function ($url) use ($browser) {
return $browser->get($url);
Expand All @@ -462,8 +455,7 @@ all the async details from the outside:
*/
function download(array $uris)
{
$loop = React\EventLoop\Factory::create();
$browser = new React\Http\Browser($loop);
$browser = new React\Http\Browser();

$promise = Queue::all(3, $uris, function ($uri) use ($browser) {
return $browser->get($uri);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"require-dev": {
"clue/block-react": "^1.0",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/event-loop": "^1.0 || ^0.5",
"react/http": "^1.0"
"react/event-loop": "^1.2",
"react/http": "^1.4"
}
}
4 changes: 1 addition & 3 deletions examples/01-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
'http://www.google.com/',
);

$loop = Factory::create();
$browser = new Browser($loop);
$browser = new Browser();

// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here to avoid using excessive network resources
Expand All @@ -37,4 +36,3 @@ function (Exception $e) use ($url) {
);
}

$loop->run();
4 changes: 1 addition & 3 deletions examples/02-http-all.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
//'http://httpbin.org/delay/2',
);

$loop = Factory::create();
$browser = new Browser($loop);
$browser = new Browser();

// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here to avoid using excessive network resources
Expand All @@ -39,4 +38,3 @@ function ($e) {
}
);

$loop->run();
4 changes: 1 addition & 3 deletions examples/03-http-any.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
'http://www.google.com/invalid',
);

$loop = Factory::create();
$browser = new Browser($loop);
$browser = new Browser();

// each job should use the browser to GET a certain URL
// limit number of concurrent jobs here to avoid using excessive network resources
Expand All @@ -41,4 +40,3 @@ function ($e) {
}
);

$loop->run();
3 changes: 1 addition & 2 deletions examples/11-http-blocking.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

function download(array $urls)
{
$loop = Factory::create();
$browser = new Browser($loop);
$browser = new Browser();

$urls = array_combine($urls, $urls);
$promise = Queue::all(3, $urls, function ($url) use ($browser) {
Expand Down
6 changes: 2 additions & 4 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class Queue implements \Countable
* resolves with the results of all jobs on success.
*
* ```php
* $loop = React\EventLoop\Factory::create();
* $browser = new React\Http\Browser($loop);
* $browser = new React\Http\Browser();
*
* $promise = Queue::all(3, $urls, function ($url) use ($browser) {
* return $browser->get($url);
Expand Down Expand Up @@ -154,8 +153,7 @@ public static function all($concurrency, array $jobs, $handler)
* to `cancel()` all outstanding jobs.
*
* ```php
* $loop = React\EventLoop\Factory::create();
* $browser = new React\Http\Browser($loop);
* $browser = new React\Http\Browser();
*
* $promise = Queue::any(3, $urls, function ($url) use ($browser) {
* return $browser->get($url);
Expand Down