namespace NATS.Server.Subscriptions;

public sealed class SubListCacheSweeper
{
    private int _scheduled;

    public void ScheduleSweep(Action sweep)
    {
        if (Interlocked.CompareExchange(ref _scheduled, 1, 0) != 0)
            return;

        _ = Task.Run(() =>
        {
            try
            {
                sweep();
            }
            finally
            {
                Interlocked.Exchange(ref _scheduled, 0);
            }
        });
    }

    public Task TriggerSweepAsync(Action sweep)
    {
        return Task.Run(sweep);
    }
}
