0

I have the below code.

this.ObservableForProperty(x => x.SelectedDay)
    .Throttle(TimeSpan.FromMilliseconds(3600))
    .Where(x => SelectedDay != null)
    .ObserveOn(CoreWindow.GetForCurrentThread().Dispatcher)
    .Subscribe(x => SomeRandomMethod());

Throttle is working great and it will not call SomeRandomMethod until x.SelectedDay has stopped changing. However it is calling it for each time that it has changed.

So i do this:  
Change,  
Change,  
Wait  
//SomeRandomMethod called 2 times at end of throttle  
Change  
Wait  
//SomeRandomMethod called 3 times
Change  
Change  
Change 
Wait  
//SomeRandomMethod called 6 times.

How can i get it to ignore all previous change events and only get the latest at the time throttle has done it's thing.

So i would like this:
Change   
Change 
Wait  
//SomeRandomMethod called once
Change  
Wait  
//SomeRandomMethod called once
Change  
Change  
Change 
Wait  
//SomeRandomMethod called once
4imble
  • 13,979
  • 15
  • 70
  • 125

1 Answers1

1

Where are you calling this method? You should only be calling it in the Constructor, and only calling it once. Also, here's a better version of the code above:

this.ObservableForProperty(x => x.SelectedDay)
    .Throttle(TimeSpan.FromMilliseconds(3600), RxApp.DeferredScheduler)
    .Where(x => SelectedDay != null)
    .Subscribe(x => SomeRandomMethod());
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • ha ha, of course. Feel such a dumb-ass. 'RxApp.DeferredScheduler' is a nice touch too. Many thanks. – 4imble Mar 12 '13 at 09:23
  • @Kohan No worries, it's a common mistake. Try to think of RxUI calls as describing how things are related - it's a declaration, like a XAML Binding. – Ana Betts Mar 12 '13 at 21:32