com.cmonbaby.http.rxjava.CallEnqueueOnSubscribe Maven / Gradle / Ivy
Show all versions of http_lower Show documentation
package com.cmonbaby.http.rxjava;
import androidx.annotation.NonNull;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.exceptions.Exceptions;
/**
* Author: Simon
*
QO: 8950764
*
Email: [email protected]
*
WebSize: https://www.cmonbaby.com
*
Version: 1.0.0
*
Date: 2020/12/28
*
Description:
*/
final class CallEnqueueOnSubscribe implements OnSubscribe> {
private final Call originalCall;
CallEnqueueOnSubscribe(Call originalCall) {
this.originalCall = originalCall;
}
@Override
public void call(Subscriber super Response> subscriber) {
// Since Call is a one-shot type, clone it for each new subscriber.
Call call = originalCall.clone();
final CallArbiter arbiter = new CallArbiter<>(call, subscriber);
subscriber.add(arbiter);
subscriber.setProducer(arbiter);
call.enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
arbiter.emitResponse(response);
}
@Override
public void onFailure(@NonNull Call call, @NonNull Throwable t) {
Exceptions.throwIfFatal(t);
arbiter.emitError(t);
}
});
}
}