Skip to content

Commit b21bd4c

Browse files
authored
Merge pull request #29 from PaulRotmann/noloop
Simplify examples by updating to new default loop
2 parents 82b7f5d + a3f623c commit b21bd4c

File tree

7 files changed

+19
-36
lines changed

7 files changed

+19
-36
lines changed

README.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ Once [installed](#install), you can use the following code to access an
6565
HTTP webserver and send a large number of HTTP GET requests:
6666

6767
```php
68-
$loop = React\EventLoop\Factory::create();
69-
$browser = new React\Http\Browser($loop);
68+
$browser = new React\Http\Browser();
7069

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

86-
$loop->run();
8785
```
8886

8987
See also the [examples](examples).
@@ -162,8 +160,7 @@ The demonstration purposes, the examples in this documentation use
162160
may use any Promise-based API with this project. Its API can be used like this:
163161

164162
```php
165-
$loop = React\EventLoop\Factory::create();
166-
$browser = new React\Http\Browser($loop);
163+
$browser = new React\Http\Browser();
167164

168165
$promise = $browser->get($url);
169166
```
@@ -172,8 +169,7 @@ If you wrap this in a `Queue` instance as given above, this code will look
172169
like this:
173170

174171
```php
175-
$loop = React\EventLoop\Factory::create();
176-
$browser = new React\Http\Browser($loop);
172+
$browser = new React\Http\Browser();
177173

178174
$q = new Queue(10, null, function ($url) use ($browser) {
179175
return $browser->get($url);
@@ -226,7 +222,7 @@ underlying resources.
226222
```php
227223
$promise = $q($url);
228224

229-
$loop->addTimer(2.0, function () use ($promise) {
225+
Loop::addTimer(2.0, function () use ($promise) {
230226
$promise->cancel();
231227
});
232228
```
@@ -250,8 +246,8 @@ The resulting code with timeouts applied look something like this:
250246
```php
251247
use React\Promise\Timer;
252248

253-
$q = new Queue(10, null, function ($uri) use ($browser, $loop) {
254-
return Timer\timeout($browser->get($uri), 2.0, $loop);
249+
$q = new Queue(10, null, function ($uri) use ($browser) {
250+
return Timer\timeout($browser->get($uri), 2.0);
255251
});
256252

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

267263
```php
268264
// usually not recommended
269-
$promise = Timer\timeout($q($url), 2.0, $loop);
265+
$promise = Timer\timeout($q($url), 2.0);
270266
```
271267

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

285281
```php
286-
$loop = React\EventLoop\Factory::create();
287-
$browser = new React\Http\Browser($loop);
282+
$browser = new React\Http\Browser();
288283

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

362357
```php
363-
$loop = React\EventLoop\Factory::create();
364-
$browser = new React\Http\Browser($loop);
358+
$browser = new React\Http\Browser();
365359

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

437-
$loop = React\EventLoop\Factory::create();
438-
$browser = new React\Http\Browser($loop);
431+
$browser = new React\Http\Browser();
439432

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

468460
$promise = Queue::all(3, $uris, function ($uri) use ($browser) {
469461
return $browser->get($uri);

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require-dev": {
2424
"clue/block-react": "^1.0",
2525
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
26-
"react/event-loop": "^1.0 || ^0.5",
27-
"react/http": "^1.0"
26+
"react/event-loop": "^1.2",
27+
"react/http": "^1.4"
2828
}
2929
}

examples/01-http.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
'http://www.google.com/',
1818
);
1919

20-
$loop = Factory::create();
21-
$browser = new Browser($loop);
20+
$browser = new Browser();
2221

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

40-
$loop->run();

examples/02-http-all.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
//'http://httpbin.org/delay/2',
1818
);
1919

20-
$loop = Factory::create();
21-
$browser = new Browser($loop);
20+
$browser = new Browser();
2221

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

42-
$loop->run();

examples/03-http-any.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
'http://www.google.com/invalid',
1919
);
2020

21-
$loop = Factory::create();
22-
$browser = new Browser($loop);
21+
$browser = new Browser();
2322

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

44-
$loop->run();

examples/11-http-blocking.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
function download(array $urls)
2222
{
23-
$loop = Factory::create();
24-
$browser = new Browser($loop);
23+
$browser = new Browser();
2524

2625
$urls = array_combine($urls, $urls);
2726
$promise = Queue::all(3, $urls, function ($url) use ($browser) {

src/Queue.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class Queue implements \Countable
3838
* resolves with the results of all jobs on success.
3939
*
4040
* ```php
41-
* $loop = React\EventLoop\Factory::create();
42-
* $browser = new React\Http\Browser($loop);
41+
* $browser = new React\Http\Browser();
4342
*
4443
* $promise = Queue::all(3, $urls, function ($url) use ($browser) {
4544
* return $browser->get($url);
@@ -154,8 +153,7 @@ public static function all($concurrency, array $jobs, $handler)
154153
* to `cancel()` all outstanding jobs.
155154
*
156155
* ```php
157-
* $loop = React\EventLoop\Factory::create();
158-
* $browser = new React\Http\Browser($loop);
156+
* $browser = new React\Http\Browser();
159157
*
160158
* $promise = Queue::any(3, $urls, function ($url) use ($browser) {
161159
* return $browser->get($url);

0 commit comments

Comments
 (0)