Skip to content

Commit

Permalink
Revert "Call handler when waiting on fulfilled/rejected Promise (#135)…
Browse files Browse the repository at this point in the history
…" (#146)

This reverts commit 9c0acf5.
  • Loading branch information
GrahamCampbell authored Oct 22, 2021
1 parent b2e8301 commit a660655
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 76 deletions.
34 changes: 8 additions & 26 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ class FulfilledPromise implements PromiseInterface
{
private $value;

/** @var Promise|null */
private $promise;

/** @var callable|null */
private $onFulfilled;

public function __construct($value)
{
if (is_object($value) && method_exists($value, 'then')) {
Expand All @@ -38,14 +32,18 @@ public function then(
return $this;
}

$this->onFulfilled = $onFulfilled;

$queue = Utils::queue();
$p = $this->promise = new Promise([$queue, 'run']);
$p = new Promise([$queue, 'run']);
$value = $this->value;
$queue->add(static function () use ($p, $value, $onFulfilled) {
if (Is::pending($p)) {
self::callHandler($p, $value, $onFulfilled);
try {
$p->resolve($onFulfilled($value));
} catch (\Throwable $e) {
$p->reject($e);
} catch (\Exception $e) {
$p->reject($e);
}
}
});

Expand All @@ -59,11 +57,6 @@ public function otherwise(callable $onRejected)

public function wait($unwrap = true, $defaultDelivery = null)
{
// Don't run the queue to avoid deadlocks, instead directly resolve the promise.
if ($this->promise && Is::pending($this->promise)) {
self::callHandler($this->promise, $this->value, $this->onFulfilled);
}

return $unwrap ? $this->value : null;
}

Expand All @@ -88,15 +81,4 @@ public function cancel()
{
// pass
}

private static function callHandler(Promise $promise, $value, callable $handler)
{
try {
$promise->resolve($handler($value));
} catch (\Throwable $e) {
$promise->reject($e);
} catch (\Exception $e) {
$promise->reject($e);
}
}
}
37 changes: 11 additions & 26 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ class RejectedPromise implements PromiseInterface
{
private $reason;

/** @var Promise|null */
private $promise;

/** @var callable|null */
private $onRejected;

public function __construct($reason)
{
if (is_object($reason) && method_exists($reason, 'then')) {
Expand All @@ -38,14 +32,21 @@ public function then(
return $this;
}

$this->onRejected = $onRejected;

$queue = Utils::queue();
$reason = $this->reason;
$p = $this->promise = new Promise([$queue, 'run']);
$p = new Promise([$queue, 'run']);
$queue->add(static function () use ($p, $reason, $onRejected) {
if (Is::pending($p)) {
self::callHandler($p, $reason, $onRejected);
try {
// Return a resolved promise if onRejected does not throw.
$p->resolve($onRejected($reason));
} catch (\Throwable $e) {
// onRejected threw, so return a rejected promise.
$p->reject($e);
} catch (\Exception $e) {
// onRejected threw, so return a rejected promise.
$p->reject($e);
}
}
});

Expand All @@ -63,11 +64,6 @@ public function wait($unwrap = true, $defaultDelivery = null)
throw Create::exceptionFor($this->reason);
}

// Don't run the queue to avoid deadlocks, instead directly reject the promise.
if ($this->promise && Is::pending($this->promise)) {
self::callHandler($this->promise, $this->reason, $this->onRejected);
}

return null;
}

Expand All @@ -92,15 +88,4 @@ public function cancel()
{
// pass
}

private static function callHandler(Promise $promise, $reason, callable $handler)
{
try {
$promise->resolve($handler($reason));
} catch (\Throwable $e) {
$promise->reject($e);
} catch (\Exception $e) {
$promise->reject($e);
}
}
}
12 changes: 0 additions & 12 deletions tests/FulfilledPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ public function testAsynchronouslyInvokesOnFulfilled()
$this->assertSame('a', $r);
}

public function testInvokesOnFulfilledOnWait()
{
$p = new FulfilledPromise('a');
$r = null;
$f = function ($d) use (&$r) { $r = $d; };
$p->then($f);
$this->assertNull($r);
$p->wait();
$this->assertSame('a', $r);
}

public function testReturnsNewRejectedWhenOnFulfilledFails()
{
$p = new FulfilledPromise('a');
Expand Down Expand Up @@ -120,6 +109,5 @@ public function testDoesNotTryToFulfillTwiceDuringTrampoline()
$t1 = $fp->then(function ($v) { return $v . ' b'; });
$t1->resolve('why!');
$this->assertSame('why!', $t1->wait());
P\Utils::queue()->run();
}
}
12 changes: 0 additions & 12 deletions tests/RejectedPromiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ public function testInvokesOnRejectedAsynchronously()
$this->assertSame('a', $r);
}

public function testInvokesOnRejectedOnWait()
{
$p = new RejectedPromise('a');
$r = null;
$f = function ($reason) use (&$r) { $r = $reason; };
$p->then(null, $f);
$this->assertNull($r);
$p->wait(false);
$this->assertSame('a', $r);
}

public function testReturnsNewRejectedWhenOnRejectedFails()
{
$p = new RejectedPromise('a');
Expand Down Expand Up @@ -156,6 +145,5 @@ public function testDoesNotTryToRejectTwiceDuringTrampoline()
$t1 = $fp->then(null, function ($v) { return $v . ' b'; });
$t1->resolve('why!');
$this->assertSame('why!', $t1->wait());
P\Utils::queue()->run();
}
}

0 comments on commit a660655

Please sign in to comment.