Skip to content

Commit

Permalink
Optimize Promise v3 API compatibility to avoid hitting autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Mar 7, 2022
1 parent ca15683 commit 92e37e0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

Expand Down Expand Up @@ -144,7 +143,7 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)
// cancelling this promise will only try to cancel the input promise,
// thus leaving responsibility to the input promise.
$canceller = null;
if ($promise instanceof CancellablePromiseInterface || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) {
if (\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 @@ -184,7 +183,7 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop = null)

// 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 || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) {
if (\method_exists($promise, 'cancel')) {
$promise->cancel();
}
$promise = null;
Expand Down
1 change: 1 addition & 0 deletions tests/FunctionTimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testRejectedWillNotStartTimer()
public function testPendingWillRejectOnTimeout()
{
$promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
$promise->expects($this->once())->method('then')->willReturn($this->getMockBuilder('React\Promise\PromiseInterface')->getMock());

$promise = Timer\timeout($promise, 0.01);

Expand Down

0 comments on commit 92e37e0

Please sign in to comment.