Skip to content

Are there any build in observers for invoking action if onError or onNext was called? #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lexer opened this issue Dec 4, 2013 · 11 comments

Comments

@lexer
Copy link

lexer commented Dec 4, 2013

Are there any build in observers for invoking action if onError or onNext was called?

Typical use case is rest call from UI. Before REST call you should disable inteface and if operation success or fails you should reenable it again.

    ProgressDialogFragment.show(getActivity());

    api.someMethpd()
            .doOnEach(new NextOrErrorObserver<DriverApplication>(new Action0() {
                @Override
                public void call() {
                    ProgressDialogFragment.dismiss(getActivity());
                }
            }))
            .subscribe(new Action1<Result>() {
                           @Override
                           public void call(Result result) {
                               ....
                           }
                       }, new Action1<Throwable>() {
                           @Override
                           public void call(Throwable throwable) {
                               ...
                           }
                       }
            );

Currently I implemented one by my self.

    public class NextOrErrorObserver<T> implements Observer<T> {
        private Action0 nextOrErrorAction;

        public NextOrErrorObserver(Action0 nextOrErrorAction) {

            this.nextOrErrorAction = nextOrErrorAction;
        }

        @Override
        public void onCompleted() {

        }

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

        @Override
        public void onNext(T args) {
            nextOrErrorAction.call();
        }
    }

BTW: I cannot use finallyDo for this. Finally do only triggered for onComplete and onError. Also I need my method to be called before onNext and onError.

@lexer
Copy link
Author

lexer commented Dec 4, 2013

Probably we can add additional variation of doOnEach. Like doOnNextOrError?

@benjchristensen
Copy link
Member

@lexer
Copy link
Author

lexer commented Dec 4, 2013

@benjchristensen so right now Im using doOnEach with my custom observer. There is no doOnNextOrError that I need. Do you thinks its reasonable to have this overload in framework? I can implement it and make a pull request.

@benjchristensen
Copy link
Member

There is a doOnEach that takes actions for onNext and onError. I don't understand how that is different than a doOnNextOrOnError.

@lexer
Copy link
Author

lexer commented Dec 4, 2013

@benjchristensen difference is that i need to run same logic in both onNext and onError. If I will use onNext and onError i will need to call ProgressDialogFragment.dismiss() in each of them.

@benjchristensen
Copy link
Member

Why is that an issue to call it in either of them? You can pass the same function to both the onError and onNext args for doOnEach. One or the other will be called and ProgressDialogFragment.dismiss() will be invoked either way.

@lexer
Copy link
Author

lexer commented Dec 4, 2013

@benjchristensen The only problem is that It's very frequent use case.

So code like this will be repeated very often: api.doOnEach(actionOnNextOrError, actionOnNextOrError)
I just feel that this overload looks much nicer: api.doOnNextOrError(actionOnNextOrError);

@benjchristensen
Copy link
Member

It's not appropriate to do both onNext and onError with the same function as they receive different argument types. OnNext receives type T, onError receives Throwable. Thus your use case is unique in ignoring either argument. The onEach operator is not the right place to combine these.

It sounds like you want something like finallyDo for terminal state (after onComplete or onError) but before instead of after.

@lexer
Copy link
Author

lexer commented Dec 4, 2013

@benjchristensen You are right. I actually used finallyDo before. But in mine specific use case I realized that I cannot use finallyDo since the code should be executed before onNext and onError.

So you think that should be another operator with name like beforeDo?

Or its not a generic case and my approach with NextOrErrorObserver is fine:

    api.someMethpd()
            .doOnEach(new NextOrErrorObserver<DriverApplication>(new Action0() {
                @Override
                public void call() {
                    ProgressDialogFragment.dismiss(getActivity());
                }
            }))

@lexer
Copy link
Author

lexer commented Dec 9, 2013

@benjchristensen I ended with using this custom class, which I can supply as an argument to doOnEach

    public abstract class NextOrErrorAction0<T> implements Action0, Observer<T> {
        @Override
        public void onCompleted() {

        }

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

        @Override
        public void onNext(T args) {
            call();
        }

        @Override
        public abstract void call();
    }

@benjchristensen
Copy link
Member

I think your use of doOnEach is appropriate for your use case.

jihoonson pushed a commit to jihoonson/RxJava that referenced this issue Mar 6, 2020
…asure met… (ReactiveX#556)

* Issue ReactiveX#547: Added a Sliding Time Window implementations to measure metrics.

* Issue ReactiveX#547: Added a Fixed size Sliding Window implementation which aggregates the last N calls and replaced the existing RingBitSet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants