Thursday, August 02, 2012

Think Rx (Reactive Extensions) is just for Stocks and stuff like that? Here's a cool use with the FileSystemWatcher

Matthieu MEZIL - FileSystemWatcher, Rx and Throttle

"Imagine that we want to be notified if a directory files changed.

In general, when we use files, we don’t need to have a real time application.

Moreover, if we change many files in same time, we perhaps don’t need to have many notification and one notification at the end could be enough.

For this case, Rx and particularly Throttle method is very useful.


Now we can use the observable like this:

var fileWatcher = new FileWatcher(".", "*.*", TimeSpan.FromSeconds(5));
var fileObserver = fileWatcher.GetObservable().Subscribe(fce => { /*TODO*/ });

Imagine that if we have n changes on the directory in less than 5 seconds between two consecutive changes.
With this code, we have one only notification, 5 seconds after the last one.


Most of the time I hear Rx being mentioned for use in high volume cases like stocks, high event loads, stuff that the normal dev might not see very often. And so the normal dev might think they can ignore Rx.

This is a great example that the normal dev should be able to easily grok and easily see the value that Rx can provide them...

No comments: