thesis/sync-once

Thesis Sync Once

Fund package maintenance!
www.tinkoff.ru/cf/5MqZQas2dk7

Installs: 12 680

Dependents: 3

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

pkg:composer/thesis/sync-once

0.1.1 2025-05-07 18:30 UTC

This package is auto-updated.

Last update: 2025-10-07 20:52:50 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);
    }
}