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