1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-30 04:29:12 +01:00

Remove __destruct() from Internal\Operation

Users now define their own destruct methods invoking complete().
This commit is contained in:
Aaron Piotrowski 2017-07-29 10:25:06 -05:00
parent f1b1f0070d
commit a18d7386bf
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
3 changed files with 30 additions and 11 deletions

View File

@ -2,6 +2,8 @@
namespace Amp\Postgres\Internal;
use Amp\Loop;
trait Operation {
/** @var bool */
private $complete = false;
@ -9,10 +11,6 @@ trait Operation {
/** @var callable[] */
private $onComplete = [];
public function __destruct() {
$this->complete();
}
public function onComplete(callable $onComplete) {
if ($this->complete) {
$onComplete();
@ -29,7 +27,13 @@ trait Operation {
$this->complete = true;
foreach ($this->onComplete as $callback) {
$callback();
try {
$callback();
} catch (\Throwable $exception) {
Loop::defer(function () use ($exception) {
throw $exception; // Rethrow to event loop error handler.
});
}
}
$this->onComplete = null;
}

View File

@ -29,6 +29,12 @@ class Listener implements Iterator, Operation {
$this->unlisten = $unlisten;
}
public function __destruct() {
if ($this->unlisten) {
$this->unlisten(); // Invokes $this->complete().
}
}
/**
* {@inheritdoc}
*/
@ -56,10 +62,17 @@ class Listener implements Iterator, Operation {
* Unlistens from the channel. No more values will be emitted from this listener.
*
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
*
* @throws \Error If this method was previously invoked.
*/
public function unlisten(): Promise {
if (!$this->unlisten) {
throw new \Error("Already unlistened on this channel");
}
/** @var \Amp\Promise $promise */
$promise = ($this->unlisten)($this->channel);
$this->unlisten = null;
$promise->onResolve($this->callableFromInstanceMethod("complete"));
return $promise;
}

View File

@ -19,9 +19,6 @@ class Transaction implements Executor, Operation {
/** @var int */
private $isolation;
/** @var callable */
private $onResolve;
/**
* @param \Amp\Postgres\Executor $executor
* @param int $isolation
@ -42,7 +39,12 @@ class Transaction implements Executor, Operation {
}
$this->executor = $executor;
$this->onResolve = $this->callableFromInstanceMethod("complete");
}
public function __destruct() {
if ($this->executor) {
$this->rollback(); // Invokes $this->complete().
}
}
/**
@ -126,7 +128,7 @@ class Transaction implements Executor, Operation {
$promise = $this->executor->query("COMMIT");
$this->executor = null;
$promise->onResolve($this->onResolve);
$promise->onResolve($this->callableFromInstanceMethod("complete"));
return $promise;
}
@ -145,7 +147,7 @@ class Transaction implements Executor, Operation {
$promise = $this->executor->query("ROLLBACK");
$this->executor = null;
$promise->onResolve($this->onResolve);
$promise->onResolve($this->callableFromInstanceMethod("complete"));
return $promise;
}