I am having problem understanding how Observable.Delay works and when the Dispose() is meant to be called. Would anyone familiar with Rx be able to help please?
The following code snippet:
static void Main(string[] args)
{
var oneNumberEveryFiveSeconds = new SomeObservable();
// Instant echo
oneNumberEveryFiveSeconds.SubscribeOn(Scheduler.ThreadPool).Subscribe(num => Console.WriteLine(num));
// One second delay
oneNumberEveryFiveSeconds.Delay(TimeSpan.FromSeconds(1)).SubscribeOn(Scheduler.ThreadPool).Subscribe(num => Console.WriteLine("...{0}...", num));
// Two second delay
oneNumberEveryFiveSeconds.Delay(TimeSpan.FromSeconds(2)).SubscribeOn(Scheduler.ThreadPool).Subscribe(num => Console.WriteLine("......{0}......", num));
Console.ReadKey();
}
public class SomeObservable : IObservable<int>
{
public IDisposable Subscribe(IObserver<int> o)
{
for (var i = 0; i < 2; i++)
{
o.OnNext(i);
}
o.OnCompleted();
return new DisposableAction(() => { Console.WriteLine("DISPOSED"); });
}
}
public class DisposableAction : IDisposable
{
public DisposableAction(Action dispose)
{
this.dispose = dispose;
}
readonly Action dispose;
public void Dispose()
{
dispose();
}
}
produces the below result:
0
1
DISPOSED
DISPOSED
DISPOSED
...0...
...1...
......0......
......1......
I was expecting it to be more like:
0
1
DISPOSED
...0...
...1...
DISPOSED
......0......
......1......
DISPOSED
Any idea??