public static async ValueTask DelayedStateMonitor()

in src/DotPulsar/Extensions/StateExtensions.cs [113:154]


    public static async ValueTask DelayedStateMonitor<TEntity, TState, TFaultContext>(
        this TEntity stateImplementer,
        TState state,
        TimeSpan delay,
        Func<TEntity, TState, CancellationToken, ValueTask<TFaultContext>> onStateLeft,
        Func<TEntity, TState, TFaultContext, CancellationToken, ValueTask> onStateReached,
        CancellationToken cancellationToken) where TEntity : IState<TState> where TState : notnull where TFaultContext : class
    {
        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var currentState = await stateImplementer.OnStateChangeFrom(state, delay, cancellationToken).ConfigureAwait(false);
            if (stateImplementer.IsFinalState(currentState))
                return;

            TFaultContext? faultContext = null;

            try
            {
                faultContext = await onStateLeft(stateImplementer, currentState, cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                // Ignore
            }

            currentState = await stateImplementer.OnStateChangeTo(state, cancellationToken).ConfigureAwait(false);
            if (stateImplementer.IsFinalState(currentState))
                return;

            try
            {
                if (faultContext is not null)
                    await onStateReached(stateImplementer, currentState, faultContext, cancellationToken).ConfigureAwait(false);
            }
            catch
            {
                // Ignore
            }
        }
    }