Today I created a new Reactive Extensions (Rx) LINQ operator to deal with a particular situation. The situation is that sometimes you only have small test data but you want to observe a process and project the values from that process onto a GUI surface – but if the process is very fast or the test data is small, then it can be hard to observe changes in time.
So in order to slow things down, I created a little developer test Rx operator called TimeChaos. Here is its F# definition:
type IObservable<'a> with member this.TimeChaos (min : int, max : int) : IObservable<'a> = if min > max then failwith "Inverse latency interval" if min < 0 then failwith "Negative latency" if min = max then if min = 0 then this else this.Do(fun x -> System.Threading.Thread.Sleep(min)) else let rnd = new Random() this.Do(fun x -> System.Threading.Thread.Sleep(rnd.Next(min, max)))
It is used like this:
let slower = process.TimeChaos(50,100) // slow down the arrival of every value by 50-100 ms
That’s it. 🙂