$30 off During Our Annual Pro Sale. View Details »

Reactive Programming by UniRx for Asynchronous & Event Processing

Yoshifumi Kawai
July 30, 2014
33

Reactive Programming by UniRx for Asynchronous & Event Processing

Yoshifumi Kawai

July 30, 2014
Tweet

More Decks by Yoshifumi Kawai

Transcript

  1. Reactive Programming by UniRx
    for Asynchronous & Event Processing
    2014/07/30
    Yoshifumi Kawai - @neuecc

    View Slide

  2. 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

    View Slide

  3. What’s Reactive Programming?

    View Slide

  4. Functional Reactive Programming
    http://en.wikipedia.org/wiki/Functional_reactive_programming
    Excel ……
    Reactive Extensions
    FRP (Haskell )
    2009 .NET Reactive Extensions

    View Slide

  5. Functional Reactive Programming
    http://en.wikipedia.org/wiki/Functional_reactive_programming
    Excel ……
    Reactive Extensions
    FRP (Haskell )
    2009 .NET Reactive Extensions
    UniRx
    ReactiveExtensions(Rx) Unity

    View Slide

  6. Gartner’s Hype Cycle
    2013 Application Architecture/Application Development
    On the Rise - Reactive Programming

    View Slide

  7. Technology Radar Languages & Framework
    ThoughtWorks Technology Radar July 2014
    Rx ADOPT OK
    TRIAL
    ASSESS
    HOLD

    View Slide

  8. Across Languages
    RxJava
    Netflix for Java/Scala
    ReactiveCocoa
    GitHub 5073 for Objective-C
    RxJS/bacon.js
    JavaScript
    RxJS bacon.js

    View Slide

  9. View Slide

  10. UniRx

    View Slide

  11. UniRx 100
    Asset Store
    Reactive Programming
    Reactive Extensions
    RxJava Wiki

    View Slide

  12. UniRx - Reactive Extensions for Unity

    View Slide

  13. Reactive Extensions(Rx) is
    LINQ
    C# LINQ
    LINQ(to Objects) C#
    Rx LINQ
    LINQ
    Reactive
    Programming
    Rx

    View Slide

  14. 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 :)

    View Slide

  15. 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

    View Slide

  16. Reactive Extensions
    is
    LINQ to Events
    LINQ to Asynchronous

    View Slide

  17. Event is Observable Sequence
    Rx
    IObservable time
    OnTap
    3 Tap
    5 Tap
    1 Tap

    View Slide

  18. IEnumerable <-> IObservable
    IObservable time
    IEnumerable length
    IEnumerable length
    .Where(x => x % 2 == 0)
    IObservable time
    .Where(x => x % 2 == 0)
    Where
    LINQ

    View Slide

  19. Push Event Stream
    Event Processing
    Interactive/Visualize

    View Slide

  20. Internet of Things
    Push
    Kinect
    Oculus
    Twitter Streaming
    PubSub, WebSocket
    Logs are Stream(Fluentd, Amazon Kinesis, Azure Event Hubs)
    MonoBehaviour Update , OnMouseClick, etc...

    View Slide

  21. LINQ

    View Slide

  22. LINQ
    LINQ
    LINQ

    View Slide

  23. Better EventHandling

    View Slide

  24. Limitations of .NET Events
    //
    public event Action OnHitDamage;
    //
    player.OnHitDamage += (damage) =>
    {
    if (damage >= 1000)
    {
    // " "
    }
    };
    player.OnHitDamage -= /* */

    View Slide

  25. Observable Sequence to the Rescue
    IObservable onHitDamage = player.OnHitDamage;
    var criticalHit = onHitDamage
    .Where(x =>x >= 1000);
    var subscription = criticalHit.Subscribe(damage => /* ... */);
    subscription.Dispose();
    LINQ

    View Slide

  26. 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();
    }

    View Slide

  27. Curing
    Your
    Asynchronous
    Programming
    Blues

    View Slide

  28. 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!

    View Slide

  29. 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

    View Slide

  30. IEnumerator GetGoogle(Action onCompleted, Action 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

    View Slide

  31. 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

    View Slide

  32. Why can Rx apply to asynchronous?
    x
    IEnumerable
    IObservable
    IObservable time
    event
    async
    IE

    View Slide

  33. Why can Rx apply to asynchronous?
    x
    IEnumerable
    IObservable
    IObservable time
    event
    async
    IE
    Orchestrate Rx

    View Slide

  34. 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
    });

    View Slide

  35. IObservable time
    IObservable time
    WhenAll
    Observable.WhenAll(
    ObservableWWW.Get(),
    ObservableWWW.Get(),
    ObservableWWW.Get())
    Subscribe(xs => xs[0], xs[1], xs[2])

    View Slide

  36. Conclusion

    View Slide

  37. Reactive Programming
    UniRx
    Available Now
    GitHub - https://github.com/neuecc/UniRx/
    Asset Store(FREE) – http://u3d.as/7tT
    Update
    (v4.4)

    View Slide