com.gs.api.accelrx.context.rxjava3.ContextPropagatorOnObservableSubscribeAction Maven / Gradle / Ivy
package com.gs.api.accelrx.context.rxjava3;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.BiFunction;
import java.util.concurrent.Executor;
@SuppressWarnings("rawtypes")
public class ContextPropagatorOnObservableSubscribeAction implements BiFunction {
private final ThreadContext threadContext;
public ContextPropagatorOnObservableSubscribeAction(ThreadContext threadContext) {
this.threadContext = threadContext;
}
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public Observer apply(Observable maybe, Observer observer) throws Throwable {
return new ContextCapturerObservable<>(observer, threadContext.currentContextExecutor());
}
private static final class ContextCapturerObservable implements Observer {
private final Observer downstream;
private final Executor contextExecutor;
public ContextCapturerObservable(Observer observer, Executor contextExecutor) {
this.downstream = observer;
this.contextExecutor = contextExecutor;
}
@Override
public void onSubscribe(@NonNull Disposable d) {
contextExecutor.execute(() -> downstream.onSubscribe(d));
}
@Override
public void onNext(@NonNull T t) {
contextExecutor.execute(() -> downstream.onNext(t));
}
@Override
public void onError(@NonNull Throwable e) {
contextExecutor.execute(() -> downstream.onError(e));
}
@Override
public void onComplete() {
contextExecutor.execute(downstream::onComplete);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy