reactive programming by unirx for asynchronous & event processing
DESCRIPTION
Unity Asset LT https://github.com/neuecc/UniRx/TRANSCRIPT
![Page 1: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/1.jpg)
Reactive Programming by UniRxfor Asynchronous & Event Processing
2014/07/30Yoshifumi Kawai - @neuecc
![Page 2: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/2.jpg)
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
![Page 3: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/3.jpg)
What’s Reactive Programming?
![Page 4: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/4.jpg)
Functional Reactive Programming
http://en.wikipedia.org/wiki/Functional_reactive_programming
Excel ……
Reactive Extensions
FRP (Haskell )
2009 .NET Reactive Extensions
![Page 5: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/5.jpg)
Functional Reactive Programming
http://en.wikipedia.org/wiki/Functional_reactive_programming
Excel ……
Reactive Extensions
FRP (Haskell )
2009 .NET Reactive Extensions
UniRx
ReactiveExtensions(Rx) Unity
![Page 6: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/6.jpg)
Gartner’s Hype Cycle
2013 Application Architecture/Application Development
On the Rise - Reactive Programming
![Page 7: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/7.jpg)
Technology Radar Languages & Framework
ThoughtWorks Technology Radar July 2014
Rx ADOPT OK
TRIAL
ASSESS
HOLD
![Page 8: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/8.jpg)
Across Languages
RxJava
Netflix for Java/Scala
ReactiveCocoa
GitHub 5073 for Objective-C
RxJS/bacon.js
JavaScript
RxJS bacon.js
![Page 9: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/9.jpg)
![Page 10: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/10.jpg)
UniRx
![Page 11: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/11.jpg)
UniRx 100
Asset Store
Reactive Programming
Reactive Extensions
RxJava Wiki
![Page 12: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/12.jpg)
UniRx - Reactive Extensions for Unity
![Page 13: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/13.jpg)
Reactive Extensions(Rx) is
LINQ
C# LINQ
LINQ(to Objects) C#
Rx LINQ
LINQ
Reactive
Programming
Rx
![Page 14: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/14.jpg)
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 :)
![Page 15: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/15.jpg)
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
![Page 16: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/16.jpg)
Reactive Extensions isLINQ to EventsLINQ to Asynchronous
![Page 17: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/17.jpg)
Event is Observable Sequence
Rx
IObservable<T> time
OnTap
3 Tap
5 Tap
1 Tap
![Page 18: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/18.jpg)
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
![Page 19: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/19.jpg)
Push Event Stream
Event Processing
Interactive/Visualize
![Page 20: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/20.jpg)
Internet of Things
Push
Kinect
Oculus
Twitter Streaming
PubSub, WebSocket
Logs are Stream(Fluentd, Amazon Kinesis, Azure Event Hubs)
MonoBehaviour Update , OnMouseClick, etc...
![Page 21: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/21.jpg)
LINQ
![Page 22: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/22.jpg)
LINQ
LINQ
LINQ
![Page 23: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/23.jpg)
Better EventHandling
![Page 24: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/24.jpg)
Limitations of .NET Events
// public event Action<int> OnHitDamage;
// player.OnHitDamage += (damage) =>{
if (damage >= 1000){
// " "}
};
player.OnHitDamage -= /* */
![Page 25: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/25.jpg)
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
![Page 26: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/26.jpg)
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();
}
![Page 27: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/27.jpg)
CuringYourAsynchronousProgrammingBlues
![Page 28: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/28.jpg)
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!
![Page 29: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/29.jpg)
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
![Page 30: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/30.jpg)
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
![Page 31: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/31.jpg)
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
![Page 32: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/32.jpg)
Why can Rx apply to asynchronous?
x
IEnumerable<T>
IObservable<T>
IObservable<T> time
event
async
IE<T>
![Page 33: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/33.jpg)
Why can Rx apply to asynchronous?
x
IEnumerable<T>
IObservable<T>
IObservable<T> time
event
async
IE<T>
Orchestrate Rx
![Page 34: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/34.jpg)
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
});
![Page 35: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/35.jpg)
IObservable<T> time
IObservable<T> time
WhenAll
Observable.WhenAll(
ObservableWWW.Get(),
ObservableWWW.Get(),
ObservableWWW.Get())
Subscribe(xs => xs[0], xs[1], xs[2])
![Page 36: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/36.jpg)
Conclusion
![Page 37: Reactive Programming by UniRx for Asynchronous & Event Processing](https://reader034.vdocuments.net/reader034/viewer/2022050619/540d67588d7f728d7e8b48fb/html5/thumbnails/37.jpg)
Reactive Programming
UniRx
Available Now
GitHub - https://github.com/neuecc/UniRx/
Asset Store(FREE) – http://u3d.as/7tT
Update
(v4.4)