1
0
mirror of https://github.com/danog/postgres.git synced 2025-01-05 20:48:17 +01:00
postgres/lib/Internal/CompletionQueue.php

45 lines
959 B
PHP
Raw Normal View History

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