Misterhex
Misterhex

Reputation: 939

Rx Timeout with Select?

Observable.Interval(TimeSpan.FromSeconds(5))
.Timeout(dueTime: DateTimeOffset.UtcNow,
 other: Observable.Return<long>(-1))
.Subscribe(Console.WriteLine);

The code snippet above would return -1 when timed out. However, i need something that allow me to map the sequence, like the select operator.

I think i need something like a

.Timeout(DateTimeOffset dueTime, Func<IObservable<TSource>,Func<TSource, TResult> selector)

so that i do something like this

 Observable.Interval(TimeSpan.FromSeconds(5))
    .Timeout<long, string>(dueTime: DateTimeOffset.UtcNow,
     other: i=> Observable.Return<string>(i * i.ToString()))
    .Subscribe(Console.WriteLine);

Can someone enlighten me on this? Thanks.

Upvotes: 0

Views: 492

Answers (1)

Ana Betts
Ana Betts

Reputation: 74654

How about:

Observable.Interval(TimeSpan.FromSeconds(5))
    .SelectMany(x => DoWork().Timeout(...))
    .Subscribe(Console.WriteLine);

You could implement DoWork as:

IObservable<Unit> DoWork()
{
    return Observable.Start(() => Thread.Sleep(1000));
}

Upvotes: 1

Related Questions