thesis/sync-once

Thesis Sync Once

0.1.1 2025-05-07 18:30 UTC

This package is auto-updated.

Last update: 2025-05-07 19:59:32 UTC


README

PHP Version Requirement GitHub Release Code Coverage Mutation testing badge

Installation

composer require thesis/sync-once

Usage

use Amp\TimeoutCancellation;
use Thesis\Amqp\Channel;
use Thesis\Amqp\Client;
use Thesis\Amqp\Message;
use Thesis\Sync\Once;

final readonly class AmqpTransport
{
    /**
     * @var Once<Channel>
     */
    private Once $publishChannel;

    public function __construct(
        private Client $client,
    ) {
        $this->publishChannel = new Once(
            // make sure to use static closures to avoid circular references
            function: static fn (): Channel => $client->channel(),
            isAlive: static fn (Channel $channel): bool => !$channel->isClosed(),
        );
    }

    public function publish(Message $message): void
    {
        $this
            ->publishChannel
            ->await(new TimeoutCancellation(10))
            ->publish($message);
    }
}