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

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

The newest version!
package com.gs.api.accelrx.context.rxjava3;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.MaybeObserver;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.BiFunction;

import java.util.concurrent.Executor;

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

    private final ThreadContext threadContext;

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

    @Override
    @SuppressWarnings({"rawtypes", "unchecked"})
    public MaybeObserver apply(Maybe maybe, MaybeObserver observer) throws Throwable {
        return new ContextCapturerMaybe<>(observer, threadContext.currentContextExecutor());
    }

    private static final class ContextCapturerMaybe implements MaybeObserver {

        private final MaybeObserver downstream;
        private final Executor contextExecutor;

        public ContextCapturerMaybe(MaybeObserver 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));
        }

        @Override
        public void onComplete() {
            contextExecutor.execute(downstream::onComplete);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy