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

Enforce always passing promise to race function #207

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
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
17 changes: 6 additions & 11 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,17 @@ function all(array $promisesOrValues): PromiseInterface
* Initiates a competitive race that allows one winner. Returns a promise which is
* resolved in the same way the first settled promise resolves.
*
* The returned promise will become **infinitely pending** if `$promisesOrValues`
* contains 0 items.
*
* @param array $promisesOrValues
* @param PromiseInterface $promise
* @param array<PromiseInterface> $promises
* @return PromiseInterface
*/
function race(array $promisesOrValues): PromiseInterface
function race(PromiseInterface $promise, PromiseInterface ...$promises): PromiseInterface
{
if (!$promisesOrValues) {
return new Promise(function (): void {});
}

$promises[] = $promise;
$cancellationQueue = new Internal\CancellationQueue();

return new Promise(function ($resolve, $reject) use ($promisesOrValues, $cancellationQueue): void {
foreach ($promisesOrValues as $promiseOrValue) {
return new Promise(function ($resolve, $reject) use ($promises, $cancellationQueue): void {
foreach ($promises as $promiseOrValue) {
$cancellationQueue->enqueue($promiseOrValue);

resolve($promiseOrValue)
Copy link
Member

@jsor jsor Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resolve($promiseOrValue)
$promiseOrValue

Passing throught resolve can be removed as the function signature now declares PromiseInterface.

Expand Down
46 changes: 5 additions & 41 deletions tests/FunctionRaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@

class FunctionRaceTest extends TestCase
{
/** @test */
public function shouldReturnForeverPendingPromiseForEmptyInput()
{
race(
[]
)->then($this->expectCallableNever(), $this->expectCallableNever());
}

/** @test */
public function shouldResolveValuesArray()
{
$mock = $this->createCallableMock();
$mock
->expects(self::once())
->method('__invoke')
->with(self::identicalTo(1));

race(
[1, 2, 3]
)->then($mock);
}

/** @test */
public function shouldResolvePromisesArray()
{
Expand All @@ -42,7 +20,7 @@ public function shouldResolvePromisesArray()
$d3 = new Deferred();

race(
[$d1->promise(), $d2->promise(), $d3->promise()]
$d1->promise(), $d2->promise(), $d3->promise()
)->then($mock);

$d2->resolve(2);
Expand All @@ -51,20 +29,6 @@ public function shouldResolvePromisesArray()
$d3->resolve(3);
}

/** @test */
public function shouldResolveSparseArrayInput()
{
$mock = $this->createCallableMock();
$mock
->expects(self::once())
->method('__invoke')
->with(self::identicalTo(null));

race(
[null, 1, null, 2, 3]
)->then($mock);
}

/** @test */
public function shouldRejectIfFirstSettledPromiseRejects()
{
Expand All @@ -81,7 +45,7 @@ public function shouldRejectIfFirstSettledPromiseRejects()
$d3 = new Deferred();

race(
[$d1->promise(), $d2->promise(), $d3->promise()]
$d1->promise(), $d2->promise(), $d3->promise()
)->then($this->expectCallableNever(), $mock);

$d2->reject($exception);
Expand All @@ -96,7 +60,7 @@ public function shouldCancelInputArrayPromises()
$promise1 = new Promise(function () {}, $this->expectCallableOnce());
$promise2 = new Promise(function () {}, $this->expectCallableOnce());

race([$promise1, $promise2])->cancel();
race($promise1, $promise2)->cancel();
}

/** @test */
Expand All @@ -107,7 +71,7 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfill

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

race([$deferred->promise(), $promise2])->cancel();
race($deferred->promise(), $promise2)->cancel();
}

/** @test */
Expand All @@ -118,6 +82,6 @@ public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects

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

race([$deferred->promise(), $promise2])->cancel();
race($deferred->promise(), $promise2)->cancel();
}
}