2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres\Internal;
|
|
|
|
|
|
|
|
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 __destruct() {
|
|
|
|
$this->complete();
|
|
|
|
}
|
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) {
|
|
|
|
$callback();
|
|
|
|
}
|
|
|
|
$this->onComplete = null;
|
|
|
|
}
|
|
|
|
}
|