com.cmonbaby.http.rxjava.ResultOnSubscribe Maven / Gradle / Ivy
Show all versions of http_lower Show documentation
package com.cmonbaby.http.rxjava;
import retrofit2.Response;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.exceptions.Exceptions;
import rx.exceptions.OnCompletedFailedException;
import rx.exceptions.OnErrorFailedException;
import rx.exceptions.OnErrorNotImplementedException;
/**
* Author: Simon
*
QO: 8950764
*
Email: [email protected]
*
WebSize: https://www.cmonbaby.com
*
Version: 1.0.0
*
Date: 2020/12/28
*
Description:
*/
final class ResultOnSubscribe implements OnSubscribe> {
private final OnSubscribe> upstream;
ResultOnSubscribe(OnSubscribe> upstream) {
this.upstream = upstream;
}
@Override
public void call(Subscriber super Result> subscriber) {
upstream.call(new ResultSubscriber<>(subscriber));
}
private static class ResultSubscriber extends Subscriber> {
private final Subscriber super Result> subscriber;
ResultSubscriber(Subscriber super Result> subscriber) {
super(subscriber);
this.subscriber = subscriber;
}
@Override
public void onNext(Response response) {
subscriber.onNext(Result.response(response));
}
@Override
public void onError(Throwable throwable) {
try {
subscriber.onNext(Result.error(throwable));
} catch (Throwable t) {
try {
subscriber.onError(t);
} catch (OnCompletedFailedException
| OnErrorFailedException
| OnErrorNotImplementedException e) {
e.printStackTrace();
} catch (Throwable inner) {
Exceptions.throwIfFatal(inner);
inner.printStackTrace();
}
return;
}
subscriber.onCompleted();
}
@Override
public void onCompleted() {
subscriber.onCompleted();
}
}
}