2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres\Internal;
|
|
|
|
|
2017-07-29 17:25:06 +02:00
|
|
|
use Amp\Loop;
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
trait Operation {
|
|
|
|
/** @var bool */
|
|
|
|
private $complete = false;
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var callable[] */
|
|
|
|
private $onComplete = [];
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
public function onComplete(callable $onComplete) {
|
|
|
|
if ($this->complete) {
|
|
|
|
$onComplete();
|
|
|
|
return;
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->onComplete[] = $onComplete;
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
private function complete() {
|
|
|
|
if ($this->complete) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->complete = true;
|
|
|
|
foreach ($this->onComplete as $callback) {
|
2017-07-29 17:25:06 +02:00
|
|
|
try {
|
|
|
|
$callback();
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
Loop::defer(function () use ($exception) {
|
|
|
|
throw $exception; // Rethrow to event loop error handler.
|
|
|
|
});
|
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
$this->onComplete = null;
|
|
|
|
}
|
|
|
|
}
|