From 4ed8e65468a587aba8555ac004a13df0ee3cdbe0 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Thu, 16 Jun 2016 23:27:14 -0500 Subject: [PATCH] Cleaner resolution --- lib/Internal/Placeholder.php | 51 ++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/Internal/Placeholder.php b/lib/Internal/Placeholder.php index 05b539a..b8a4ea4 100644 --- a/lib/Internal/Placeholder.php +++ b/lib/Internal/Placeholder.php @@ -54,12 +54,14 @@ trait Placeholder { if (null === $this->onResolved) { $this->onResolved = $onResolved; - } elseif (!$this->onResolved instanceof WhenQueue) { - $this->onResolved = new WhenQueue($this->onResolved); - $this->onResolved->push($onResolved); - } else { - $this->onResolved->push($onResolved); + return; } + + if (!$this->onResolved instanceof WhenQueue) { + $this->onResolved = new WhenQueue($this->onResolved); + } + + $this->onResolved->push($onResolved); } /** @@ -73,25 +75,28 @@ trait Placeholder { $this->resolved = true; $this->result = $value; - if ($this->onResolved !== null) { - if ($this->result instanceof Awaitable) { - $this->result->when($this->onResolved); - } else { - try { - $onResolved = $this->onResolved; - $onResolved(null, $this->result); - } catch (\Throwable $exception) { - Loop::defer(static function () use ($exception) { - throw $exception; - }); - } catch (\Exception $exception) { - Loop::defer(static function () use ($exception) { - throw $exception; - }); - } - } + if ($this->onResolved === null) { + return; + } - $this->onResolved = null; + $onResolved = $this->onResolved; + $this->onResolved = null; + + if ($this->result instanceof Awaitable) { + $this->result->when($onResolved); + return; + } + + try { + $onResolved(null, $this->result); + } catch (\Throwable $exception) { + Loop::defer(static function () use ($exception) { + throw $exception; + }); + } catch (\Exception $exception) { + Loop::defer(static function () use ($exception) { + throw $exception; + }); } }