Skip to content
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
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 3 additions & 15 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -115,28 +105,26 @@ 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;
}

$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;
}

$reasons[$i] = $reason;

if (0 === --$toReject) {
$cancellationQueue();
$reject($reasons);
}
};
Expand Down
4 changes: 2 additions & 2 deletions tests/FunctionAnyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function shouldCancelInputArrayPromises()
}

/** @test */
public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
{
$mock = $this->createCallableMock();
$mock
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tests/FunctionRaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function shouldCancelInputArrayPromises()
}

/** @test */
public function shouldCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
public function shouldNotCancelOtherPendingInputArrayPromisesIfOnePromiseFulfills()
{
$mock = $this->createCallableMock();
$mock
Expand All @@ -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
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tests/FunctionSomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function shouldCancelInputArrayPromises()
}

/** @test */
public function shouldCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfill()
public function shouldNotCancelOtherPendingInputArrayPromisesIfEnoughPromisesFulfill()
{
$mock = $this->createCallableMock();
$mock
Expand All @@ -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
Expand All @@ -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);
Expand Down