Rx .NET 跳过某些场景的值更改

Rx .NET skip value changes by some scenario

我在使用 Rx 实现特定场景时遇到问题。我有一些代码:

enum Status { NotConnected, Connecting, Connected }

class Provider
{
    public Status State { get => m_state; set => this.RaiseAndSetIfChanged(ref m_state, value); }
    public double Divergence { get => m_divergence; set => this.RaiseAndSetIfChanged(ref m_divergence, value); }
}

public void Run(Provider provider)
{
    provider.WhenAny(viewItem => viewItem.Divergence, change => change.Sender)
                                         .Throttle(TimeSpan.FromMilliseconds(500))
                                         .Subscribe(OnDivergenceChanged);
}

private void OnDivergenceChanged(Provider provider)
{
    ...
}

可以通过环图NotConnected->Connecting->Connected.->NotConnected->Connecting...改变状态
初始状态为 NotConnected,Divergence 变化未被阻止。
我需要跳过更改 Divergence 值,从连接的那一刻开始(状态为正在连接)到“已连接”状态建立后 5 秒结束。

这是一个可能的解决方案:

IObservable<double> Divergence;
IObservable<Status> State;
IObservable<double> onDivergenceChanged = State
    .Select(state => state switch
    {
        Status.NotConnected => Divergence,
        Status.Connecting => Observable.Empty<double>(),
        Status.Connected => Observable.Timer(TimeSpan.FromSeconds(5)).Select(_ => Divergence).Switch(),
        _ => throw new ArgumentOutOfRangeException(nameof(state), state, null)
    })
    .Switch();

它非常明确 - 对于每个状态,要么采用:a) 数字流; b) 没有; c) 5 秒计时器后的数字。