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

[RFC] Remove some(), map(), reduce() functions #219

Merged
merged 3 commits into from
Jun 1, 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
51 changes: 2 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ Table of Contents
* [all()](#all)
* [race()](#race)
* [any()](#any)
* [some()](#some)
* [map()](#map)
* [reduce()](#reduce)
4. [Examples](#examples)
* [How to use Deferred](#how-to-use-deferred)
* [How promise forwarding works](#how-promise-forwarding-works)
Expand Down Expand Up @@ -365,10 +362,9 @@ once all consumers called the `cancel()` method of the promise.

### Functions

Useful functions for creating, joining, mapping and reducing collections of
promises.
Useful functions for creating and joining collections of promises.

All functions working on promise collections (like `all()`, `race()`, `some()`
All functions working on promise collections (like `all()`, `race()`,
etc.) support cancellation. This means, if you call `cancel()` on the returned
promise, all promises in the collection are cancelled.

Expand Down Expand Up @@ -442,49 +438,6 @@ which holds all rejection reasons. The rejection reasons can be obtained with
The returned promise will also reject with a `React\Promise\Exception\LengthException`
if `$promisesOrValues` contains 0 items.

#### some()

```php
$promise = React\Promise\some(array $promisesOrValues, integer $howMany);
```

Returns a promise that will resolve when at least `$howMany` of the supplied items in
`$promisesOrValues` fulfill. The resolution value of the returned promise
will be an array of length `$howMany` containing the resolution values of
`$howMany` fulfilled promises that were resolved first.

The returned promise will reject if it becomes impossible for `$howMany` items
to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
reject). The rejection value will be a `React\Promise\Exception\CompositeException`
which holds `(count($promisesOrValues) - $howMany) + 1` rejection reasons.
The rejection reasons can be obtained with `CompositeException::getExceptions()`.

The returned promise will also reject with a `React\Promise\Exception\LengthException`
if `$promisesOrValues` contains less items than `$howMany`.

#### map()

```php
$promise = React\Promise\map(array $promisesOrValues, callable $mapFunc);
```

Traditional map function, similar to `array_map()`, but allows input to contain
promises and/or values, and `$mapFunc` may return either a value or a promise.

The map function receives each item as argument, where item is a fully resolved
value of a promise or value in `$promisesOrValues`.

#### reduce()

```php
$promise = React\Promise\reduce(array $promisesOrValues, callable $reduceFunc, $initialValue = null);
```

Traditional reduce function, similar to `array_reduce()`, but input may contain
promises and/or values, and `$reduceFunc` may return either a value or a
promise, *and* `$initialValue` may be a promise or a value for the starting
value.

Examples
--------

Expand Down
173 changes: 35 additions & 138 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,33 @@ function reject(\Throwable $reason): PromiseInterface
*/
function all(array $promisesOrValues): PromiseInterface
{
return map($promisesOrValues, function ($val) {
return $val;
});
if (!$promisesOrValues) {
return resolve([]);
}

$cancellationQueue = new Internal\CancellationQueue();

return new Promise(function ($resolve, $reject) use ($promisesOrValues, $cancellationQueue): void {
$toResolve = \count($promisesOrValues);
$values = [];

foreach ($promisesOrValues as $i => $promiseOrValue) {
$cancellationQueue->enqueue($promiseOrValue);
$values[$i] = null;

resolve($promiseOrValue)
->done(
function ($mapped) use ($i, &$values, &$toResolve, $resolve): void {
$values[$i] = $mapped;

if (0 === --$toResolve) {
$resolve($values);
}
},
$reject
);
}
}, $cancellationQueue);
}

/**
Expand Down Expand Up @@ -122,45 +146,13 @@ function race(array $promisesOrValues): PromiseInterface
*/
function any(array $promisesOrValues): PromiseInterface
{
return some($promisesOrValues, 1)
->then(function ($val) {
return \array_shift($val);
});
}

/**
* Returns a promise that will resolve when `$howMany` of the supplied items in
* `$promisesOrValues` resolve. The resolution value of the returned promise
* will be an array of length `$howMany` containing the resolution values of the
* triggering items.
*
* The returned promise will reject if it becomes impossible for `$howMany` items
* to resolve (that is, when `(count($promisesOrValues) - $howMany) + 1` items
* reject). The rejection value will be an array of
* `(count($promisesOrValues) - $howMany) + 1` rejection reasons.
*
* The returned promise will also reject with a `React\Promise\Exception\LengthException`
* if `$promisesOrValues` contains less items than `$howMany`.
*
* @param array $promisesOrValues
* @param int $howMany
* @return PromiseInterface
*/
function some(array $promisesOrValues, int $howMany): PromiseInterface
{
if ($howMany < 1) {
return resolve([]);
}

$len = \count($promisesOrValues);

if ($len < $howMany) {
if (!$promisesOrValues) {
return reject(
new Exception\LengthException(
\sprintf(
'Input array must contain at least %d item%s but contains only %s item%s.',
$howMany,
1 === $howMany ? '' : 's',
'Input array must contain at least 1 item but contains only %s item%s.',
$len,
1 === $len ? '' : 's'
)
Expand All @@ -170,37 +162,23 @@ function some(array $promisesOrValues, int $howMany): PromiseInterface

$cancellationQueue = new Internal\CancellationQueue();

return new Promise(function ($resolve, $reject) use ($len, $promisesOrValues, $howMany, $cancellationQueue): void {
$toResolve = $howMany;
$toReject = ($len - $toResolve) + 1;
$values = [];
return new Promise(function ($resolve, $reject) use ($len, $promisesOrValues, $cancellationQueue): void {
$toReject = $len;
$reasons = [];

foreach ($promisesOrValues as $i => $promiseOrValue) {
$fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve): void {
if ($toResolve < 1 || $toReject < 1) {
return;
}

$values[$i] = $val;

if (0 === --$toResolve) {
$resolve($values);
}
$fulfiller = function ($val) use ($resolve): void {
$resolve($val);
};

$rejecter = function (\Throwable $reason) use ($i, &$reasons, &$toReject, $toResolve, $reject): void {
if ($toResolve < 1 || $toReject < 1) {
return;
}

$rejecter = function (\Throwable $reason) use ($i, &$reasons, &$toReject, $reject): void {
$reasons[$i] = $reason;

if (0 === --$toReject) {
$reject(
new CompositeException(
$reasons,
'Too many promises rejected.'
'All promises rejected.'
)
);
}
Expand All @@ -214,87 +192,6 @@ function some(array $promisesOrValues, int $howMany): PromiseInterface
}, $cancellationQueue);
}

/**
* Traditional map function, similar to `array_map()`, but allows input to contain
* promises and/or values, and `$mapFunc` may return either a value or a promise.
*
* The map function receives each item as argument, where item is a fully resolved
* value of a promise or value in `$promisesOrValues`.
*
* @param array $promisesOrValues
* @param callable $mapFunc
* @return PromiseInterface
*/
function map(array $promisesOrValues, callable $mapFunc): PromiseInterface
{
if (!$promisesOrValues) {
return resolve([]);
}

$cancellationQueue = new Internal\CancellationQueue();

return new Promise(function ($resolve, $reject) use ($promisesOrValues, $mapFunc, $cancellationQueue): void {
$toResolve = \count($promisesOrValues);
$values = [];

foreach ($promisesOrValues as $i => $promiseOrValue) {
$cancellationQueue->enqueue($promiseOrValue);
$values[$i] = null;

resolve($promiseOrValue)
->then($mapFunc)
->done(
function ($mapped) use ($i, &$values, &$toResolve, $resolve): void {
$values[$i] = $mapped;

if (0 === --$toResolve) {
$resolve($values);
}
},
$reject
);
}
}, $cancellationQueue);
}

/**
* Traditional reduce function, similar to `array_reduce()`, but input may contain
* promises and/or values, and `$reduceFunc` may return either a value or a
* promise, *and* `$initialValue` may be a promise or a value for the starting
* value.
*
* @param array $promisesOrValues
* @param callable $reduceFunc
* @param mixed $initialValue
* @return PromiseInterface
*/
function reduce(array $promisesOrValues, callable $reduceFunc, $initialValue = null): PromiseInterface
{
$cancellationQueue = new Internal\CancellationQueue();

return new Promise(function ($resolve, $reject) use ($promisesOrValues, $reduceFunc, $initialValue, $cancellationQueue): void {
$total = \count($promisesOrValues);
$i = 0;

$wrappedReduceFunc = function ($current, $val) use ($reduceFunc, $cancellationQueue, $total, &$i): PromiseInterface {
$cancellationQueue->enqueue($val);

return $current
->then(function ($c) use ($reduceFunc, $total, &$i, $val) {
return resolve($val)
->then(function ($value) use ($reduceFunc, $total, &$i, $c) {
return $reduceFunc($c, $value, $i++, $total);
});
});
};

$cancellationQueue->enqueue($initialValue);

\array_reduce($promisesOrValues, $wrappedReduceFunc, resolve($initialValue))
->done($resolve, $reject);
}, $cancellationQueue);
}

/**
* @internal
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/FunctionAnyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function shouldRejectWithAllRejectedInputValuesIfAllInputsAreRejected()

$compositeException = new CompositeException(
[0 => $exception1, 1 => $exception2, 2 => $exception3],
'Too many promises rejected.'
'All promises rejected.'
);

$mock = $this->createCallableMock();
Expand Down Expand Up @@ -126,6 +126,6 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfill

$promise2 = new Promise(function () {}, $this->expectCallableNever());

some([$deferred->promise(), $promise2], 1)->cancel();
any([$deferred->promise(), $promise2], 1)->cancel();
}
}
Loading