Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Promise v3 API compatibility to avoid hitting autoloader #55

Merged
merged 1 commit into from
Mar 7, 2022
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
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