Search in sources :

Example 1 with ContextWriteSubscriber

use of reactor.core.publisher.FluxContextWrite.ContextWriteSubscriber 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)

Aggregations

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