use of io.smallrye.mutiny.subscription.ContextSupport in project smallrye-mutiny by smallrye.
the class DeferredMultiWithContext method subscribe.
@Override
public void subscribe(MultiSubscriber<? super T> downstream) {
Context context;
if (downstream instanceof ContextSupport) {
context = ((ContextSupport) downstream).context();
} else {
context = Context.empty();
}
Multi<? extends T> multi;
try {
multi = mapper.apply(context);
if (multi == null) {
throw new NullPointerException(ParameterValidation.MAPPER_RETURNED_NULL);
}
} catch (Throwable failure) {
Subscriptions.fail(downstream, failure);
return;
}
multi.subscribe(Infrastructure.onMultiSubscription(multi, downstream));
}
use of io.smallrye.mutiny.subscription.ContextSupport in project smallrye-mutiny by smallrye.
the class MultiPublishOp method connect.
@Override
public void connect(ConnectableMultiConnection connection) {
boolean doConnect;
PublishSubscriber<T> ps;
// we loop because concurrent connect/disconnect and termination may change the state
for (; ; ) {
// retrieve the current subscriber-to-source instance
ps = current.get();
// if there is none yet or the current has been disposed
if (ps == null || ps.cancelled.get()) {
// create a new subscriber-to-source
MultiSubscriber<?> subscriber = connection.getSubscriber();
Context context;
if (subscriber instanceof ContextSupport) {
context = ((ContextSupport) subscriber).context();
} else {
context = Context.empty();
}
PublishSubscriber<T> u = new PublishSubscriber<>(current, bufferSize, context);
// try setting it as the current subscriber-to-source
if (!current.compareAndSet(ps, u)) {
// and created a new subscriber-to-source as well, retry
continue;
}
ps = u;
}
// if connect() was called concurrently, only one of them should actually
// connect to the source
doConnect = !ps.shouldConnect.get() && ps.shouldConnect.compareAndSet(false, true);
break;
}
if (connection != null) {
connection.accept(ps);
}
if (doConnect) {
upstream.subscribe(Infrastructure.onMultiSubscription(upstream, ps));
}
}
use of io.smallrye.mutiny.subscription.ContextSupport in project smallrye-mutiny by smallrye.
the class MultiWithContext method subscribe.
@Override
@SuppressWarnings("unchecked")
public void subscribe(MultiSubscriber<? super O> downstream) {
ParameterValidation.nonNull(downstream, "downstream");
Context context;
if (downstream instanceof ContextSupport) {
context = ((ContextSupport) downstream).context();
} else {
context = Context.empty();
}
Multi<O> multi;
try {
multi = builder.apply((Multi<I>) upstream, context);
if (multi == null) {
downstream.onSubscribe(Subscriptions.CANCELLED);
downstream.onFailure(new NullPointerException("The builder function returned null"));
return;
}
} catch (Throwable err) {
downstream.onSubscribe(Subscriptions.CANCELLED);
downstream.onFailure(err);
return;
}
Multi<O> pipelineWithContext = Infrastructure.onMultiCreation(multi);
pipelineWithContext.subscribe().withSubscriber(downstream);
}
Aggregations