Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Programming by UniRx for Asynchronous ...

Yoshifumi Kawai
July 30, 2014
63

Reactive Programming by UniRx for Asynchronous & Event Processing

Yoshifumi Kawai

July 30, 2014
Tweet

More Decks by Yoshifumi Kawai

Transcript

  1. 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
  2. Across Languages RxJava Netflix for Java/Scala ReactiveCocoa GitHub 5073 for

    Objective-C RxJS/bacon.js JavaScript RxJS bacon.js
  3. 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 :)
  4. 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
  5. 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
  6. Internet of Things Push Kinect Oculus Twitter Streaming PubSub, WebSocket

    Logs are Stream(Fluentd, Amazon Kinesis, Azure Event Hubs) MonoBehaviour Update , OnMouseClick, etc...
  7. Limitations of .NET Events // public event Action<int> OnHitDamage; //

    player.OnHitDamage += (damage) => { if (damage >= 1000) { // " " } }; player.OnHitDamage -= /* */
  8. 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
  9. Lifecycle Resource Management // Disposable CompositeDisposable 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(); }
  10. 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!
  11. 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
  12. 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
  13. 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
  14. 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)); // google Debug.Log(xs[1].Substring(0, 100)); // bing Debug.Log(xs[2].Substring(0, 100)); // unity });