skype: pavel.nasovich
email: forcewake@gmail.com
"Using Rx, developers represent asynchronous data streams with
- Rx README.md
+
+
=
pull
foreach
push
Subscribe(IObserver)
T Current, bool MoveNext()
throws Exception
returns
OnNext(T)
OnError(Exception)
OnCompleted
Observable.FromEventPattern
Observable.FromAsync
Observer.Create
Observable.Create
IDisposable subscription = Observable
.FromEventPattern<EventHandler<TextChangedEventArgs>, TextChangedEventArgs>
(ah => textEditor.TextChanged += ah, rh => textEditor.TextChanged -= rh)
.Throttle (TimeSpan.FromMilliseconds(400))
.Select (query => query.EventArgs.NewTextValue.Trim())
.Where (query => query.Length > 0)
.ObserveOn(RxApp.MainThreadScheduler)
.Do(q => listView.ItemsSource = null)
.Do(q => loading.IsVisible = true)
.Timeout(TimeSpan.FromSeconds(7))
.Select (query => githubAPI.GetUserObservable (query))
.Switch ()
.Catch((Refit.ApiException e) => Observable.Return(new User()))
.ObserveOn(RxApp.MainThreadScheduler)
.Do(q => loading.ProgressTo(1,2, Easing.Linear))
.Select (user => githubAPI.GetReposOwned(user.Login))
.Switch ()
.ObserveOn(RxApp.MainThreadScheduler)
.Do(q => loading.ProgressTo(2,2, Easing.Linear))
.Select (list => list.Where(repo => repo.Private != true))
.Do(r => loading.IsVisible = false)
.Retry()
.Subscribe(
onNext: list => listView.ItemsSource = list
);
Don’t forget to handle the exceptions (the best way to do it is by using Catch()).
Do() is a really good tool for logging any observable that doesn’t behave the way you intended.
Don’t forget to clean up after you subscribe to any observable (every Subscribe returns IDisposable).
When learning how to write reactive code, you don’t need to write everything in a reactive way.
If you try to write reactive code that you don't feel comfortable with, it’s better to write it as you used to and come back to it when you have more experience.
Don’t be afraid to create your own extensions. It’s really simple to write for example a WhereIsNotNull(), which should be self-explanatory.
So how can you test an observable that should trigger every hour? With ease: just include into your test project the “Microsoft.Reactive.Testing” nuget package (from the same guys that develop Rx) and use the TestScheduler. It is a virtual time scheduler and makes you a Time Lord.