diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e031c21..b5133eaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,29 @@ CHANGELOG for 2.x ================= +* 2.5.0 (xxxx-xx-xx) + + * Revert automatic cancellation of pending collection promises once the + output promise resolves. This was introduced in 42d86b7 (PR #36, released + in [v2.3.0](https://github.com/reactphp/promise/releases/tag/v2.3.0)) and + was both unintended and backward incompatible. + + If you need automatic cancellation, you can use something like: + + ```php + function allAndCancel(array $promises) + { + return \React\Promise\all($promises) + ->always(function() use ($promises) { + foreach ($promises as $promise) { + if ($promise instanceof \React\Promise\CancellablePromiseInterface) { + $promise->cancel(); + } + } + }); + } + ``` + * 2.4.1 (2016-05-03) * Fix `some()` not cancelling pending promises when too much input promises diff --git a/src/functions.php b/src/functions.php index 5f55f2ea..4f49b011 100644 --- a/src/functions.php +++ b/src/functions.php @@ -54,21 +54,11 @@ function race($promisesOrValues) return; } - $fulfiller = function ($value) use ($cancellationQueue, $resolve) { - $cancellationQueue(); - $resolve($value); - }; - - $rejecter = function ($reason) use ($cancellationQueue, $reject) { - $cancellationQueue(); - $reject($reason); - }; - foreach ($array as $promiseOrValue) { $cancellationQueue->enqueue($promiseOrValue); resolve($promiseOrValue) - ->done($fulfiller, $rejecter, $notify); + ->done($resolve, $reject, $notify); } }, $reject, $notify); }, $cancellationQueue); @@ -115,7 +105,7 @@ function some($promisesOrValues, $howMany) $reasons = []; foreach ($array as $i => $promiseOrValue) { - $fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve, $cancellationQueue) { + $fulfiller = function ($val) use ($i, &$values, &$toResolve, $toReject, $resolve) { if ($toResolve < 1 || $toReject < 1) { return; } @@ -123,12 +113,11 @@ function some($promisesOrValues, $howMany) $values[$i] = $val; if (0 === --$toResolve) { - $cancellationQueue(); $resolve($values); } }; - $rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject, $cancellationQueue) { + $rejecter = function ($reason) use ($i, &$reasons, &$toReject, $toResolve, $reject) { if ($toResolve < 1 || $toReject < 1) { return; } @@ -136,7 +125,6 @@ function some($promisesOrValues, $howMany) $reasons[$i] = $reason; if (0 === --$toReject) { - $cancellationQueue(); $reject($reasons); } }; diff --git a/tests/FunctionAnyTest.php b/tests/FunctionAnyTest.php index 6c40674b..811473e0 100644 --- a/tests/FunctionAnyTest.php +++ b/tests/FunctionAnyTest.php @@ -175,7 +175,7 @@ public function shouldCancelInputArrayPromises() } /** @test */ - public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() + public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() { $mock = $this->createCallableMock(); $mock @@ -188,7 +188,7 @@ public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() $mock2 = $this->getMock('React\Promise\CancellablePromiseInterface'); $mock2 - ->expects($this->once()) + ->expects($this->never()) ->method('cancel'); some([$deferred->promise(), $mock2], 1)->cancel(); diff --git a/tests/FunctionRaceTest.php b/tests/FunctionRaceTest.php index 6cd1e987..7fd8acbe 100644 --- a/tests/FunctionRaceTest.php +++ b/tests/FunctionRaceTest.php @@ -162,7 +162,7 @@ public function shouldCancelInputArrayPromises() } /** @test */ - public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() + public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() { $mock = $this->createCallableMock(); $mock @@ -174,14 +174,14 @@ public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills() $mock2 = $this->getMock('React\Promise\CancellablePromiseInterface'); $mock2 - ->expects($this->once()) + ->expects($this->never()) ->method('cancel'); race([$deferred->promise(), $mock2])->cancel(); } /** @test */ - public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseRejects() + public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseRejects() { $mock = $this->createCallableMock(); $mock @@ -193,7 +193,7 @@ public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseRejects() $mock2 = $this->getMock('React\Promise\CancellablePromiseInterface'); $mock2 - ->expects($this->once()) + ->expects($this->never()) ->method('cancel'); race([$deferred->promise(), $mock2])->cancel(); diff --git a/tests/FunctionSomeTest.php b/tests/FunctionSomeTest.php index 374c84f0..4808e20e 100644 --- a/tests/FunctionSomeTest.php +++ b/tests/FunctionSomeTest.php @@ -209,7 +209,7 @@ public function shouldCancelInputArrayPromises() } /** @test */ - public function shouldCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfill() + public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfill() { $mock = $this->createCallableMock(); $mock @@ -221,14 +221,14 @@ public function shouldCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfil $mock2 = $this->getMock('React\Promise\CancellablePromiseInterface'); $mock2 - ->expects($this->once()) + ->expects($this->never()) ->method('cancel'); some([$deferred->promise(), $mock2], 1); } /** @test */ - public function shouldCancelOtherPendingInputArrayPromisesIfEnoughPromisesReject() + public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesReject() { $mock = $this->createCallableMock(); $mock @@ -240,7 +240,7 @@ public function shouldCancelOtherPendingInputArrayPromisesIfEnoughPromisesReject $mock2 = $this->getMock('React\Promise\CancellablePromiseInterface'); $mock2 - ->expects($this->once()) + ->expects($this->never()) ->method('cancel'); some([$deferred->promise(), $mock2], 2);