From 32369079e6690de7ce44a4b4be3ef858df80f325 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Thu, 3 Aug 2017 00:42:53 -0500 Subject: [PATCH] Drop Operation trait Replaced with CompletionQueue. --- lib/Internal/Operation.php | 40 -------------------------------------- lib/Listener.php | 18 ++++++++++++----- lib/Transaction.php | 20 +++++++++++++------ 3 files changed, 27 insertions(+), 51 deletions(-) delete mode 100644 lib/Internal/Operation.php diff --git a/lib/Internal/Operation.php b/lib/Internal/Operation.php deleted file mode 100644 index 98780c8..0000000 --- a/lib/Internal/Operation.php +++ /dev/null @@ -1,40 +0,0 @@ -complete) { - $onComplete(); - return; - } - - $this->onComplete[] = $onComplete; - } - - private function complete() { - if ($this->complete) { - return; - } - - $this->complete = true; - foreach ($this->onComplete as $callback) { - try { - $callback(); - } catch (\Throwable $exception) { - Loop::defer(function () use ($exception) { - throw $exception; // Rethrow to event loop error handler. - }); - } - } - $this->onComplete = null; - } -} diff --git a/lib/Listener.php b/lib/Listener.php index 40cf4f2..e5590b2 100644 --- a/lib/Listener.php +++ b/lib/Listener.php @@ -2,13 +2,10 @@ namespace Amp\Postgres; -use Amp\CallableMaker; use Amp\Iterator; use Amp\Promise; class Listener implements Iterator, Operation { - use CallableMaker, Internal\Operation; - /** @var \Amp\Iterator */ private $iterator; @@ -18,6 +15,9 @@ class Listener implements Iterator, Operation { /** @var callable */ private $unlisten; + /** @var \Amp\Postgres\Internal\CompletionQueue */ + private $queue; + /** * @param \Amp\Iterator $iterator Iterator emitting notificatons on the channel. * @param string $channel Channel name. @@ -27,14 +27,22 @@ class Listener implements Iterator, Operation { $this->iterator = $iterator; $this->channel = $channel; $this->unlisten = $unlisten; + $this->queue = new Internal\CompletionQueue; } public function __destruct() { if ($this->unlisten) { - $this->unlisten(); // Invokes $this->complete(). + $this->unlisten(); // Invokes $this->queue->complete(). } } + /** + * {@inheritdoc} + */ + public function onComplete(callable $onComplete) { + $this->queue->onComplete($onComplete); + } + /** * {@inheritdoc} */ @@ -73,7 +81,7 @@ class Listener implements Iterator, Operation { /** @var \Amp\Promise $promise */ $promise = ($this->unlisten)($this->channel); $this->unlisten = null; - $promise->onResolve($this->callableFromInstanceMethod("complete")); + $promise->onResolve([$this->queue, "complete"]); return $promise; } } diff --git a/lib/Transaction.php b/lib/Transaction.php index dc35a3e..8edfb20 100644 --- a/lib/Transaction.php +++ b/lib/Transaction.php @@ -2,12 +2,9 @@ namespace Amp\Postgres; -use Amp\CallableMaker; use Amp\Promise; class Transaction implements Handle, Operation { - use Internal\Operation, CallableMaker; - const UNCOMMITTED = 0; const COMMITTED = 1; const REPEATABLE = 2; @@ -19,6 +16,9 @@ class Transaction implements Handle, Operation { /** @var int */ private $isolation; + /** @var \Amp\Postgres\Internal\CompletionQueue */ + private $queue; + /** * @param \Amp\Postgres\Handle $handle * @param int $isolation @@ -39,14 +39,22 @@ class Transaction implements Handle, Operation { } $this->handle = $handle; + $this->queue = new Internal\CompletionQueue; } public function __destruct() { if ($this->handle) { - $this->rollback(); // Invokes $this->complete(). + $this->rollback(); // Invokes $this->queue->complete(). } } + /** + * {@inheritdoc} + */ + public function onComplete(callable $onComplete) { + $this->queue->onComplete($onComplete); + } + /** * @return bool True if the transaction is active, false if it has been committed or rolled back. */ @@ -128,7 +136,7 @@ class Transaction implements Handle, Operation { $promise = $this->handle->query("COMMIT"); $this->handle = null; - $promise->onResolve($this->callableFromInstanceMethod("complete")); + $promise->onResolve([$this->queue, "complete"]); return $promise; } @@ -147,7 +155,7 @@ class Transaction implements Handle, Operation { $promise = $this->handle->query("ROLLBACK"); $this->handle = null; - $promise->onResolve($this->callableFromInstanceMethod("complete")); + $promise->onResolve([$this->queue, "complete"]); return $promise; }