From ed343153fc3e4c49fb18bc23d071dc56f8e2191a Mon Sep 17 00:00:00 2001 From: Yada Clintjens Date: Mon, 4 Mar 2024 14:07:36 +0100 Subject: [PATCH] Improve documentation and examples --- README.md | 16 +++++++++++++--- examples/01-transform.php | 8 ++------ examples/02-transform-all.php | 10 ++++------ examples/03-transform-any.php | 10 ++++------ 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 7d93e01..99c8bde 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ operations, but keeping thousands of jobs in memory at once may easily take up all resources on your side. Instead, you can use this library to stream your arbitrarily large input list as individual records to a non-blocking (async) transformation handler. It uses -[ReactPHP](https://reactphp.org) to enable you to concurrently process multiple +[ReactPHP](https://reactphp.org/) to enable you to concurrently process multiple records at once. You can control the concurrency limit, so that by allowing it to process 10 operations at the same time, you can thus process this large input list around 10 times faster and at the same time you're no longer limited @@ -72,13 +72,17 @@ 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 +get("https://ipapi.co/$user[ip]/country_name/")->then( - function (ResponseInterface $response) use ($user) { + function (Psr\Http\Message\ResponseInterface $response) use ($user) { // response successfully received // add country to user array and return updated user $user['country'] = (string)$response->getBody(); @@ -411,6 +415,8 @@ $transformer = new Transformer(10, function ($data) use ($http) { }); $source->pipe($gunzip)->pipe($ndjson)->pipe($transformer)->pipe($dest); + +$transformer->on('error', 'printf'); ``` Keep in mind that the transformation handler may return a rejected promise. @@ -456,6 +462,8 @@ $promise = Transformer::all($input, 3, function ($data) use ($browser, $url) { $promise->then(function ($count) { echo 'All ' . $count . ' jobs successful!' . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -561,6 +569,8 @@ $promise = Transformer::any($input, 3, function ($data) use ($browser, $url) { $promise->then(function (ResponseInterface $response) { echo 'First successful job: ' . $response->getBody() . PHP_EOL; +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` diff --git a/examples/01-transform.php b/examples/01-transform.php index 1d50b0b..6b3adb7 100644 --- a/examples/01-transform.php +++ b/examples/01-transform.php @@ -1,8 +1,5 @@ get("https://ipapi.co/$user[ip]/country_name/")->then( - function (ResponseInterface $response) use ($user) { + function (Psr\Http\Message\ResponseInterface $response) use ($user) { // response successfully received // add country to user array and return updated user $user['country'] = (string)$response->getBody(); @@ -50,4 +47,3 @@ function (ResponseInterface $response) use ($user) { echo '[DONE]' . PHP_EOL; }); $transformer->on('error', 'printf'); - diff --git a/examples/02-transform-all.php b/examples/02-transform-all.php index c62f21e..c69370f 100644 --- a/examples/02-transform-all.php +++ b/examples/02-transform-all.php @@ -1,8 +1,5 @@ post( $url, array('Content-Type' => 'application/json'), json_encode($user) - )->then(function (ResponseInterface $response) { + )->then(function (Psr\Http\Message\ResponseInterface $response) { // demo HTTP response validation $body = json_decode($response->getBody()); if (!isset($body->json)) { throw new RuntimeException('Unexpected response'); } + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); }); @@ -46,4 +45,3 @@ function (Exception $e) { } } ); - diff --git a/examples/03-transform-any.php b/examples/03-transform-any.php index a1e5134..0523c9a 100644 --- a/examples/03-transform-any.php +++ b/examples/03-transform-any.php @@ -1,8 +1,5 @@ post( $url, array('Content-Type' => 'application/json'), json_encode($user) - )->then(function (ResponseInterface $response) use ($user) { + )->then(function (Psr\Http\Message\ResponseInterface $response) use ($user) { // demo HTTP response validation $body = json_decode($response->getBody()); if (!isset($body->json)) { @@ -36,6 +33,8 @@ // demo result includes full user from NDJSON with additional properties $user['result'] = $body; return $user; + }, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); }); @@ -50,4 +49,3 @@ function (Exception $e) { } } ); -