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

ReactiveX

 ReactiveX

We will understand what is Reactive Streams and how helpful it is to deal with Async Task

Srikrishna

March 09, 2018
Tweet

More Decks by Srikrishna

Other Decks in Technology

Transcript

  1. Jokes Apart. Why nobody cared ? - Rx loves functional

    style - Lot of operators - Mind shift in thinking. Async way - Not easy to explain
  2. Asynchronous Programing - Subscribing to a JMS queue - Application

    life cycle hooks. - Event listeners $.get(“localhost:8080", (response)=>{}); $("button").click(function(){log("Click!"}); simplest program you could ever written in Async.
  3. Parallel Async Race Conditions (auto complete) Memory Leaks (event listener)

    Complex State Machines (auto complete) Uncaught Async Errors (callback hell) Async
  4. Functional Programing - list.forEach(System.out::println) $("button").click(()=>alert("Yes! you did it.")); If you

    ever written a program like this, $.get(“localhost:8080", (response)=>{});
  5. Let’s talk about iterators In computer programming, an iterator is

    an object that enables a programmer to traverse a container
  6. Facebook: Get all the friends of a given user who

    is working in the same company and sort by names ? Lets understand by a problem
  7. public List<User> getFriendsByWork(String userId){ List<User> friends = new LinkedList<>(); List<String>

    friendIds = userService.getFriends(userId); for(int ii = 0;ii<friendIds.size();ii++){ String friendId = friendIds.get(ii); User user = userService.getUser(friendId); if(user.company == "sella"){ friends.add(user); } } Collections.sort(friends); return friends; }
  8. public List<User> getFriendsByWork(String userId){ Set<User> friends = new HashSet<>(); Set<String>

    friendIds = userService.getFriends(userId); Iterator<String> iterator = friendIds.iterator(); while(iterator.hasNext()){ String id = iterator.next(); User user = userService.getUser(friendId); if(user.company == "sella"){ friends.add(user); } } List<User> temp = toList(friends); Collections.sort(temp); return temp; } {
  9. public List<User> getFriendsByWork(String userId){ Set<User> friends = new HashSet<>(); Set<String>

    friendIds = userService.getFriends(userId); for(String friendId: friendIds){ User user = userService.getUser(friendId); if(user.company == "sella"){ friends.add(user); } } List<User> temp = toList(friends); Collections.sort(temp); return temp; } {
  10. List<User> users; user.forEach(System.out::println); void forEach(Consumer<? super T> action) { Objects.requireNonNull(action);

    for (T t : this) { action.accept(t); } } public interface Consumer<T> { void accept(T t); }
  11. public List<User> getFriendsByWork(String userId){ Set<User> friends = new HashSet<>(); Set<String>

    friendIds = userService.getFriends(userId); Iterator<String> iterator = friendIds.iterator(); while(iterator.hasNext()){ String id = iterator.next(); User user = userService.getUser(friendId); if(user.company == "sella"){ friends.add(user); } } List<User> temp = toList(friends); return temp; } Before Imperative Programing
  12. Stream API is nothing more then a Internal Iteration operators

    to solve iteration problems in functional style
  13. Observer Pattern The observer pattern is a software design pattern

    in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems, in "event driven" software. - wikipedia
  14. Observer Pattern Ex: - Subscribing to a JMS queue in

    Java - Application life cycle hooks. - Event listeners $.get(“localhost:8080", (response)=>{}); $("button").click(function(){log("Click!"});
  15. Iterator Observer Arrives On Time or Static Arrives Over time

    Collection or Container Events Pull Push next(), hasNext(), throw error(); Only next() Diff: Iterator vs. Observer Finite Infinite
  16. - collection that arrives over time ? - stream of

    data that can be finite? - has onComplete() notification ? - has onError() notification? - can have operators like Stream API What if we consider Observer as…
  17. That is where an Observable Born var source$ = Observable.create(

    (observer) => { observer.next(data); observer.complete(); observer.error(error); }); source$ .map().filter().sort() .subscribe(onNext, onError, onComplete);
  18. So Many Push/Async APIs can be unified DOM Events Websockets

    Pub/Sub Database API Rest Calls I/O Streams
  19. No surprise if you are wondering how this works. Lets

    see some more operators before jumping into it