All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.api.accelrx.context.rxjava3.ContextPropagatorOnSingleSubscribeAction Maven / Gradle / Ivy

package com.gs.api.accelrx.context.rxjava3;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.BiFunction;

import java.util.concurrent.Executor;

@SuppressWarnings("rawtypes")
public class ContextPropagatorOnSingleSubscribeAction implements BiFunction {

    private final ThreadContext threadContext;

    public ContextPropagatorOnSingleSubscribeAction(ThreadContext threadContext) {
        this.threadContext = threadContext;
    }

    @Override
    @SuppressWarnings({"rawtypes", "unchecked"})
    public SingleObserver apply(Single single, SingleObserver observer) throws Throwable {
        return new ContextCapturerSingle(observer, threadContext.currentContextExecutor());
    }

    private static final class ContextCapturerSingle implements SingleObserver {

        private final SingleObserver downstream;
        private final Executor contextExecutor;

        public ContextCapturerSingle(SingleObserver observer, Executor contextExecutor) {
            this.downstream = observer;
            this.contextExecutor = contextExecutor;
        }

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            contextExecutor.execute(() -> downstream.onSubscribe(d));
        }

        @Override
        public void onSuccess(@NonNull T t) {
            contextExecutor.execute(() -> downstream.onSuccess(t));
        }

        @Override
        public void onError(@NonNull Throwable e) {
            contextExecutor.execute(() -> downstream.onError(e));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy