Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay function to postpone IObservable values dynamically

[ This question is regarding IObservable / Rx ]

Working Fixed Delay

var frequency = TimeSpan.FromMinutes(5);
Result.Delay(frequency).Subscribe(i => Debug.WriteLine("After Fixed Delay"));

Pseudo-Code for Variable Delay

Result.Delay(GetAsymptotingTime()).Subscribe(i => Debug.WriteLine("After Changing Delay"));

While the code for variable delay compiles it gets called only once, providing only the first value (essentially a fixed value).

  • How could you subscribe with a dynamic delay in Reactive Extensions?
like image 282
Cel Avatar asked Nov 30 '25 05:11

Cel


1 Answers

Looks like there is a new overload of .Delay that allows this functionality within RX itself:

From http://blogs.msdn.com/b/rxteam/archive/2012/03/12/reactive-extensions-v2-0-beta-available-now.aspx :

var res = input.Delay(x => Observable.Timer(TimeSpan.FromSeconds(x.Length)));

Given the user input, it gets delays for a duration equal to the length of the input in seconds. Stated otherwise, the delay of each element can now be dependent on the data itself.

like image 160
Cel Avatar answered Dec 01 '25 19:12

Cel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!