Search in sources :

Example 1 with CorePublisher

use of reactor.core.CorePublisher in project reactor-core by reactor.

the class ContextTrackingFunctionWrapper method apply.

@Override
public CorePublisher<V> apply(Publisher<T> source) {
    String key = CONTEXT_MARKER_PREFIX + System.identityHashCode(source);
    // Wrap source with a logic that will check whether the key is still there and remove it
    source = Operators.<T, T>liftPublisher((p, actual) -> {
        Context ctx = actual.currentContext();
        if (!ctx.hasKey(key)) {
            throw new IllegalStateException("Context loss after applying " + marker);
        }
        Context newContext = ctx.delete(key);
        return new ContextWriteSubscriber<>(actual, newContext);
    }).apply(source);
    Publisher<V> result = transformer.apply(source);
    // It is okay to return `CorePublisher` here since `transform` will use `from()` anyways
    return new CorePublisher<V>() {

        @Override
        public void subscribe(CoreSubscriber<? super V> actual) {
            Context ctx = actual.currentContext().put(key, true);
            CoreSubscriber<V> subscriber = new ContextWriteSubscriber<>(actual, ctx);
            if (result instanceof CorePublisher) {
                ((CorePublisher<V>) result).subscribe(subscriber);
            } else {
                result.subscribe(subscriber);
            }
        }

        @Override
        public void subscribe(Subscriber<? super V> subscriber) {
            subscribe(Operators.toCoreSubscriber(subscriber));
        }
    };
}
Also used : Context(reactor.util.context.Context) CoreSubscriber(reactor.core.CoreSubscriber) ContextWriteSubscriber(reactor.core.publisher.FluxContextWrite.ContextWriteSubscriber) Subscriber(org.reactivestreams.Subscriber) CorePublisher(reactor.core.CorePublisher) CoreSubscriber(reactor.core.CoreSubscriber) ContextWriteSubscriber(reactor.core.publisher.FluxContextWrite.ContextWriteSubscriber)

Example 2 with CorePublisher

use of reactor.core.CorePublisher in project reactor-core by reactor.

the class Mono method subscribe.

@Override
@SuppressWarnings("unchecked")
public final void subscribe(Subscriber<? super T> actual) {
    CorePublisher publisher = Operators.onLastAssembly(this);
    CoreSubscriber subscriber = Operators.toCoreSubscriber(actual);
    try {
        if (publisher instanceof OptimizableOperator) {
            OptimizableOperator operator = (OptimizableOperator) publisher;
            while (true) {
                subscriber = operator.subscribeOrReturn(subscriber);
                if (subscriber == null) {
                    // null means "I will subscribe myself", returning...
                    return;
                }
                OptimizableOperator newSource = operator.nextOptimizableSource();
                if (newSource == null) {
                    publisher = operator.source();
                    break;
                }
                operator = newSource;
            }
        }
        publisher.subscribe(subscriber);
    } catch (Throwable e) {
        Operators.reportThrowInSubscribe(subscriber, e);
        return;
    }
}
Also used : CorePublisher(reactor.core.CorePublisher) CoreSubscriber(reactor.core.CoreSubscriber)

Example 3 with CorePublisher

use of reactor.core.CorePublisher in project reactor-core by reactor.

the class Flux method subscribe.

@Override
@SuppressWarnings("unchecked")
public final void subscribe(Subscriber<? super T> actual) {
    CorePublisher publisher = Operators.onLastAssembly(this);
    CoreSubscriber subscriber = Operators.toCoreSubscriber(actual);
    try {
        if (publisher instanceof OptimizableOperator) {
            OptimizableOperator operator = (OptimizableOperator) publisher;
            while (true) {
                subscriber = operator.subscribeOrReturn(subscriber);
                if (subscriber == null) {
                    // null means "I will subscribe myself", returning...
                    return;
                }
                OptimizableOperator newSource = operator.nextOptimizableSource();
                if (newSource == null) {
                    publisher = operator.source();
                    break;
                }
                operator = newSource;
            }
        }
        publisher.subscribe(subscriber);
    } catch (Throwable e) {
        Operators.reportThrowInSubscribe(subscriber, e);
        return;
    }
}
Also used : CorePublisher(reactor.core.CorePublisher) CoreSubscriber(reactor.core.CoreSubscriber)

Aggregations

CorePublisher (reactor.core.CorePublisher)3 CoreSubscriber (reactor.core.CoreSubscriber)3 Subscriber (org.reactivestreams.Subscriber)1 ContextWriteSubscriber (reactor.core.publisher.FluxContextWrite.ContextWriteSubscriber)1 Context (reactor.util.context.Context)1