add Connection::onClose

This commit is contained in:
2021-09-15 01:25:47 +03:00
parent af4e86d219
commit f3f67bedd3

View File

@@ -24,12 +24,19 @@ abstract class Connection
{ {
protected Stream $stream; protected Stream $stream;
/**
* @var callable
*/
private $onCloseCallback;
public function __construct( public function __construct(
private string $address, private string $address,
private ClientConfig $clientConfig, private ClientConfig $clientConfig,
private LoggerInterface $logger, private LoggerInterface $logger,
) { ) {
$this->stream = new NullStream(); $this->stream = new NullStream();
$this->onCloseCallback = static function () {
};
} }
public function __destruct() public function __destruct()
@@ -126,6 +133,15 @@ abstract class Connection
'class' => static::class, 'class' => static::class,
'address' => $this->address, 'address' => $this->address,
]); ]);
($this->onCloseCallback)();
}
public function onClose(callable $callback): static
{
$this->onCloseCallback = $callback;
return $this;
} }
protected function handleError(Frame\Error $error): void protected function handleError(Frame\Error $error): void