-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(async): introduce more async helper functions
Signed-off-by: azjezz <azjezz@protonmail.com>
- Loading branch information
Showing
27 changed files
with
459 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Async; | ||
|
||
use Psl\Dict; | ||
|
||
/** | ||
* Run the tasks iterable of functions in parallel, without waiting until the previous function has completed. | ||
* | ||
* If one tasks fails, the exception will be thrown immediately, and the result of the callables will be ignored. | ||
* | ||
* If multiple tasks failed at once, a {@see Exception\CompositeException} will be thrown. | ||
* | ||
* Once the tasks have completed, an array containing the results will be returned preserving the original awaitables order. | ||
* | ||
* <code> | ||
* use Psl\Async; | ||
* | ||
* // execute `getOne()` and `getTwo()` functions in parallel. | ||
* | ||
* [$one, $two] = Async\parallel([ | ||
* getOne(...), | ||
* getTwo(...), | ||
* ]); | ||
* </code> | ||
* | ||
* @note {@see parallel()} is about kicking-off I/O tasks in parallel, not about parallel execution of code. | ||
* If your tasks do not use any timers or perform any I/O, they will actually be executed in series. | ||
* | ||
* <code> | ||
* use Psl\Async; | ||
* | ||
* // the following runs in series. | ||
* | ||
* [$one, $two] = Async\parallel([ | ||
* fn() => file_get_contents('path/to/file1.txt'), | ||
* fn() => file_get_contents('path/to/file2.txt'), | ||
* ]); | ||
* </code> | ||
* | ||
* @note Use {@see reflect()} to continue the execution of other tasks when a task fails. | ||
* | ||
* <code> | ||
* use Psl\Async; | ||
* | ||
* // execute `getOne()` and `getTwo()` functions in parallel. | ||
* // if either one of the given tasks fails, the other will continue execution. | ||
* | ||
* [$one, $two] = Async\parallel([ | ||
* Async\reflect(getOne(...)), | ||
* Async\reflect(getTwo(...)), | ||
* ]); | ||
* </code> | ||
* | ||
* @template Tk of array-key | ||
* @template Tv | ||
* | ||
* @param iterable<Tk, (callable(): Tv)> $tasks | ||
* | ||
* @return array<Tk, Tv> | ||
*/ | ||
function parallel(iterable $tasks): array | ||
{ | ||
$awaitables = Dict\map( | ||
$tasks, | ||
/** | ||
* @param callable(): Tv $callable | ||
* | ||
* @return Awaitable<Tv> | ||
*/ | ||
static fn(callable $callable): Awaitable => run($callable), | ||
); | ||
|
||
return namespace\all($awaitables); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Async; | ||
|
||
use Closure; | ||
use Exception; | ||
use Psl\Result; | ||
|
||
/** | ||
* Wraps the given task in another task that always completes with a {@see Result\Success}, | ||
* or {@see Result\Failure} if the callable throws an {@see Exception}. | ||
* | ||
* @template T | ||
* | ||
* @param (callable(): T) $task | ||
* | ||
* @return (Closure(): Result\ResultInterface<T>) | ||
* | ||
* @see Result\wrap() | ||
* | ||
* @pure | ||
*/ | ||
function reflect(callable $task): Closure | ||
{ | ||
return static fn() => Result\wrap($task); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Async; | ||
|
||
use Psl\Dict; | ||
|
||
/** | ||
* Run the functions in the tasks collection in series, each one running once the previous function has completed. | ||
* | ||
* If any functions in the series throws, no more functions are run, and the exception is immediately thrown. | ||
* | ||
* @template Tk of array-key | ||
* @template Tv | ||
* | ||
* @param iterable<Tk, (callable(): Tv)> $tasks | ||
* | ||
* @return array<Tk, Tv> | ||
*/ | ||
function series(iterable $tasks): array | ||
{ | ||
return Dict\map( | ||
$tasks, | ||
/** | ||
* @param callable(): Tv $callable | ||
* | ||
* @return Tv | ||
*/ | ||
static fn(callable $callable): mixed => run($callable)->await(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Async; | ||
|
||
use Psl\Result; | ||
|
||
/** | ||
* Wraps the async function in an awaitable that completes with a {@see Result\Success}, | ||
* or {@see Result\Failure} if the task throws an {@see Exception}. | ||
* | ||
* @template T | ||
* | ||
* @param (callable(): T) $task | ||
* | ||
* @return Result\ResultInterface<T> | ||
* | ||
* @see reflect() | ||
*/ | ||
function wrap(callable $task): Result\ResultInterface | ||
{ | ||
return run(reflect($task))->await(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.