mirror of
https://github.com/danog/postgres.git
synced 2024-12-12 17:37:36 +01:00
a18d7386bf
Users now define their own destruct methods invoking complete().
41 lines
874 B
PHP
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;
|
|
}
|
|
}
|