Tech Talk #4 : RxJava and Using RxJava in MVP - Dương Văn Tới
RxJS vs RxJava: Intro
-
Upload
martin-toshev -
Category
Technology
-
view
750 -
download
0
Transcript of RxJS vs RxJava: Intro
![Page 1: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/1.jpg)
RxJS vs RxJavaIntro
![Page 2: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/2.jpg)
Agenda
• Reactive Programming• Functional Reactive Programming• RxJS• RxJava
![Page 3: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/3.jpg)
Reactive Programming
![Page 4: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/4.jpg)
Reactive Programming
• In imperative programming languages expressions are evaluated “in place”:
a = 10;b = 20;c = a + b; // c = 30a = 40; // c = 30
![Page 5: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/5.jpg)
Reactive Programming
• In reactive programming languages changes in the data flow may trigger changes to multiple variables:
a = 10;b = 20;c = a + b; // c = 30a = 40; // c = 60
![Page 6: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/6.jpg)
Reactive Programming
• Reactive programming may also refer to the interaction between different systems or components in a system
• In that regard the libraries we will be discussing do not serve as a replacement of message brokers (such as RabbitMQ, Kafka, MSMQ and a number of others)
![Page 7: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/7.jpg)
Reactive Programming
• In this clash we would refer to the “programming language” - inferred meaning behind reactive programming
![Page 8: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/8.jpg)
Reactive Programming
• The Reactive Manifesto:
![Page 9: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/9.jpg)
Functional Reactive Programming
![Page 10: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/10.jpg)
Functional Reactive Programming
• A more common subset is functional reactive programming (FRP) that makes use of streams and stream operations for anything (input, changes in variables and many more) in order to achieve reactiveness
• Connal Elliot – ground-breaking paper @Conference on Functional Programming, 1997
![Page 11: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/11.jpg)
Functional Reactive Programming
•Reactive Programming = Programming with Asynchronous Data Streams
•Functional Reactive Programming (FRP) = programming paradigm for reactive programming that uses the building blocks of functional programming (e.g. map, reduce, filter). FRP has been used for programming graphical user interfaces (GUIs), robotics, and music, aiming to simplify these problems by explicitly modeling time. [Wikipedia]
![Page 12: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/12.jpg)
Functional Reactive Programming
•Reactive Programming = Programming with Asynchronous Data Streams
•Functional Reactive Programming (FRP) = programming paradigm for reactive programming that uses the building blocks of functional programming (e.g. map, reduce and filter)
![Page 13: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/13.jpg)
Functional Reactive Programming
• FRP has been used in:
– responsive graphical user interfaces (GUIs)
– robotics
– the music industry
![Page 14: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/14.jpg)
Functional Reactive Programming
• RxJava and RxJS implement the functional reactive programming paradigm in terms of libraries for the Java and JavaScript programming languages correspondingly …
• Derived on the basis of Microsoft’s Reactive Extensions (Rx) library for .NET
![Page 15: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/15.jpg)
Functional Reactive Programming
Microsoft® open source polyglot project ReactiveX (Reactive Extensions) [http://reactivex.io]: Rx = Observables + LINQ + Schedulers :)• Supported Languages – Java: RxJava, JavaScript: RxJS, C#:
Rx.NET, C#(Unity): UniRx, Scala: RxScala, Clojure: RxClojure, C++: RxCpp, Ruby: Rx.rb, Python: RxPY, Groovy: RxGroovy, JRuby: RxJRuby, Kotlin: RxKotlin, Swift: RxSwift
• ReactiveX for platforms and frameworks: RxNetty, RxAndroid, RxCocoa
![Page 16: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/16.jpg)
Functional Reactive Programming
• Rx has an implicit contract that must be followed:
event source
OnNext(item) OnNext(item) OnNext(item)
OnCompleted()
OnError(Exception)
![Page 17: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/17.jpg)
RxJS
![Page 18: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/18.jpg)
RxJsRx.Observable.from(["Reactive", "Extensions", "Java“])
.take(2) .map(function(s) { s + " : on " + new Date()})
.subscribe(function(s) {console.log(s)});
Result: Reactive : on Wed Jun 17 21:54:02 GMT+02:00 2015
Extensions : on Wed Jun 17 21:54:02 GMT+02:00 2015
![Page 19: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/19.jpg)
RxJava
![Page 20: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/20.jpg)
RxJava
Observable.from(new String[] {"Reactive", "Extensions", "Java"})
.take(2) .map(s -> s + " : on " + new Date()) .subscribe(s -> System.out.println(s));
Result: Reactive : on Wed Jun 17 21:54:02 GMT+02:00 2015
Extensions : on Wed Jun 17 21:54:02 GMT+02:00 2015
![Page 21: RxJS vs RxJava: Intro](https://reader035.fdocuments.net/reader035/viewer/2022062310/5872a1001a28ab07208b5baf/html5/thumbnails/21.jpg)
RxJava
• Lambdas and streams introduced in Java 8 make the FRP style of programming in Java easier to accomplish
• There is a proposal for introducing a reactive streaming API as part of the core libraries in JDK 9