Thursday, November 26, 2009


Rx is awesome :)

I have just started a new project called RxSandbox. It will be a very simple application that help to understand how Rx works, how each operator is implemented. But lets start from the beginning. Few days ago I was playing with Rx operators such as Zip, Merge, Wait, etc. writing a very short chunk of code for each operator. I was wondering how do they work in details, if they cache the values from sources, when exactly is OnCompleted executed on the observers and so on. The code could look like this:

a.Zip(b, (x, y) => x + " - " + y)

I wanted to a and b be the implementation of IObservable<string>. But what is the easiest way to create the implementation for them? I just wanted to be focused on writing code using operators so I needed some infrastructure for creating sample observable data sources. The RxSandbox is the solution! When you write something like this:

Expression<Func<ManualObservable<string>, ManualObservable<string>,
    IObservable<string>>> zipExpression
        = (a, b) => a.Zip(b, (x, y) => x + " - " + y);
Control control = RxExpressionVisualizer.CreateControl(zipExpression);

RxSandbox will automatically generate testing UI control:


In the code above ManualObservable means that the user can manually define the values sent to the expression by writing them in a text box. Other possible could be RandomObservable, IntervalObservable and there are many more options. The UI control will be different for each type of Observable source. The only reason why the Expression<Func<...>> type has been used here is that this allows us to display the body of expression, but of course this is just an option. In general, the test case is a method taking some Observable sources and returning observable collection. It can be some really complicated code snippet with many statements as well.

Possible scenarios working with RxSanbox:

  • Run RxSandbox application, create new project in VS, write Rx expression, compile it, RxSandbox automatically  finds new version of dll file and loads it, you are ready to test it (it may be MEF, VS AddIn not just of stand along application)
  • Run RxSandbox, write your Rx expression inside RxSandbox, start testing it (the easy way to do this is to use ExpressionTextBox control from WF4.0 which allows us to write single VB expression and expose it as an Linq expression type)
  • Drawing marble diagrams live during testing and also presenting the definitions of the operators as marble diagrams (of course with pac-mans and hearts :) - )
  • Supporting Silverlight version
  • Many many more....

I hope you got the idea. Here you can find the first prototype.


WA6O said...

Very interesting app! Thank you very much.

It doesn't work OOB with the latest Rx drop though, but that why the source code is here, right? :)

Thanks again!

Marcin Najder said...


Thanks for comment!

You are right, this version of RxSandbox does not work with latest drop of Rx. If you want to use RxSandbox with the latest Rx version you need to:
- change references to new Rx drop
- comment one line in App.xaml.cs file
//Observable.Context = SynchronizationContext.Current;
- change two method names
WaitUntil -> SkipUntil
Until -> TakeUntil

I know that not many thing we can do with RxSandbox in that version but it is just a quick prototype (proof of concept):)

M4G33k said...

watching you showing Rx capabilities with this software was very impressive. keep up the good work! ;)