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

Use Promise v3 template types #42

Merged
merged 1 commit into from
Jul 27, 2023
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ is reached, it will not start a new operation and instead queue this for future
execution. Once one of the pending operations complete, it will pick the next
job from the queue and execute this operation.

The `new Queue(int $concurrency, ?int $limit, callable $handler)` call
The `new Queue(int $concurrency, ?int $limit, callable(mixed):PromiseInterface<T> $handler)` call
can be used to create a new queue instance.
You can create any number of queues, for example when you want to apply
different limits to different kinds of operations.
Expand Down Expand Up @@ -275,7 +275,7 @@ for more details.

#### all()

The static `all(int $concurrency, array $jobs, callable $handler): PromiseInterface<mixed[]>` method can be used to
The static `all(int $concurrency, array<TKey,TIn> $jobs, callable(TIn):PromiseInterface<TOut> $handler): PromiseInterface<array<TKey,TOut>>` method can be used to
concurrently process all given jobs through the given `$handler`.

This is a convenience method which uses the `Queue` internally to
Expand Down Expand Up @@ -349,7 +349,7 @@ $promise = Queue::all(10, $jobs, array($browser, 'get'));

#### any()

The static `any(int $concurrency, array $jobs, callable $handler): PromiseInterface<mixed>` method can be used to
The static `any(int $concurrency, array<TKey,TIn> $jobs, callable(TIn):Promise<TOut> $handler): PromiseInterface<TOut>` method can be used to
concurrently process the given jobs through the given `$handler` and
resolve with first resolution value.

Expand Down
33 changes: 21 additions & 12 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* is reached, it will not start a new operation and instead queue this for future
* execution. Once one of the pending operations complete, it will pick the next
* job from the queue and execute this operation.
*
* @template T
*/
class Queue implements \Countable
{
Expand Down Expand Up @@ -100,10 +102,13 @@ class Queue implements \Countable
* > Keep in mind that returning an array of response messages means that
* the whole response body has to be kept in memory.
*
* @param int $concurrency concurrency soft limit
* @param array $jobs
* @param callable $handler
* @return PromiseInterface Returns a Promise<mixed[]> which resolves with an array of all resolution values
* @template TKey
* @template TIn
* @template TOut
* @param int $concurrency concurrency soft limit
* @param array<TKey,TIn> $jobs
* @param callable(TIn):PromiseInterface<TOut> $handler
* @return PromiseInterface<array<TKey,TOut>> Returns a Promise which resolves with an array of all resolution values
* or rejects when any of the operations reject.
*/
public static function all($concurrency, array $jobs, $handler)
Expand Down Expand Up @@ -212,10 +217,13 @@ public static function all($concurrency, array $jobs, $handler)
* $promise = Queue::any(10, $jobs, array($browser, 'get'));
* ```
*
* @param int $concurrency concurrency soft limit
* @param array $jobs
* @param callable $handler
* @return PromiseInterface Returns a Promise<mixed> which resolves with a single resolution value
* @template TKey
* @template TIn
* @template TOut
* @param int $concurrency concurrency soft limit
* @param array<TKey,TIn> $jobs
* @param callable(TIn):PromiseInterface<TOut> $handler
* @return PromiseInterface<TOut> Returns a Promise which resolves with a single resolution value
* or rejects when all of the operations reject.
*/
public static function any($concurrency, array $jobs, $handler)
Expand Down Expand Up @@ -307,9 +315,9 @@ public static function any($concurrency, array $jobs, $handler)
* $q = new Queue(10, null, array($browser, 'get'));
* ```
*
* @param int $concurrency concurrency soft limit
* @param int|null $limit queue hard limit or NULL=unlimited
* @param callable $handler
* @param int $concurrency concurrency soft limit
* @param int|null $limit queue hard limit or NULL=unlimited
* @param callable(mixed):PromiseInterface<T> $handler
* @throws \InvalidArgumentException
*/
public function __construct($concurrency, $limit, $handler)
Expand Down Expand Up @@ -338,7 +346,7 @@ public function __construct($concurrency, $limit, $handler)
* completed. This means that this is handled entirely transparently and you do not
* need to worry about this concurrency limit yourself.
*
* @return \React\Promise\PromiseInterface
* @return PromiseInterface<T>
*/
public function __invoke()
{
Expand Down Expand Up @@ -395,6 +403,7 @@ public function count()

/**
* @internal
* @param PromiseInterface<T> $promise
*/
public function await(PromiseInterface $promise)
{
Expand Down