Skip to content

Commit

Permalink
Forward compatibility with react/promise 3
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Apr 27, 2020
1 parent fda8533 commit fb261f8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require": {
"php": ">=5.3",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/promise": "^2.7.0 || ^1.2.1"
"react/promise": "^3.0 || ^2.7.0 || ^1.2.1"
},
"require-dev": {
"phpunit/phpunit": "^7.0 || ^6.4 || ^5.7 || ^4.8.35"
Expand Down
6 changes: 3 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace React\Promise\Timer;

use React\Promise\CancellablePromiseInterface;
use React\EventLoop\LoopInterface;
use React\Promise\CancellablePromiseInterface;
use React\Promise\PromiseInterface;
use React\Promise\Promise;

Expand All @@ -12,7 +12,7 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop)
// cancelling this promise will only try to cancel the input promise,
// thus leaving responsibility to the input promise.
$canceller = null;
if ($promise instanceof CancellablePromiseInterface) {
if ($promise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) {
// pass promise by reference to clean reference after cancellation handler
// has been invoked once in order to avoid garbage references in call stack.
$canceller = function () use (&$promise) {
Expand Down Expand Up @@ -48,7 +48,7 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop)

// try to invoke cancellation handler of input promise and then clean
// reference in order to avoid garbage references in call stack.
if ($promise instanceof CancellablePromiseInterface) {
if ($promise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) {
$promise->cancel();
}
$promise = null;
Expand Down
10 changes: 6 additions & 4 deletions tests/FunctionTimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testResolvedWillNotStartTimer()

public function testRejectedWillRejectRightAway()
{
$promise = Promise\reject();
$promise = Promise\reject(new \Exception('reject'));

$promise = Timer\timeout($promise, 3, $this->loop);

Expand All @@ -49,7 +49,7 @@ public function testRejectedWillRejectRightAway()

public function testRejectedWillNotStartTimer()
{
$promise = Promise\reject();
$promise = Promise\reject(new \Exception('reject'));

Timer\timeout($promise, 3, $this->loop);

Expand All @@ -73,10 +73,12 @@ public function testPendingWillRejectOnTimeout()

public function testPendingCancellableWillBeCancelledThroughFollowerOnTimeout()
{
$cancellable = $this->getMockBuilder('React\Promise\CancellablePromiseInterface')->getMock();
$cancellableInterface = interface_exists('React\Promise\CancellablePromiseInterface') ?
'React\Promise\CancellablePromiseInterface' : 'React\Promise\PromiseInterface';
$cancellable = $this->getMockBuilder($cancellableInterface)->getMock();
$cancellable->expects($this->once())->method('cancel');

$promise = $this->getMockBuilder('React\Promise\CancellablePromiseInterface')->getMock();
$promise = $this->getMockBuilder($cancellableInterface)->getMock();
$promise->expects($this->once())->method('then')->willReturn($cancellable);

Timer\timeout($promise, 0.01, $this->loop);
Expand Down

0 comments on commit fb261f8

Please sign in to comment.