Reactive Programming by UniRxfor Asynchronous & Event Processing
2014/07/30Yoshifumi Kawai - @neuecc
Self Introduction
@
CTO
C# 5.0 + .NET Framework 4.5 + ASP.NET MVC 5
C# Web
@
Microsoft MVP for Visual C# / C#
Web http://neue.cc/
Twitter @neuecc
What’s Reactive Programming?
Functional Reactive Programming
http://en.wikipedia.org/wiki/Functional_reactive_programming
Excel ……
Reactive Extensions
FRP (Haskell )
2009 .NET Reactive Extensions
Functional Reactive Programming
http://en.wikipedia.org/wiki/Functional_reactive_programming
Excel ……
Reactive Extensions
FRP (Haskell )
2009 .NET Reactive Extensions
UniRx
ReactiveExtensions(Rx) Unity
Gartner’s Hype Cycle
2013 Application Architecture/Application Development
On the Rise - Reactive Programming
Technology Radar Languages & Framework
ThoughtWorks Technology Radar July 2014
Rx ADOPT OK
TRIAL
ASSESS
HOLD
Across Languages
RxJava
Netflix for Java/Scala
ReactiveCocoa
GitHub 5073 for Objective-C
RxJS/bacon.js
JavaScript
RxJS bacon.js
UniRx
UniRx 100
Asset Store
Reactive Programming
Reactive Extensions
RxJava Wiki
UniRx - Reactive Extensions for Unity
Reactive Extensions(Rx) is
LINQ
C# LINQ
LINQ(to Objects) C#
Rx LINQ
LINQ
Reactive
Programming
Rx
UniRx is
.NET(C#) Rx Unity
https://github.com/neuecc/UniRx
http://u3d.as/7tT (Asset Store, Price : FREE)
Rx + Unity
C# ……
https://rx.codeplex.com/
Bart De Smet Microsoft
Unity :)
Q
C# LINQ Rx 5
……
3 @IT
http://www.atmarkit.co.jp/fdotnet/introrx/introrx_01/introrx_01_01.html
AOT Safe
LINQ iOS AOT
Unity + iOS AOT
http://neue.cc/2014/07/01_474.html
Reactive Extensions isLINQ to EventsLINQ to Asynchronous
Event is Observable Sequence
Rx
IObservable<T> time
OnTap
3 Tap
5 Tap
1 Tap
IEnumerable <-> IObservable
IObservable<T> time
IEnumerable<T> length
IEnumerable<T> length
.Where(x => x % 2 == 0)
IObservable<T> time
.Where(x => x % 2 == 0)
Where
LINQ
Push Event Stream
Event Processing
Interactive/Visualize
Internet of Things
Push
Kinect
Oculus
Twitter Streaming
PubSub, WebSocket
Logs are Stream(Fluentd, Amazon Kinesis, Azure Event Hubs)
MonoBehaviour Update , OnMouseClick, etc...
LINQ
LINQ
LINQ
LINQ
Better EventHandling
Limitations of .NET Events
// public event Action<int> OnHitDamage;
// player.OnHitDamage += (damage) =>{
if (damage >= 1000){
// " "}
};
player.OnHitDamage -= /* */
Observable Sequence to the Rescue
IObservable<int> onHitDamage = player.OnHitDamage;
var criticalHit = onHitDamage.Where(x =>x >= 1000);
var subscription = criticalHit.Subscribe(damage => /* ... */);
subscription.Dispose();
LINQ
Lifecycle Resource Management// DisposableCompositeDisposable subscriptions = new CompositeDisposable();
void Awake(){
var player = new Player();var enemy1 = new Player();var enemy2 = new Player();
// player.OnHitDamage.Subscribe().AddTo(subscriptions);enemy1.OnHitDamage.Subscribe().AddTo(subscriptions);enemy2.OnHitDamage.Subscribe().AddTo(subscriptions);
}
void OnDestroy(){
// subscriptions.Dispose();
}
CuringYourAsynchronousProgrammingBlues
yield return is awaitable
IEnumerator GetBingText(){
var www = new WWW("http://bing.com/");yield return www; //
Debug.Log(www.text);}
It’s Unity’s awesome feature!
But...IEnumerator GetGoogle(){
var www = new WWW("http://google.com/");yield return www;
}
IEnumerator OnMouseDown(){
try{
// yield return StartCoroutine(GetGoogle());
}catch{}
}
IEnumerator
yield return try-catch
IEnumerator GetGoogle(Action<string> onCompleted, Action<Exception> onError){
var www = new WWW("http://google.com/");yield return www;
if (!www.error) onError(new Exception(www.error));else onCompleted(www.text);
}
……(JavaScript )
IEnumerator
Rx Unity
ObservableWWW.Get("http://google.co.jp/").SelectMany(x => ObservableWWW.Get(x)) // .Retry(3) // 3.Subscribe(
x => Debug.Log(x), // ex => Debug.LogException(ex)); //
Rx
x x
x
Why can Rx apply to asynchronous?
x
IEnumerable<T>
IObservable<T>
IObservable<T> time
event
async
IE<T>
Why can Rx apply to asynchronous?
x
IEnumerable<T>
IObservable<T>
IObservable<T> time
event
async
IE<T>
Orchestrate Rx
var parallel = Observable.WhenAll(ObservableWWW.Get("http://google.com/"),ObservableWWW.Get("http://bing.com/"),ObservableWWW.Get("http://unity3d.com/"));
parallel.Subscribe(xs =>{
Debug.Log(xs[0].Substring(0, 100)); // googleDebug.Log(xs[1].Substring(0, 100)); // bingDebug.Log(xs[2].Substring(0, 100)); // unity
});
IObservable<T> time
IObservable<T> time
WhenAll
Observable.WhenAll(
ObservableWWW.Get(),
ObservableWWW.Get(),
ObservableWWW.Get())
Subscribe(xs => xs[0], xs[1], xs[2])
Conclusion
Reactive Programming
UniRx
Available Now
GitHub - https://github.com/neuecc/UniRx/
Asset Store(FREE) – http://u3d.as/7tT
Update
(v4.4)