From 42f7fccd891331599d869d2fd44874eea8e61574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 3 Jul 2022 17:55:21 +0200 Subject: [PATCH] Remove internal `Queue` leftovers --- src/Internal/FulfilledPromise.php | 16 +++---- src/Internal/Queue.php | 30 ------------- src/Internal/RejectedPromise.php | 16 +++---- src/functions.php | 14 ------- tests/Internal/QueueTest.php | 70 ------------------------------- 5 files changed, 10 insertions(+), 136 deletions(-) delete mode 100644 src/Internal/Queue.php delete mode 100644 tests/Internal/QueueTest.php diff --git a/src/Internal/FulfilledPromise.php b/src/Internal/FulfilledPromise.php index 78fadf31..d3e535e0 100644 --- a/src/Internal/FulfilledPromise.php +++ b/src/Internal/FulfilledPromise.php @@ -2,9 +2,7 @@ namespace React\Promise\Internal; -use React\Promise\Promise; use React\Promise\PromiseInterface; -use function React\Promise\enqueue; use function React\Promise\resolve; /** @@ -29,15 +27,11 @@ public function then(callable $onFulfilled = null, callable $onRejected = null): return $this; } - return new Promise(function (callable $resolve, callable $reject) use ($onFulfilled): void { - enqueue(function () use ($resolve, $reject, $onFulfilled): void { - try { - $resolve($onFulfilled($this->value)); - } catch (\Throwable $exception) { - $reject($exception); - } - }); - }); + try { + return resolve($onFulfilled($this->value)); + } catch (\Throwable $exception) { + return new RejectedPromise($exception); + } } public function catch(callable $onRejected): PromiseInterface diff --git a/src/Internal/Queue.php b/src/Internal/Queue.php deleted file mode 100644 index 70f85817..00000000 --- a/src/Internal/Queue.php +++ /dev/null @@ -1,30 +0,0 @@ -queue, $task)) { - $this->drain(); - } - } - - private function drain(): void - { - for ($i = \key($this->queue); isset($this->queue[$i]); $i++) { - $task = $this->queue[$i]; - unset($this->queue[$i]); - - $task(); - } - - $this->queue = []; - } -} diff --git a/src/Internal/RejectedPromise.php b/src/Internal/RejectedPromise.php index e5565989..a8360e24 100644 --- a/src/Internal/RejectedPromise.php +++ b/src/Internal/RejectedPromise.php @@ -2,10 +2,8 @@ namespace React\Promise\Internal; -use React\Promise\Promise; use React\Promise\PromiseInterface; use function React\Promise\_checkTypehint; -use function React\Promise\enqueue; use function React\Promise\resolve; /** @@ -26,15 +24,11 @@ public function then(callable $onFulfilled = null, callable $onRejected = null): return $this; } - return new Promise(function (callable $resolve, callable $reject) use ($onRejected): void { - enqueue(function () use ($resolve, $reject, $onRejected): void { - try { - $resolve($onRejected($this->reason)); - } catch (\Throwable $exception) { - $reject($exception); - } - }); - }); + try { + return resolve($onRejected($this->reason)); + } catch (\Throwable $exception) { + return new RejectedPromise($exception); + } } public function catch(callable $onRejected): PromiseInterface diff --git a/src/functions.php b/src/functions.php index 7517d6a2..b8eac495 100644 --- a/src/functions.php +++ b/src/functions.php @@ -205,20 +205,6 @@ function (\Throwable $reason) use ($i, &$reasons, &$toReject, $reject, &$continu }, $cancellationQueue); } -/** - * @internal - */ -function enqueue(callable $task): void -{ - static $queue; - - if (!$queue) { - $queue = new Internal\Queue(); - } - - $queue->enqueue($task); -} - /** * @internal */ diff --git a/tests/Internal/QueueTest.php b/tests/Internal/QueueTest.php deleted file mode 100644 index 51ffedc1..00000000 --- a/tests/Internal/QueueTest.php +++ /dev/null @@ -1,70 +0,0 @@ -enqueue($this->expectCallableOnce()); - $queue->enqueue($this->expectCallableOnce()); - } - - /** @test */ - public function executesNestedEnqueuedTasks() - { - $queue = new Queue(); - - $nested = $this->expectCallableOnce(); - - $task = function () use ($queue, $nested) { - $queue->enqueue($nested); - }; - - $queue->enqueue($task); - } - - /** - * @test - * @requires PHP 8.1 - */ - public function executesFollowingTasksIfPriorTaskSuspendsFiber() - { - $queue = new Queue(); - - $fiber = new \Fiber(function () use ($queue) { - $queue->enqueue(function () { - \Fiber::suspend(2); - }); - return 1; - }); - - $ret = $fiber->start(); - $this->assertEquals(2, $ret); - - $queue->enqueue($this->expectCallableOnce()); - } - - /** - * @test - */ - public function rethrowsExceptionsThrownFromTasks() - { - $this->expectException(Exception::class); - $this->expectExceptionMessage('test'); - $mock = $this->createCallableMock(); - $mock - ->expects(self::once()) - ->method('__invoke') - ->will(self::throwException(new Exception('test'))); - - $queue = new Queue(); - $queue->enqueue($mock); - } -}