namespace ClaudeDo.Worker.Prime;

public sealed class PrimeScheduleSignal : IDisposable
{
    private CancellationTokenSource _cts = new();
    private readonly object _lock = new();

    public CancellationToken CurrentToken
    {
        get { lock (_lock) return _cts.Token; }
    }

    public void Signal()
    {
        CancellationTokenSource old;
        lock (_lock)
        {
            old = _cts;
            _cts = new CancellationTokenSource();
        }
        try { old.Cancel(); } catch { /* already cancelled */ }
        old.Dispose();
    }

    public void Dispose()
    {
        lock (_lock) _cts.Dispose();
    }
}
