的IObservable<>通过CombineLatest并不会发射

时间:2017-07-11 23:01:17

标签: c# system.reactive

当我在breakpointcurrent上放置normal时,会调用它们。但是,如果我将breakpoint放在我CombineLatest下面的行上,则永远不会调用它。

FastPeriod = 60秒。 SlowPeriod = 120秒。

不确定我做错了什么。

public class SpikeIndicator : IDisposable, IIndicator
{
....
public IObservable<IndicatorValues> Stream { get; private set; }


private void IntializeSpikeStream()
{
    var current = Observable.Interval(TimeSpan.FromSeconds(FastPeriod))
         .Select(_ => GetLastPrice());

    var normal = Observable.Interval(TimeSpan.FromSeconds(SlowPeriod))
         .Select(_ => GetLastPrice()).Delay(TimeSpan.FromSeconds(FastPeriod));

    var stream = current.CombineLatest(normal, (a, b) => (a - b) / a)
        .Where(diffPercent => diffPercent != PercentDiff)
        .Select(ratePercent => new SpikeIndicatorValues { PercentRate = ratePercent }).Publish();

    spikeSubscription = stream.Connect();

    Stream = stream;            
}

public void Dispose()
{
    spikeSubscription?.Dispose();
}

public IObservable<IndicatorValues> GetObservable()
{
    return this.Stream;
}
...
}

1 个答案:

答案 0 :(得分:1)

想出来。不确定我需要多少这些改变,但这是有效的。

private void IntializeSpikeStream()
    {
        var current = Observable.Interval(TimeSpan.FromSeconds(FastPeriod))
             .Select(_ => GetLastPrice()).Publish();

        var normal = Observable.Interval(TimeSpan.FromSeconds(SlowPeriod))
             .Select(_ => GetLastPrice()).Delay(TimeSpan.FromSeconds(FastPeriod)).Publish();


        ts1Subscription1 = current.Connect();
        ts1Subscription2 = normal.Connect(); 

        var stream = current.CombineLatest(normal, (a, b) => (a - b) / a)
            .Where(diffPercent => diffPercent != PercentDiff)
            .Select(ratePercent => new SpikeIndicatorValues { PercentRate = ratePercent }).Publish();

        spikeSubscription = stream.Connect();

        Stream = stream;            
    }

    public void Dispose()
    {
        spikeSubscription?.Dispose();
        ts1Subscription1?.Dispose();
        ts1Subscription2?.Dispose();
    }