JakeWharton / RxBinding

RxJava binding APIs for Android's UI widgets.
Apache License 2.0
9.68k stars 971 forks source link

Observable.create() vs Observable.fromAsync() #274

Closed alex-townsend closed 8 years ago

alex-townsend commented 8 years ago

RxJava 1.1.7 introduced Observable.fromAsync() to "... bridge the callback world with the reactive". Would implementing Observable.fromAsync() with the callback-based view listeners in Android be functionally the same as the Observable.create() with Observable.OnSubscribe<T> method that RxBinding uses?

Example Observable.fromAsync() vs TextViewTextOnSubscribe

public static Observable<CharSequence> textChanges(final TextView textView) {
        return Observable.fromAsync(new Action1<AsyncEmitter<CharSequence>>() {
            @Override
            public void call(final AsyncEmitter<CharSequence> emitter) {
                final TextWatcher textWatcher = new SimpleTextWatcher() {
                    @Override
                    public void afterTextChanged(Editable s) {
                        emitter.onNext(s);
                    }
                };
                textView.addTextChangedListener(textWatcher);

                final AsyncEmitter.Cancellable cancellable = new AsyncEmitter.Cancellable() {
                    @Override
                    public void cancel() throws Exception {
                        textView.removeTextChangedListener(textWatcher);
                    }
                };
                emitter.setCancellation(cancellable);
            }
        }, AsyncEmitter.BackpressureMode.BUFFER); // not quite sure what BackpressureMode would be acceptable
    }

Are there potential performance or output differences between the two implementations? Observable.fromAsnyc() is still flagged as Experimental as well.

(was not sure whether to post this question to RxJava or RxBinding -- will move if this is out of place)

JakeWharton commented 8 years ago

Would implementing Observable.fromAsync() with the callback-based view listeners in Android be functionally the same as the Observable.create() with Observable.OnSubscribe<T> method that RxBinding uses?

Yes. As a library we cannot use it because it's not a stable API though. It's great for use in applications though.

Are there potential performance or output differences between the two implementations?

No, they will be exactly the same in performance and function.

PaulWoitaschek commented 8 years ago

If this gets promoted to the regulären api: which backpressure Mode would be suitable for this library?

JakeWharton commented 8 years ago

I would use NONE in the library. It's the user's choice (via applying onBackpressureXxx) if they want to add backpressure. By default, though, I wouldn't include any (which is what we do now with create()).