Time Chaos

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. 🙂

Advertisements

About xosfaere

Software Developer
This entry was posted in Declarative, Imperative, Paradigm, Software, Technical and tagged , , , , , , , . Bookmark the permalink.

One Response to Time Chaos

  1. xosfaere says:

    Note: A more robust and general implementation would of course make use of the IScheduler interface. But this is for development purposes and that was not needed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s