use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class ReactiveSeq method merge.
default ReactiveSeq<T> merge(Adapter<T>... adapters) {
return defer(() -> {
Publisher<T>[] publishers = ReactiveSeq.of(adapters).map(a -> a.stream()).toArray(n -> new Publisher[n]);
final Counter c = new Counter();
c.active.set(publishers.length + 1);
final QueueBasedSubscriber<T> init = QueueBasedSubscriber.subscriber(QueueFactories.boundedQueue(5_000), c, publishers.length);
final Supplier<Continuation> sp = () -> {
backpressureAware().subscribe(init);
for (final Publisher next : publishers) {
next.subscribe(QueueBasedSubscriber.subscriber(init.getQueue(), c, publishers.length));
}
init.close();
return Continuation.empty();
};
final Continuation continuation = new Continuation(sp);
init.addContinuation(continuation);
return ReactiveSeq.fromStream(init.jdkStream());
});
}
use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class ReactiveSeq method broadcast.
default Topic<T> broadcast() {
Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build().withTimeout(1);
Topic<T> topic = new Topic<T>(queue, QueueFactories.<T>unboundedNonBlockingQueue());
AtomicBoolean wip = new AtomicBoolean(false);
Spliterator<T> split = this.spliterator();
Continuation[] ref = { null };
Continuation cont = new Continuation(() -> {
if (wip.compareAndSet(false, true)) {
try {
// use the first consuming thread to tell this Stream onto the Queue
if (!split.tryAdvance(topic::offer)) {
topic.close();
return Continuation.empty();
}
} finally {
wip.set(false);
}
}
return ref[0];
});
ref[0] = cont;
queue.addContinuation(cont);
return topic;
}
use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class ReactiveSeq method mergeP.
/**
* A potentially asynchronous merge operation where data from each publisher may arrive out of order (if publishers
* are configured to publish asynchronously.
* The QueueFactory parameter can be used by pull based Streams to control the maximum queued elements @see {@link QueueFactories}
* Push based reactive-streams signal demand via their subscription.
*/
// name will be refactored to merge in the future
@Deprecated
default ReactiveSeq<T> mergeP(final QueueFactory<T> factory, final Publisher<T>... publishers) {
return defer(() -> {
final Counter c = new Counter();
c.active.set(publishers.length + 1);
final QueueBasedSubscriber<T> init = QueueBasedSubscriber.subscriber(factory, c, publishers.length);
final Supplier<Continuation> sp = () -> {
subscribe(init);
for (final Publisher next : publishers) {
next.subscribe(QueueBasedSubscriber.subscriber(init.getQueue(), c, publishers.length));
}
init.close();
return Continuation.empty();
};
final Continuation continuation = new Continuation(sp);
init.addContinuation(continuation);
return ReactiveSeq.fromStream(init.jdkStream());
});
}
use of com.oath.cyclops.types.futurestream.Continuation in project cyclops by aol.
the class ReactiveStreamX method unwrapStream.
@Override
public Stream<T> unwrapStream() {
if (async == Type.NO_BACKPRESSURE) {
Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build();
AtomicBoolean wip = new AtomicBoolean(false);
Continuation cont = new Continuation(() -> {
if (wip.compareAndSet(false, true)) {
this.source.subscribeAll(queue::offer, i -> {
queue.close();
}, () -> queue.close());
}
return Continuation.empty();
});
queue.addContinuation(cont);
return queue.stream();
}
return StreamSupport.stream(new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).spliterator(), false);
}
Aggregations