Rx-Demo
Rx-Demo copied to clipboard
Demos and sample for Reactive Extensions. Includes example of using Rx as a real-time OLAP cube
Code to accompany Intro to Reactive extentions talk
#LinqPad Demos
- [BackgroundThread](LinqPadSamples/01 BackgroundThread.linq)
- [ThreadSafe](LinqPadSamples/01 ThreadSafe.linq)
- [ColdEnumerable]("LinqPadSamples/02 ColdEnumerable.linq")
- [ColdObservable]("LinqPadSamples/03 ColdObservable.linq")
- [HotObservable]("LinqPadSamples/04 HotObservable.linq")
- [Create]("LinqPadSamples/06 Create.linq")
- [CreateDisposable]("LinqPadSamples/06 CreateDisposable.linq")
- [GenerateWithTime]("LinqPadSamples/07 GenerateWithTime.linq")
VisualStudio Demos
Querying a server
- A slow or expensive query
- On a background loop
- With user initiated action, for the impatient
- But not too often
- Compare the ad-hoc w/ the Rx approach
###UI for password check
- Accept correct password
- Reject incorrect
- No "submit button"
- Timeout
- Must be unit testable
Twitter client
- Need to make an IO from a traditional API
- Want a mock version for UI development
- Only want to show latest for each user
- Want late subscribers to have latest values
Streaming Olap
Agenda
- What's the Rx thing about, why should I care?
- Will it make my life easier?
- What's new in 1.0
What's the Rx thing about, why should I care?
- For some, Rx is something of a revelation.
- A good way to deal with asynchronous code
- A good way to deal with event based code
- A good way to deal with concurrent code
- A good way to deal with time sensitive code
Reactive matters
-
Systems and architectures that are not reactive are legacy systems
- Real life is async
- Sync, ie, blocking activities are souces of real stress
- waiting in line for coffee, the elevator, the check to clear, etc
-
Batch, pull, polling, request/response were common b/c building asyncronous code was (much) harder
-
Till now...
A good way?
- Composable
- Testable
- Pure (side-effect free)
###Two use cases
- Managing a bar
- Calling a web service async
So what is Rx?
- A new Interface pair
IObservable<T>
andIObserver<T>
for .Net BCL- The dual of
IEnumerable<T>
andIEnumerator<T>
- The dual of
- A core implementation and a set of Linq combinators for doing useful things
A look at some recent Rx questions
- Detect "double tap" on a smartphone
- Update on maximum value reached (price, diskspace, water volume, temp, etc)
- Detecting if a server went down
Open questions?
More Details - IEnumerable and IEnumerator
interface IEnumerable<T>
{
IEnumerator<T> GetEnumerator();
}
interface IEnumerator<T>: IDisposable
{
T Current { get; }
bool MoveNext();
}
More Details - IObservable and IObserver
interface IObservable<T>
{
IDisposable Subscribe(IObserver<T> observer);
}
interface IObserver<T>
{
void OnCompleted();
void OnError(Exception error);
void OnNext(T value);
}
Pop Quiz - Hot or Not?
Pop Quiz - Hot or Not?
-
new[] { 1, 2, 3, 4 }
-
new List<string>()
-
Enumerable.Range(1, Int32.MaxValue)
-
new int[Int32.MaxValue]
-
People.Where(pers => pers.Age >= 21)
-
Observable.Range(1,3)
-
Observable.FromEvent("Click")
Traps
- Avoid implementing IObservable from scratch
- Hot or not?