1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-12 17:37:36 +01:00
postgres/lib/Internal/Operation.php
Aaron Piotrowski a18d7386bf
Remove __destruct() from Internal\Operation
Users now define their own destruct methods invoking complete().
2017-07-29 10:25:06 -05:00

41 lines
874 B
PHP

<?php
namespace Amp\Postgres\Internal;
use Amp\Loop;
trait Operation {
/** @var bool */
private $complete = false;
/** @var callable[] */
private $onComplete = [];
public function onComplete(callable $onComplete) {
if ($this->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;
}
}