1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-12 17:37:36 +01:00
postgres/lib/Internal/Operation.php

41 lines
874 B
PHP
Raw Normal View History

2016-12-30 06:21:17 +01:00
<?php
2016-09-14 16:27:39 +02:00
namespace Amp\Postgres\Internal;
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) {
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;
}
}