Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I throttle a slider's value change event?

I got a slider that on value change forces a fairly serious computation, so I want to throttle it to fire actual event after for example 50ms pass when user has finished sliding it.

While I learned some various stuff about Rx its unclear how should I approach this using MVVM pattern.

In my current MVVM approach I got slider value bound to my viewModel. I would prefer to add Rx throttle with minimal possible impact on existing code (as a beginning at least).

Ive seen some other threads about MVVM and Rx and I don't think they lead me to some exact direction with my problem. I see various possible approaches and would like not to invent a bycicle.

like image 260
Valentin Kuzub Avatar asked Sep 20 '11 14:09

Valentin Kuzub


1 Answers

In this case, you should bind to the PropertyChanged event of your ViewModel, something like:

Observable.FromEvent<PropertyChangedEventArgs>(x => this.PropertyChanged +=x, x => this.PropertyChanged -= x)
    .Where(x => x.PropertyName == "SliderName")
    .Select(_ => this.SliderName)
    .Throttle(TimeSpan.FromMilliseconds(50));

Or, if you were using ReactiveUI, it'd look like this:

this.WhenAnyValue(x => x.SliderName)
    .Throttle(TimeSpan.FromMilliseconds(50), RxApp.DeferredScheduler);
like image 107
Ana Betts Avatar answered Nov 10 '22 07:11

Ana Betts