0

I have been trying to learn ReactiveUI + RxNet lately... I love them both and they are quite mind bending... I have been reading 'Programming Reactive Extensions and Linq' and it includes this code snippet (modified so that it uses the latest classes/methods):

var sched = new TestScheduler();
var input = sched.CreateColdObservable(
    OnNext(205, 1),
    OnNext(305, 10),
    OnNext(405, 100),
    OnNext(505, 1000),
    OnNext(605, 10000),
    OnCompleted<int>(1100));

int i = 0;
var windows = input.Window(
    Observable.Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(100), sched).Take(7),
    x => Observable.Timer(TimeSpan.FromMilliseconds(50), sched));

windows.Timestamp(sched)
       .Subscribe(obs =>
       {
           int current = ++i;
           Console.WriteLine($"Started Obserable {current} at {obs.Timestamp.Millisecond:n0}ms");

           obs.Value.Subscribe(
               item => 
               Console.WriteLine($"     {item} at {sched.Now.Millisecond:n0}ms"),
               () => Console.WriteLine($"Ended Obserable {current} at {sched.Now.Millisecond:n0}"));
       });

sched.Start();

This is the ouput:

Started Obserable 1 at 0ms
     1 at 0ms
     10 at 0ms
     100 at 0ms
     1000 at 0ms
     10000 at 0ms
Ended Obserable 1 at 50
Started Obserable 2 at 100ms
Ended Obserable 2 at 150
Started Obserable 3 at 200ms
Ended Obserable 3 at 250
Started Obserable 4 at 300ms
Ended Obserable 4 at 350
Started Obserable 5 at 400ms
Ended Obserable 5 at 450
Started Obserable 6 at 500ms
Ended Obserable 6 at 550
Started Obserable 7 at 600ms
Ended Obserable 7 at 650

And this is the expected output:

Started Observable 1 at 0ms
Ended Observable 1 at 50ms
Started Observable 2 at 100ms
Ended Observable 2 at 150ms
Started Observable 3 at 200ms
1 at 205ms
Ended Observable 3 at 250ms
Started Observable 4 at 300ms
10 at 305ms
Ended Observable 4 at 350ms
Started Observable 5 at 400ms
100 at 405ms
Ended Observable 5 at 450ms
Started Observable 6 at 500ms
1000 at 505ms
Ended Observable 6 at 550ms
Started Observable 7 at 600ms
10000 at 605ms
Ended Observable 7 at 650ms

Any idea why? what have I missed?

Muhammad Azeez
  • 926
  • 8
  • 18
  • You shouldn't have nested subscribes - always perform some sort of `.SelectMany` (or `Switch()`, but not in this case) to create a single subscription. – Enigmativity Jun 01 '19 at 07:56

1 Answers1

0

I don't know what you have in your OnNext method, but the constructor for Recorded<Notification<T>>, what is what you put into the CreateColdObservable method, takes ticks and not milliseconds as the first argument. So I would try this:

var input = sched.CreateColdObservable(
    OnNext(2050000, 1),
    OnNext(3050000, 10),
    OnNext(4050000, 100),
    OnNext(5050000, 1000),
    OnNext(6050000, 10000),
    OnCompleted<int>(11000000));
Magnus
  • 353
  • 3
  • 8