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));
}
};
}
Aggregations