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

Update to use new reactphp/async package instead of clue/reactphp-block #34

Merged
merged 1 commit into from
Sep 30, 2022
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
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,13 @@ $promise = Queue::any(10, $jobs, array($browser, 'get'));
#### Blocking

As stated above, this library provides you a powerful, async API by default.
If, however, you want to integrate this into your traditional, blocking
environment, you may want to look into also using
[clue/reactphp-block](https://github.com/clue/reactphp-block).

The resulting blocking code that awaits a number of concurrent HTTP requests
could look something like this:
You can also integrate this into your traditional, blocking environment by using
[reactphp/async](https://github.com/reactphp/async). This allows you to simply
await async HTTP requests like this:

```php
use Clue\React\Block;
use function React\Async\await;

$browser = new React\Http\Browser();

Expand All @@ -439,7 +437,7 @@ $promise = Queue::all(3, $urls, function ($url) use ($browser) {
});

try {
$responses = Block\await($promise, $loop);
$responses = await($promise);
// responses successfully received
} catch (Exception $e) {
// an error occured while performing the requests
Expand All @@ -450,6 +448,8 @@ Similarly, you can also wrap this in a function to provide a simple API and hide
all the async details from the outside:

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

/**
* Concurrently downloads all the given URIs
*
Expand All @@ -465,12 +465,13 @@ function download(array $uris)
return $browser->get($uri);
});

return Clue\React\Block\await($promise, $loop);
return await($promise);
}
```

Please refer to [clue/reactphp-block](https://github.com/clue/reactphp-block#readme)
for more details.
This is made possible thanks to fibers available in PHP 8.1+ and our
compatibility API that also works on all supported PHP versions.
Please refer to [reactphp/async](https://github.com/reactphp/async#readme) for more details.

> Keep in mind that returning an array of response messages means that the whole
response body has to be kept in memory.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"react/promise": "^3 || ^2.2.1 || ^1.2.1"
},
"require-dev": {
"clue/block-react": "^1.5",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react/async": "^4 || ^3 || ^2",
"react/event-loop": "^1.2",
"react/http": "^1.8"
}
Expand Down
5 changes: 1 addition & 4 deletions examples/11-http-blocking.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

use Clue\React\Block;
use React\EventLoop\Loop;

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

// list of all URLs you want to download
Expand Down Expand Up @@ -33,7 +30,7 @@ function (Exception $e) {
);
});

return Block\await($promise, Loop::get());
return React\Async\await($promise);
}

$responses = download($urls);
Expand Down