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

Demystifying RxJava Subscribers (Øredev 2015)

Demystifying RxJava Subscribers (Øredev 2015)

RxJava is a powerful library for creating and composing streams of data. It can quickly be used to great effect, but a deeper understand of its internals will prevent running into pitfalls later on.

This talk will focus on the core mechanism of how streams are created and observed: subscribers and subscriptions. We will start with an introduction to the contract of the Subscriber type and how it is used by sources to create streams. Then we will touch on operators and how they use subscribers to modify the data flowing through streams. Finally we'll look at how threading behaves in operators like subscribeOn and observeOn.

Only a very basic level of RxJava knowledge is required for this talk. It will be assumed that you have used or at least seen the basics of RxJava's API before.

Video: https://vimeo.com/144812843

Jake Wharton

November 05, 2015
Tweet

More Decks by Jake Wharton

Other Decks in Programming

Transcript

  1. Demystifying
    RxJava Subscribers
    Jake Wharton

    View full-size slide

  2. Reactive Streams

    View full-size slide

  3. Reactive Streams
    1 2 3 4 5 6

    View full-size slide

  4. Reactive Streams
    1 2 3 4 5 6
    [ ]

    View full-size slide

  5. Reactive Streams
    1 2 3 4 5 6
    [ ]
    for ( in
    i )

    View full-size slide

  6. Reactive Streams
    1 2 3 4 5 6
    [ ]
    for ( in
    i )
    scale( )
    i

    View full-size slide

  7. Reactive Streams
    1 2 3 4 5 6
    [ ]
    for ( in
    i )
    if ( % 2 == 0)
    i
    scale( )
    i

    View full-size slide

  8. Reactive Streams

    View full-size slide

  9. Reactive Streams
    1 2 3 4 5 6

    View full-size slide

  10. Reactive Streams
    1 2 3 4 5 6

    View full-size slide

  11. Reactive Streams

    View full-size slide

  12. Reactive Streams
    filter( % 2 == 0)
    i

    View full-size slide

  13. Reactive Streams
    filter( % 2 == 0)
    i

    View full-size slide

  14. Reactive Streams
    1 2 3 4 5 6
    2 4 6
    filter( % 2 == 0)
    i

    View full-size slide

  15. Reactive Streams
    1 2 3 4 5 6

    View full-size slide

  16. Observable.just( , , , , , )
    Reactive Streams
    1 2 3 4 5 6

    View full-size slide

  17. Observable.just( , , , , , )
    Reactive Streams
    1 2 3 4 5 6
    .filter( -> % 2 == 0)
    i i

    View full-size slide

  18. Observable.just( , , , , , )
    Reactive Streams
    1 2 3 4 5 6
    .filter( -> % 2 == 0)
    i i
    .subscribe( -> {
    });
    i
    2 4 6

    View full-size slide

  19. Observable.just(1, 2, 3, 4, 5, 6)

    .filter(i -> i % 2 == 0)

    .subscribe(new Action1() {

    @Override public void call(Integer item) {

    System.out.println(item);

    }

    });
    Reactive Streams
    ->
    i i
    i

    View full-size slide

  20. Subscribers?

    View full-size slide

  21. Subscribers?
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }

    View full-size slide

  22. Subscribers?
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }

    View full-size slide

  23. Subscribers?
    interface Action1 {

    void call(T t);

    }
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }

    View full-size slide

  24. Subscribers?
    interface Action1 {

    void call(T t);

    }
    // ...and friends!
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }

    View full-size slide

  25. Subscribers?
    interface Action1 {

    void call(T t);

    }
    // ...and friends!
    interface Func1 {

    R call(T t);

    }
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }

    View full-size slide

  26. Subscribers?
    interface Action1 {

    void call(T t);

    }
    // ...and friends!
    interface Func1 {

    R call(T t);

    }

    // ...and friends!
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }

    View full-size slide

  27. Subscribers?
    interface Action1 {

    void call(T t);

    }X
    // ...and friends!
    interface Func1 {

    R call(T t);

    }X

    // ...and friends!
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }X
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X
    class Observable {

    static Observable create(OnSubscribe f);

    Observable lift(Operator extends R, ? super T> operator);

    Observable compose(Transformer super T, ? extends R> transformer);

    static Observable amb(Iterable extends Observable extends T>> sources);

    static Observable amb(Observable extends T> o1, Observable extends T> o2);

    static Observable amb(Observable extends T> o1, Observable extends T> o2, Observable extends T> o3);

    static Observable amb(Observable extends T> o1, Observable extends T> o2, Observable extends T> o3, Observable extends T> o4);

    static Observable amb(Observable extends T> o1, Observable extends T> o2, Observable extends T> o3, Observable extends T> o4, Observable extends T> o5);

    static Observable amb(Observable extends T> o1, Observable extends T> o2, Observable extends T> o3, Observable extends T> o4, Observable extends T> o5, Observable extends T> o6);

    static Observable amb(Observable extends T> o1, Observable extends T> o2, Observable extends T> o3, Observable extends T> o4, Observable extends T> o5, Observable extends T> o6, Observable extends T> o7);

    static Observable amb(Observable extends T> o1, Observable extends T> o2, Observable extends T> o3, Observable extends T> o4, Observable extends T> o5, Observable extends T> o6, Observable extends T> o7, Observable extends T> o8);

    static Observable amb(Observable extends T> o1, Observable extends T> o2, Observable extends T> o3, Observable extends T> o4, Observable extends T> o5, Observable extends T> o6, Observable extends T> o7, Observable extends T> o8, Observable extends T> o9);

    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Func2 super T1, ? super T2, ? extends R> combineFunction);

    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Func3 super T1, ? super T2, ? super T3, ? extends R> combineFunction);

    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Func4 super T1, ? super T2, ? super T3, ? super T4, ? extends R> combineFunction);

    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Func5 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> combineFunction);

    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Func6 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ?
    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Observable extends T7> o7, Func7 super T1, ? super T2, ? super T3, ? sup
    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Observable extends T7> o7, Observable extends T8> o8, Func8 super
    static Observable combineLatest(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Observable extends T7> o7, Observable extends T8> o8, Observable
    static Observable combineLatest(List extends Observable extends T>> sources, FuncN extends R> combineFunction);

    static Observable concat(Observable extends Observable extends T>> observables);

    static Observable concat(Observable extends T> t1, Observable extends T> t2);

    static Observable concat(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3);

    static Observable concat(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4);

    static Observable concat(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5);

    static Observable concat(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6);

    static Observable concat(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7);

    static Observable concat(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7, Observable extends T> t8);

    static Observable concat(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7, Observable extends T> t8, Observable extends T> t9);

    static Observable defer(Func0> observableFactory);

    static Observable empty();

    static Observable error(Throwable exception);

    static Observable from(Future extends T> future);

    static Observable from(Future extends T> future, long timeout, TimeUnit unit);

    static Observable from(Future extends T> future, Scheduler scheduler);

    static Observable from(Iterable extends T> iterable);

    static Observable from(T[] array);

    static Observable interval(long interval, TimeUnit unit);

    static Observable interval(long interval, TimeUnit unit, Scheduler scheduler);

    static Observable interval(long initialDelay, long period, TimeUnit unit);

    static Observable interval(long initialDelay, long period, TimeUnit unit, Scheduler scheduler);

    static Observable just(T value);

    static Observable just(T t1, T t2);

    static Observable just(T t1, T t2, T t3);

    static Observable just(T t1, T t2, T t3, T t4);

    static Observable just(T t1, T t2, T t3, T t4, T t5);

    static Observable just(T t1, T t2, T t3, T t4, T t5, T t6);

    static Observable just(T t1, T t2, T t3, T t4, T t5, T t6, T t7);

    static Observable just(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8);

    static Observable just(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9);

    static Observable just(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9, T t10);

    static Observable merge(Iterable extends Observable extends T>> sequences);

    static Observable merge(Iterable extends Observable extends T>> sequences, int maxConcurrent);

    static Observable merge(Observable extends Observable extends T>> source);

    static Observable merge(Observable extends Observable extends T>> source, int maxConcurrent);

    static Observable merge(Observable extends T> t1, Observable extends T> t2);

    static Observable merge(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3);

    static Observable merge(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4);

    static Observable merge(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5);

    static Observable merge(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6);

    static Observable merge(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7);

    static Observable merge(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7, Observable extends T> t8);

    static Observable merge(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7, Observable extends T> t8, Observable extends T> t9);

    static Observable merge(Observable extends T>[] sequences);

    static Observable merge(Observable extends T>[] sequences, int maxConcurrent);

    static Observable mergeDelayError(Observable extends Observable extends T>> source);

    static Observable mergeDelayError(Observable extends Observable extends T>> source, int maxConcurrent);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7, Observable extends T> t8);

    static Observable mergeDelayError(Observable extends T> t1, Observable extends T> t2, Observable extends T> t3, Observable extends T> t4, Observable extends T> t5, Observable extends T> t6, Observable extends T> t7, Observable extends T> t8, Observable extends T> t9);

    Observable> nest();

    static Observable never();

    static Observable range(int start, int count);

    static Observable range(int start, int count, Scheduler scheduler);

    static Observable sequenceEqual(Observable extends T> first, Observable extends T> second);

    static Observable sequenceEqual(Observable extends T> first, Observable extends T> second, Func2 super T, ? super T, Boolean> equality);

    static Observable switchOnNext(Observable extends Observable extends T>> sequenceOfSequences);

    static Observable timer(long delay, TimeUnit unit);

    static Observable timer(long delay, TimeUnit unit, Scheduler scheduler);

    static Observable using(Func0 resourceFactory, Func1 super Resource, ? extends Observable extends T>> observableFactory, Action1 super Resource> disposeAction);

    static Observable using(Func0 resourceFactory, Func1 super Resource, ? extends Observable extends T>> observableFactory, Action1 super Resource> disposeAction, boolean disposeEagerly);

    static Observable zip(Iterable extends Observable>> ws, FuncN extends R> zipFunction);

    static Observable zip(Observable extends Observable>> ws, FuncN extends R> zipFunction);

    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Func2 super T1, ? super T2, ? extends R> zipFunction);

    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Func3 super T1, ? super T2, ? super T3, ? extends R> zipFunction);

    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Func4 super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipFunction);

    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Func5 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> zipFunction);

    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Func6 super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R>
    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Observable extends T7> o7, Func7 super T1, ? super T2, ? super T3, ? super T4, ? s
    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Observable extends T7> o7, Observable extends T8> o8, Func8 super T1, ? supe
    static Observable zip(Observable extends T1> o1, Observable extends T2> o2, Observable extends T3> o3, Observable extends T4> o4, Observable extends T5> o5, Observable extends T6> o6, Observable extends T7> o7, Observable extends T8> o8, Observable extends

    View full-size slide

  28. Subscribers?
    interface Action1 {

    void call(T t);

    }X
    // ...and friends!
    interface Func1 {

    R call(T t);

    }X

    // ...and friends!
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }X
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X
    Observable share();

    Observable single();

    Observable single(Func1 super T, Boolean> predicate);

    Observable singleOrDefault(T defaultValue);

    Observable singleOrDefault(T defaultValue, Func1 super T, Boolean> predicate);

    Observable skip(int count);

    Observable skip(long time, TimeUnit unit);

    Observable skip(long time, TimeUnit unit, Scheduler scheduler);

    Observable skipLast(int count);

    Observable skipLast(long time, TimeUnit unit);

    Observable skipLast(long time, TimeUnit unit, Scheduler scheduler);

    Observable skipUntil(Observable other);

    Observable skipWhile(Func1 super T, Boolean> predicate);

    Observable startWith(Observable values);

    Observable startWith(Iterable values);

    Observable startWith(T t1);

    Observable startWith(T t1, T t2);

    Observable startWith(T t1, T t2, T t3);

    Observable startWith(T t1, T t2, T t3, T t4);

    Observable startWith(T t1, T t2, T t3, T t4, T t5);

    Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6);

    Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7);

    Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8);

    Observable startWith(T t1, T t2, T t3, T t4, T t5, T t6, T t7, T t8, T t9);

    Subscription subscribe();

    Subscription subscribe(Action1 super T> onNext);

    Subscription subscribe(Action1 super T> onNext, Action1 onError);

    Subscription subscribe(Action1 super T> onNext, Action1 onError, Action0 onComplete);

    Subscription subscribe(Observer super T> observer);

    Subscription unsafeSubscribe(Subscriber super T> subscriber);

    Subscription subscribe(Subscriber super T> subscriber);

    Observable subscribeOn(Scheduler scheduler);

    Observable switchMap(Func1 super T, ? extends Observable extends R>> func);

    Observable take(int count);

    Observable take(long time, TimeUnit unit);

    Observable take(long time, TimeUnit unit, Scheduler scheduler);

    Observable takeFirst(Func1 super T, Boolean> predicate);

    Observable takeLast(int count);

    Observable takeLast(int count, long time, TimeUnit unit);

    Observable takeLast(int count, long time, TimeUnit unit, Scheduler scheduler);

    Observable takeLast(long time, TimeUnit unit);

    Observable takeLast(long time, TimeUnit unit, Scheduler scheduler);

    Observable> takeLastBuffer(int count);

    Observable> takeLastBuffer(int count, long time, TimeUnit unit);

    Observable> takeLastBuffer(int count, long time, TimeUnit unit, Scheduler scheduler);

    Observable> takeLastBuffer(long time, TimeUnit unit);

    Observable> takeLastBuffer(long time, TimeUnit unit, Scheduler scheduler);

    Observable takeUntil(Observable extends E> other);

    Observable takeWhile(Func1 super T, Boolean> predicate);

    Observable takeUntil(Func1 super T, Boolean> stopPredicate);

    Observable throttleFirst(long windowDuration, TimeUnit unit);

    Observable throttleFirst(long skipDuration, TimeUnit unit, Scheduler scheduler);

    Observable throttleLast(long intervalDuration, TimeUnit unit);

    Observable throttleLast(long intervalDuration, TimeUnit unit, Scheduler scheduler);

    Observable throttleWithTimeout(long timeout, TimeUnit unit);

    Observable throttleWithTimeout(long timeout, TimeUnit unit, Scheduler scheduler);

    Observable> timeInterval();

    Observable> timeInterval(Scheduler scheduler);

    Observable timeout(Func0 extends Observable> firstTimeoutSelector, Func1 super T, ? extends Observable> timeoutSelector);

    Observable timeout(Func0 extends Observable> firstTimeoutSelector, Func1 super T, ? extends Observable> timeoutSelector, Observable extends T> other);

    Observable timeout(Func1 super T, ? extends Observable> timeoutSelector);

    Observable timeout(Func1 super T, ? extends Observable> timeoutSelector, Observable extends T> other);

    Observable timeout(long timeout, TimeUnit timeUnit);

    Observable timeout(long timeout, TimeUnit timeUnit, Observable extends T> other);

    Observable timeout(long timeout, TimeUnit timeUnit, Observable extends T> other, Scheduler scheduler);

    Observable timeout(long timeout, TimeUnit timeUnit, Scheduler scheduler);

    Observable> timestamp();

    Observable> timestamp(Scheduler scheduler);

    BlockingObservable toBlocking();

    Observable> toList();

    Observable> toMap(Func1 super T, ? extends K> keySelector);

    Observable> toMap(Func1 super T, ? extends K> keySelector, Func1 super T, ? extends V> valueSelector);

    Observable> toMap(Func1 super T, ? extends K> keySelector, Func1 super T, ? extends V> valueSelector, Func0 extends Map> mapFactory);

    Observable>> toMultimap(Func1 super T, ? extends K> keySelector);

    Observable>> toMultimap(Func1 super T, ? extends K> keySelector, Func1 super T, ? extends V> valueSelector);

    Observable>> toMultimap(Func1 super T, ? extends K> keySelector, Func1 super T, ? extends V> valueSelector, Func0 extends Map>> mapFactory);

    Observable>> toMultimap(Func1 super T, ? extends K> keySelector, Func1 super T, ? extends V> valueSelector, Func0 extends Map>> mapFactory, Func1 super K, ? extends Collection> collectionFactory);

    Observable> toSortedList();

    Observable> toSortedList(Func2 super T, ? super T, Integer> sortFunction);

    Observable> toSortedList(int initialCapacity);

    Observable> toSortedList(Func2 super T, ? super T, Integer> sortFunction, int initialCapacity);

    Observable unsubscribeOn(Scheduler scheduler);

    Observable withLatestFrom(Observable extends U> other, Func2 super T, ? super U, ? extends R> resultSelector);

    Observable> window(Func0 extends Observable extends TClosing>> closingSelector);

    Observable> window(int count);

    Observable> window(int count, int skip);

    Observable> window(long timespan, long timeshift, TimeUnit unit);

    Observable> window(long timespan, long timeshift, TimeUnit unit, Scheduler scheduler);

    Observable> window(long timespan, long timeshift, TimeUnit unit, int count, Scheduler scheduler);

    Observable> window(long timespan, TimeUnit unit);

    Observable> window(long timespan, TimeUnit unit, int count);

    Observable> window(long timespan, TimeUnit unit, int count, Scheduler scheduler);

    Observable> window(long timespan, TimeUnit unit, Scheduler scheduler);

    Observable> window(Observable extends TOpening> windowOpenings, Func1 super TOpening, ? extends Observable extends TClosing>> closingSelector);

    Observable> window(Observable boundary);

    Observable zipWith(Iterable extends T2> other, Func2 super T, ? super T2, ? extends R> zipFunction);

    Observable zipWith(Observable extends T2> other, Func2 super T, ? super T2, ? extends R> zipFunction);

    }

    View full-size slide

  29. Subscribers?
    interface Action1 {

    void call(T t);

    }X
    // ...and friends!
    interface Func1 {

    R call(T t);

    }X

    // ...and friends!
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }X
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X
    class Observable {
    Observable(OnSubscribe func)

    // LOL # of methods...
    }

    View full-size slide

  30. Subscribers?
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }
    class Observable {
    Observable(OnSubscribe func)

    // LOL # of methods...
    }

    View full-size slide

  31. Subscribers?
    interface Observer {

    void onNext(T item);

    void onCompleted();

    void onError(Throwable t);

    }
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }

    View full-size slide

  32. Subscribers?
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X

    View full-size slide

  33. Subscribers?
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X

    View full-size slide

  34. Subscribers?
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X
    Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();

    View full-size slide

  35. Subscribers?
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X
    Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe(new Action1() {

    @Override public void call(String item) {

    System.out.println("ITEM: " + item);

    }X

    });

    View full-size slide

  36. Subscribers?
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X
    Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe(new Observer() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }X
    @Override public void onError(Throwable e) {
    e.printStackTrace();
    }X

    @Override public void onCompleted() {}

    });

    Action1

    call

    View full-size slide

  37. Subscribers?
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }X
    Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();

    View full-size slide

  38. Subscribers?
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }
    Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);

    View full-size slide

  39. Subscribers!
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }
    Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19

    View full-size slide

  40. Subscribers!
    interface Subscription {

    void unsubscribe();

    boolean isUnsubscribed();

    }
    Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19

    View full-size slide

  41. /**

    * SafeSubscriber is a wrapper around Subscriber that ensures that
    * the Subscriber complies with the Observable contract.

    */

    class SafeSubscriber extends Subscriber {

    SafeSubscriber(Subscriber super T> actual);


    Subscriber super T> getActual();

    }

    View full-size slide

  42. /**

    * SafeSubscriber is a wrapper around Subscriber that ensures that
    * the Subscriber complies with the Observable contract.

    */

    class SafeSubscriber extends Subscriber {

    SafeSubscriber(Subscriber super T> actual);


    Subscriber super T> getActual();

    }

    View full-size slide

  43. /**

    * SafeSubscriber is a wrapper around Subscriber that ensures that
    * the Subscriber complies with the Observable contract.

    */

    class SafeSubscriber extends Subscriber {

    SafeSubscriber(Subscriber super T> actual);


    Subscriber super T> getActual();

    }

    View full-size slide

  44. /**

    * Provides a mechanism for receiving push-based notifications
    * from Observables, and permits manual unsubscribing from these
    * Observables.

    */

    abstract class Subscriber implements Observer, Subscription {

    Subscriber();

    Subscriber(Subscriber> subscriber);


    void add(Subscription s);


    void onStart();

    void setProducer(Producer p);

    protected void request(long n);

    }

    View full-size slide

  45. /**

    * Provides a mechanism for receiving push-based notifications
    * from Observables, and permits manual unsubscribing from these
    * Observables.

    */

    abstract class Subscriber implements Observer, Subscription {

    Subscriber();

    Subscriber(Subscriber> subscriber);


    void add(Subscription s);


    void onStart();

    void setProducer(Producer p);

    protected void request(long n);

    }

    View full-size slide

  46. /**

    * Provides a mechanism for receiving push-based notifications
    * from Observables, and permits manual unsubscribing from these
    * Observables.

    */

    abstract class Subscriber implements Observer, Subscription {

    Subscriber();

    Subscriber(Subscriber> subscriber);


    void add(Subscription s);


    void onStart();

    void setProducer(Producer p);

    protected void request(long n);

    }

    View full-size slide

  47. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19

    View full-size slide

  48. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    View full-size slide

  49. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  50. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > rx.Observable$26@6d311334

    View full-size slide

  51. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > rx.Observable$26@6d311334

    View full-size slide

  52. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > rx.Observable$26@6d311334

    View full-size slide

  53. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > rx.Observable$26@6d311334

    View full-size slide

  54. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > rx.Observable$26@6d311334

    View full-size slide

  55. public Subscription subscribe() {

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    // do nothing

    }


    @Override public void onCompleted() {

    // do nothing

    }


    @Override public void onError(Throwable e) {

    throw new OnErrorNotImplementedException(e);

    }

    });

    }

    View full-size slide

  56. public Subscription subscribe() {

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    // do nothing

    }


    @Override public void onCompleted() {

    // do nothing

    }


    @Override public void onError(Throwable e) {

    throw new OnErrorNotImplementedException(e);

    }

    });

    }

    View full-size slide

  57. public Subscription subscribe() {

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    // do nothing

    }X


    @Override public void onCompleted() {

    // do nothing

    }X

    @Override public void onError(Throwable e) {

    throw new OnErrorNotImplementedException(e);

    }X

    });

    }X

    View full-size slide

  58. public Subscription subscribe(Action1 super T> onNext) {

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    onNext.call(args);

    }X


    @Override public void onCompleted() {

    // do nothing

    }X


    @Override public void onError(Throwable e) {

    throw new OnErrorNotImplementedException(e);

    }X

    });

    }X



    // do nothing

    View full-size slide

  59. public Subscription subscribe(Action1 super T> onNext) {

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    onNext.call(args);

    }


    @Override public void onCompleted() {

    // do nothing

    }


    @Override public void onError(Throwable e) {

    throw new OnErrorNotImplementedException(e);

    }

    });

    }

    View full-size slide

  60. public Subscription subscribe(Action1 super T> onNext) {

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    onNext.call(args);

    }


    @Override public void onCompleted() {

    // do nothing

    }


    @Override public void onError(Throwable e) {

    throw new OnErrorNotImplementedException(e);

    }

    });

    }

    View full-size slide

  61. public Subscription subscribe(Action1 super T> onNext) {

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    onNext.call(args);

    }X


    @Override public void onCompleted() {

    // do nothing

    }X


    @Override public void onError(Throwable e) {

    throw new OnErrorNotImplementedException(e);

    }X

    });

    }X

    View full-size slide

  62. public Subscription subscribe(Observer super T> observer) {

    if (observer instanceof Subscriber) {

    return subscribe((Subscriber super T>) observer);

    }

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    observer.onNext(args);

    }X

    @Override public void onCompleted() {

    observer.onCompleted();

    }X

    @Override public void onError(Throwable e) {

    observer.onError(e);

    }X

    });

    }X
    Action1 super T> onNext



    onNext.call(args);




    // do nothing




    throw new OnErrorNotImplementedException(e);

    View full-size slide

  63. public Subscription subscribe(Observer super T> observer) {

    if (observer instanceof Subscriber) {

    return subscribe((Subscriber super T>) observer);

    }

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    observer.onNext(args);

    }

    @Override public void onCompleted() {

    observer.onCompleted();

    }

    @Override public void onError(Throwable e) {

    observer.onError(e);

    }

    });

    }

    View full-size slide

  64. public Subscription subscribe(Observer super T> observer) {

    if (observer instanceof Subscriber) {

    return subscribe((Subscriber super T>) observer);

    }

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    observer.onNext(args);

    }

    @Override public void onCompleted() {

    observer.onCompleted();

    }

    @Override public void onError(Throwable e) {

    observer.onError(e);

    }

    });

    }

    View full-size slide

  65. public Subscription subscribe(Observer super T> observer) {

    if (observer instanceof Subscriber) {

    return subscribe((Subscriber super T>) observer);

    }

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    observer.onNext(args);

    }

    @Override public void onCompleted() {

    observer.onCompleted();

    }

    @Override public void onError(Throwable e) {

    observer.onError(e);

    }

    });

    }

    View full-size slide

  66. public Subscription subscribe(Observer super T> observer) {

    if (observer instanceof Subscriber) {

    return subscribe((Subscriber super T>) observer);

    }

    return subscribe(new Subscriber() {

    @Override public void onNext(T args) {

    observer.onNext(args);

    }X

    @Override public void onCompleted() {

    observer.onCompleted();

    }X

    @Override public void onError(Throwable e) {

    observer.onError(e);

    }X

    });

    }X

    View full-size slide

  67. Observable.subscribe(Observer super T>)
    Observable.subscribe(Action1 super T>)
    Observable.subscribe()

    View full-size slide

  68. Observable.subscribe()
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Observer super T>)
    ↳ Observable.subscribe(Subscriber super T>)

    View full-size slide

  69. Observable.subscribe(Action1 super T>, Action1)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>, Action1, Action0)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Observer super T>)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe()
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>)
    ↳ Observable.subscribe(Subscriber super T>)

    View full-size slide

  70. Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>, Action1)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>, Action1, Action0)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Observer super T>)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe()
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>)
    ↳ Observable.subscribe(Subscriber super T>)

    View full-size slide

  71. Observable.subscribe(Subscriber super T>)
    ↳ ???
    Observable.subscribe(Action1 super T>, Action1)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>, Action1, Action0)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Observer super T>)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe()
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>)
    ↳ Observable.subscribe(Subscriber super T>)

    View full-size slide

  72. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    System.out.println(s);
    > rx.observers.SafeSubscriber@2f2c9b19
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > rx.Observable$26@6d311334

    View full-size slide

  73. Observable o = Observable.just("Hi!");

    Subscription s = o.subscribe();
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  74. Observable o = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    Subscription s = o.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  75. Observable o = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }X

    @Override public void onError(Throwable e) {}X

    @Override public void onCompleted() {}X

    };
    Subscription s = o.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  76. Observable o = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }X

    @Override public void onError(Throwable e) {}X

    @Override public void onCompleted() {}X

    };
    System.out.println(in);
    Subscription s = o.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  77. Observable o = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = o.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  78. Observable o = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = o.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > Main$1@eed1f14
    > ITEM: Hi!
    > Main$1@eed1f14

    View full-size slide

  79. Observations

    View full-size slide

  80. Observations
    • The consumer of a stream is always a Subscriber.

    View full-size slide

  81. Observations
    • The consumer of a stream is always a Subscriber.
    • Might be wrapping an Observer, Action1, or just empty.

    View full-size slide

  82. Observations
    • The consumer of a stream is always a Subscriber.
    • Might be wrapping an Observer, Action1, or just empty.
    • Always wrapped in SafeSubscriber to ensure contract.

    View full-size slide

  83. subscribe()
    Subscriber
    Nothing

    View full-size slide

  84. Subscriber
    Nothing
    Action1

    View full-size slide

  85. Subscriber
    Nothing
    Action1
    subscribe()

    View full-size slide

  86. Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  87. subscribe()
    Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  88. SafeSubscriber
    subscribe()
    Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  89. SafeSubscriber
    Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  90. SafeSubscriber
    Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  91. Observable.subscribe(Subscriber super T>)
    ↳ ???
    Observable.subscribe()
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>, Action1)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Action1 super T>, Action1, Action0)
    ↳ Observable.subscribe(Subscriber super T>)
    Observable.subscribe(Observer super T>)
    ↳ Observable.subscribe(Subscriber super T>)

    View full-size slide

  92. Observable.subscribe(Subscriber super T>)
    ↳ ???

    View full-size slide

  93. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    }X

    View full-size slide

  94. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }Y
    }X

    View full-size slide

  95. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }Y
    onSubscribe.call(subscriber);
    }X

    View full-size slide

  96. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }Y
    onSubscribe.call(subscriber);
    return subscriber;
    }X

    View full-size slide

  97. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }X
    onSubscribe.call(subscriber);
    return subscriber;
    }X
    class Observable {
    Observable(OnSubscribe func)

    // LOL # of methods...
    }

    View full-size slide

  98. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }X
    onSubscribe.call(subscriber);
    return subscriber;
    }X
    class Observable {
    Observable(OnSubscribe func)

    // LOL # of methods...
    }

    View full-size slide

  99. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }X
    onSubscribe.call(subscriber);
    return subscriber;
    }X
    class Observable {
    Observable(OnSubscribe func)

    // LOL # of methods...
    }

    View full-size slide

  100. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }X
    onSubscribe.call(subscriber);
    return subscriber;
    }X
    interface OnSubscribe extends Action1> {

    }

    View full-size slide

  101. /**

    * Subscribes to an Observable and provides a Subscriber that
    * implements functions to handle the items the Observable emits
    * and any error or completion notification it issues.

    */

    public Subscription subscribe(Subscriber super T> subscriber) {

    if (!(subscriber instanceof SafeSubscriber)) {

    subscriber = new SafeSubscriber(subscriber);

    }X
    onSubscribe.call(subscriber);
    return subscriber;
    }X
    Action1>

    View full-size slide

  102. SafeSubscriber
    Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  103. SafeSubscriber
    Subscriber
    Nothing
    Action1
    Observer
    Action1>

    View full-size slide

  104. SafeSubscriber
    OnSubscribe
    Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  105. Observable
    SafeSubscriber
    OnSubscribe
    Subscriber
    Nothing
    Action1
    Observer

    View full-size slide

  106. Observations
    • The consumer of a stream is always a Subscriber.
    • Might be wrapping an Observer, Action1, or just empty.
    • Always wrapped in SafeSubscriber to ensure contract.

    View full-size slide

  107. Observations
    • The consumer of a stream is always a Subscriber.
    • Might be wrapping an Observer, Action1, or just empty.
    • Always wrapped in SafeSubscriber to ensure contract.
    • Observable is just a helper for passing Subscriber to
    OnSubscribe (aka Action1>).

    View full-size slide

  108. Observable greeter = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  109. Observable greeter = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  110. static Observable just(final T value) {

    }X

    View full-size slide

  111. static Observable just(final T value) {

    return new Observable();

    }X

    View full-size slide

  112. static Observable just(final T value) {

    return new Observable(new OnSubscribe() {

    @Override public void call(Subscriber super T> s) {

    }Y

    });

    }X

    View full-size slide

  113. static Observable just(final T value) {

    return new Observable(new OnSubscribe() {

    @Override public void call(Subscriber super T> s) {

    s.onNext(value);

    }Y

    });

    }X

    View full-size slide

  114. static Observable just(final T value) {

    return new Observable(new OnSubscribe() {

    @Override public void call(Subscriber super T> s) {

    s.onNext(value);

    s.onCompleted();

    }Y

    });

    }X

    View full-size slide

  115. Observable greeter = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  116. Observable greeter =
    Observable.create(new OnSubscribe() {

    @Override public void call(Subscriber super String> s) {

    s.onNext("Hi!");

    s.onCompleted();

    }

    });

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    just

    View full-size slide

  117. Observable greeter =
    Observable.create(new OnSubscribe() {

    @Override public void call(Subscriber super String> s) {

    s.onNext("Hi!");

    s.onCompleted();

    }X

    });

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }Y

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber>XssX= (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    just

    View full-size slide

  118. just
    Observable greeter =
    Observable.create(new OnSubscribe() {

    @Override public void call(Subscriber super String> s) {
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    s.onNext("Hi!");

    s.onCompleted();

    }

    });

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }Y

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide

  119. Observable greeter =
    Observable.create(new OnSubscribe() {

    @Override public void call(Subscriber super String> s) {
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    s.onNext("Hi!");

    s.onCompleted();

    }

    });

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }X

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    just

    View full-size slide

  120. Observable greeter =
    Observable.create(new OnSubscribe() {

    @Override public void call(Subscriber super String> s) {
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    s.onNext("Hi!");

    s.onCompleted();

    }

    });

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = greeter.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    > Main$2@65ab7765
    > Main$2@65ab7765
    > ITEM: Hi!
    > Main$2@65ab7765
    just

    View full-size slide

  121. SafeSubscriber
    Subscriber
    Nothing
    Action1
    Observer
    Observable
    OnSubscribe

    View full-size slide

  122. Observable o = Observable.just("Hi!");

    Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {

    System.out.println("ITEM: " + item);

    }X

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    Subscription s = o.subscribe(in);
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());

    View full-size slide


  123. Subscriber in = new Subscriber() {

    @Override public void onNext(String item) {


    }X

    @Override public void onError(Throwable e) {}

    @Override public void onCompleted() {}

    };
    System.out.println(in);
    in
    SafeSubscriber> ss = (SafeSubscriber>) s;

    System.out.println(ss.getActual());
    Observable o = Observable.just("Hi!");
    Subscription s = o.subscribe(new Action1() {

    @Override public void call(String item) {

    System.out.println("ITEM: " + item);

    }X

    });Z

    View full-size slide

  124. Observable o = Observable.just("Hi!")

    .map(new Func1() {

    @Override public Integer call(String s) {

    return s.length();

    }

    });Y
    Subscription s = o.subscribe(new Action1() {

    @Override public void call(Integer item) {

    System.out.println("ITEM: " + item);

    }X

    });Z

    View full-size slide

  125. Observable o = Observable.just("Hi!")

    .map(new Func1() {

    @Override public Integer call(String s) {

    return s.length();

    }

    });Y
    Subscription s = o.subscribe(new Action1() {

    @Override public void call(Integer item) {

    System.out.println("ITEM: " + item);

    }X

    });Z

    View full-size slide

  126. Observable o = Observable.just("Hi!")

    .map(new Func1() {

    @Override public Integer call(String s) {

    return s.length();

    }

    });Y
    Subscription s = o.subscribe(new Action1() {

    @Override public void call(Integer item) {

    System.out.println("ITEM: " + item);

    }X

    });Z
    > ITEM: 3

    View full-size slide

  127. Observable o = Observable.just("Hi!")

    .map(new Func1() {

    @Override public Integer call(String s) {

    return s.length();

    }

    });Y
    Subscription s = o.subscribe(new Action1() {

    @Override public void call(Integer item) {

    System.out.println("ITEM: " + item);

    }X

    });Z
    > ITEM: 3

    View full-size slide

  128. Observable o = Observable.just("Hi!")

    .map(new Func1() {

    @Override public Integer call(String s) {

    return s.length();

    }

    });Y
    Subscription s = o.subscribe(new Action1() {

    @Override public void call(Integer item) {

    System.out.println("ITEM: " + item);

    }X

    });Z
    > ITEM: 3

    View full-size slide

  129. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }

    View full-size slide

  130. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }

    View full-size slide

  131. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }

    View full-size slide

  132. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }

    View full-size slide

  133. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }

    View full-size slide

  134. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }
    Observable filter(Func1 super T, Boolean> predicate) {

    return lift(new OperatorFilter(predicate));

    }

    View full-size slide

  135. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }
    Observable filter(Func1 super T, Boolean> predicate) {

    return lift(new OperatorFilter(predicate));

    }
    Observable scan(R initialValue,
    Func2 accumulator) {

    return lift(new OperatorScan(initialValue, accumulator));

    }

    View full-size slide

  136. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }
    Observable filter(Func1 super T, Boolean> predicate) {

    return lift(new OperatorFilter(predicate));

    }
    Observable scan(R initialValue,
    Func2 accumulator) {

    return lift(new OperatorScan(initialValue, accumulator));

    }
    Observable withLatestFrom(Observable extends U> other,
    Func2 super T, ? super U, ? extends R> resultSelector) {

    return lift(
    new OperatorWithLatestFrom(other, resultSelector));

    }

    View full-size slide

  137. Observable lift(Operator extends R, ? super T> operator) {
    }

    View full-size slide

  138. Observable lift(Operator extends R, ? super T> operator) {

    }

    View full-size slide

  139. Observable lift(Operator extends R, ? super T> operator) {

    }X
    interface Operator extends Func1,
    Subscriber super T>> {

    }X

    View full-size slide

  140. Observable lift(Operator extends R, ? super T> operator) {

    }X
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    interface Operator extends Func1,
    Subscriber super T>> {

    }X

    View full-size slide

  141. Observable lift(Operator extends R, ? super T> operator) {

    }X
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    Func1
    interface Operator extends Func1,
    Subscriber super T>> {

    }

    View full-size slide

  142. Observable lift(Operator extends R, ? super T> operator) {

    }X
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    Func1
    Operator
    interface Operator extends Func1,
    Subscriber super T>> {

    }X

    View full-size slide

  143. Observable lift(Operator extends R, ? super T> operator) {

    }X
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    interface Operator extends Func1,
    Subscriber super T>> {

    }X

    View full-size slide

  144. Observable lift(Operator extends R, ? super T> operator) {

    }X
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    Subscriber
    interface Operator extends Func1,
    Subscriber super T>> {

    }X

    View full-size slide

  145. Observable lift(Operator extends R, ? super T> operator) {

    }
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    Subscriber
    interface Operator extends Func1,
    Subscriber super T>> {

    }
    Subscriber

    View full-size slide

  146. Observable lift(Operator extends R, ? super T> operator) {

    }X
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    Subscriber
    interface Operator extends Func1,
    Subscriber super T>> {

    }X
    Operator
    Subscriber

    View full-size slide

  147. Observable lift(Operator extends R, ? super T> operator) {

    }X
    Observable.just("Hi!")

    .map(s -> s.length())
    .subscribe(
    new Action1 {..});
    Subscriber
    interface Operator extends Func1,
    Subscriber super T>> {

    }X
    Func1,
    Subscriber>
    Subscriber
    Operator< , >

    View full-size slide

  148. Observable lift(Operator extends R, ? super T> operator) {

    }X

    View full-size slide

  149. Observable lift(Operator extends R, ? super T> operator) {

    return new Observable();

    }X

    View full-size slide

  150. Observable lift(Operator extends R, ? super T> operator) {

    return new Observable(new OnSubscribe() {

    @Override public void call(Subscriber super R> sr) {

    }Y

    });

    }X

    View full-size slide

  151. Observable lift(Operator extends R, ? super T> operator) {

    return new Observable(new OnSubscribe() {

    @Override public void call(Subscriber super R> sr) {

    Subscriber super T> st = operator.call(sr);

    }Y

    });

    }X

    View full-size slide

  152. Observable lift(Operator extends R, ? super T> operator) {

    return new Observable(new OnSubscribe() {

    @Override public void call(Subscriber super R> sr) {

    Subscriber super T> st = operator.call(sr);

    onSubscribe.call(st);

    }Y

    });

    }X

    View full-size slide

  153. Observable map(Func1 super T, ? extends R> func) {

    return lift(new OperatorMap(func));

    }

    View full-size slide

  154. final class OperatorMap implements Operator {

    }X

    View full-size slide

  155. final class OperatorMap implements Operator {

    private final Func1 super T, ? extends R> func;


    OperatorMap(Func1 super T, ? extends R> func) {

    this.func = func;

    }W

    }X

    View full-size slide

  156. final class OperatorMap implements Operator {

    private final Func1 super T, ? extends R> func;


    OperatorMap(Func1 super T, ? extends R> func) {

    this.func = func;

    }W


    @Override
    public Subscriber super T> call(final Subscriber super R> o) {

    }Y

    }X

    View full-size slide

  157. final class OperatorMap implements Operator {

    private final Func1 super T, ? extends R> func;


    OperatorMap(Func1 super T, ? extends R> func) {

    this.func = func;

    }W


    @Override
    public Subscriber super T> call(final Subscriber super R> o) {

    return new Subscriber(o) {
    };

    }Y

    }X

    View full-size slide

  158. final class OperatorMap implements Operator {

    private final Func1 super T, ? extends R> func;


    OperatorMap(Func1 super T, ? extends R> func) {

    this.func = func;

    }W


    @Override
    public Subscriber super T> call(final Subscriber super R> o) {

    return new Subscriber(o) {

    @Override public void onNext(T t) {

    o.onNext(func.call(t));

    }Z

    };

    }Y

    }X

    View full-size slide

  159. final class OperatorMap implements Operator {

    private final Func1 super T, ? extends R> func;


    OperatorMap(Func1 super T, ? extends R> func) {

    this.func = func;

    }W


    @Override
    public Subscriber super T> call(final Subscriber super R> o) {

    return new Subscriber(o) {

    @Override public void onNext(T t) {

    o.onNext(func.call(t));

    }Z

    @Override public void onCompleted() { o.onCompleted(); }

    @Override public void onError(Throwable e) { o.onError(e); }

    };

    }Y

    }X

    View full-size slide

  160. Observations
    • The consumer of a stream is always a Subscriber.
    • Might be wrapping an Observer, Action1, or just empty.
    • Always wrapped in SafeSubscriber to ensure contract.
    • Observable is just a helper for passing Subscriber to
    OnSubscribe (aka Action1>).

    View full-size slide

  161. Observations
    • The consumer of a stream is always a Subscriber.
    • Might be wrapping an Observer, Action1, or just empty.
    • Always wrapped in SafeSubscriber to ensure contract.
    • Observable is just a helper for passing Subscriber to
    OnSubscribe (aka Action1>).
    • 

    • Operator (aka Func1,Subscriber>)
    changes Observable to Observable via lift().

    View full-size slide

  162. Observations
    • It's turtles Subscriber all the

    way down!

    View full-size slide

  163. Observable.just("Hi!")

    View full-size slide

  164. Observable
    OnSubscribe
    Observable.just("Hi!")

    View full-size slide

  165. Observable
    OnSubscribe
    Observable.just("Hi!")
    .map(s -> s.length())

    View full-size slide

  166. Observable
    OnSubscribe
    Observable.just("Hi!")
    .map(s -> s.length()) Func1

    View full-size slide

  167. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Observable.just("Hi!")
    .map(s -> s.length()) Func1

    View full-size slide

  168. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Action1

    View full-size slide

  169. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Action1

    View full-size slide

  170. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber

    View full-size slide

  171. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    SafeSubscriber

    View full-size slide

  172. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    SafeSubscriber

    View full-size slide

  173. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    SafeSubscriber
    Subscriber

    View full-size slide

  174. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    SafeSubscriber

    View full-size slide

  175. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    "Hi!"
    SafeSubscriber

    View full-size slide

  176. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    "Hi!"
    SafeSubscriber

    View full-size slide

  177. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    "Hi!"
    SafeSubscriber

    View full-size slide

  178. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    3
    SafeSubscriber

    View full-size slide

  179. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    3
    SafeSubscriber

    View full-size slide

  180. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    3
    SafeSubscriber

    View full-size slide

  181. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    onComplete()
    3
    SafeSubscriber

    View full-size slide

  182. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Subscriber
    onComplete()
    3
    SafeSubscriber

    View full-size slide

  183. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    SafeSubscriber
    onComplete()
    3
    Subscriber

    View full-size slide

  184. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    onComplete()
    3

    View full-size slide

  185. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    3

    View full-size slide

  186. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1

    View full-size slide

  187. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())

    View full-size slide

  188. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"

    View full-size slide

  189. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"

    View full-size slide

  190. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"

    View full-size slide

  191. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"

    View full-size slide

  192. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    3

    View full-size slide

  193. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    3

    View full-size slide

  194. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())

    View full-size slide

  195. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())

    View full-size slide

  196. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"

    View full-size slide

  197. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"

    View full-size slide

  198. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"

    View full-size slide

  199. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    3

    View full-size slide

  200. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    3

    View full-size slide

  201. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    3

    View full-size slide

  202. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    3

    View full-size slide

  203. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())

    View full-size slide

  204. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())

    View full-size slide

  205. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())

    View full-size slide

  206. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())

    View full-size slide

  207. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    Observable.just("Hi!")
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())

    View full-size slide

  208. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())

    View full-size slide

  209. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())

    View full-size slide

  210. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    "Hi!"
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())

    View full-size slide

  211. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())
    3

    View full-size slide

  212. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())
    3

    View full-size slide

  213. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())
    3

    View full-size slide

  214. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())
    3

    View full-size slide

  215. Observable
    OnSubscribe
    Observable
    OnSubscribe
    Operator
    Action1
    httpApi.sayHi()
    .map(s -> s.length())
    .subscribe(..)
    Func1
    Observable
    OnSubscribe
    Operator
    .observeOn(computation())
    Observable
    OnSubscribe
    Operator
    .subscribeOn(io())
    3

    View full-size slide

  216. Observations
    • The consumer of a stream is always a Subscriber.
    • Observable is just a helper for passing Subscriber to
    OnSubscribe (aka Action1>).
    • Operator (aka Func1,Subscriber>)
    changes Observable to Observable via lift().

    View full-size slide

  217. Observations
    • The consumer of a stream is always a Subscriber.
    • Observable is just a helper for passing Subscriber to
    OnSubscribe (aka Action1>).
    • Operator (aka Func1,Subscriber>)
    changes Observable to Observable via lift().
    • subscribeOn() and observeOn() are operators which change the
    thread a Subscriber subscribes on and is called on, respectively.

    View full-size slide

  218. More Resources

    View full-size slide

  219. More Resources
    • Use the source!

    https://github.com/ReactiveX/RxJava/

    View full-size slide

  220. More Resources
    • Use the source!

    https://github.com/ReactiveX/RxJava/
    • "Advanced RxJava" blog series by David Karnok

    http://akarnokd.blogspot.hu/

    View full-size slide

  221. More Resources
    • Use the source!

    https://github.com/ReactiveX/RxJava/
    • "Advanced RxJava" blog series by David Karnok

    http://akarnokd.blogspot.hu/
    • "What if the user was a function?" by André Staltz

    https://www.youtube.com/watch?v=1zj7M1LnJV4

    View full-size slide

  222. RxWTF?
    • ReactiveX homepage

    http://reactivex.io/

    View full-size slide

  223. RxWTF?
    • ReactiveX homepage

    http://reactivex.io/
    • "The introduction to Reactive Programming you've been missing"

    https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

    View full-size slide

  224. RxWTF?
    • ReactiveX homepage

    http://reactivex.io/
    • "The introduction to Reactive Programming you've been missing"

    https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
    • RxMarbles

    http://rxmarbles.com/

    View full-size slide

  225. RxWTF?
    • ReactiveX homepage

    http://reactivex.io/
    • "The introduction to Reactive Programming you've been missing"

    https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
    • RxMarbles

    http://rxmarbles.com/
    • Talks by Ben Christensen

    https://speakerdeck.com/benjchristensen

    View full-size slide

  226. jakewharton
    jakewharton
    jakewharton
    twitter.com/
    google.com/+
    .com
    Demystifying
    RxJava Subscribers

    View full-size slide