From 02c3121e57674b5786eee068b41affe4e61a822a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 6 Mar 2022 17:24:03 +0100 Subject: [PATCH] Optimize Promise v3 API compatibility to avoid hitting autoloader --- src/functions.php | 5 ++--- tests/FunctionTimeoutTest.php | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/functions.php b/src/functions.php index 474667d..a8b1fd5 100644 --- a/src/functions.php +++ b/src/functions.php @@ -4,7 +4,6 @@ use React\EventLoop\Loop; use React\EventLoop\LoopInterface; -use React\Promise\CancellablePromiseInterface; use React\Promise\Promise; use React\Promise\PromiseInterface; @@ -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) { @@ -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; diff --git a/tests/FunctionTimeoutTest.php b/tests/FunctionTimeoutTest.php index b794fe7..dfe6eb8 100644 --- a/tests/FunctionTimeoutTest.php +++ b/tests/FunctionTimeoutTest.php @@ -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);