Search in sources :

Example 1 with BACKPRESSURE

use of com.oath.cyclops.internal.stream.ReactiveStreamX.Type.BACKPRESSURE in project cyclops by aol.

the class ReactiveStreamX method multicast.

public Seq<ReactiveSeq<T>> multicast(int num) {
    if (this.async == Type.NO_BACKPRESSURE) {
        ConcurrentLinkedQueue<Subscriber> subs = new ConcurrentLinkedQueue<>();
        Seq<ReactiveSeq<T>> result = Seq.empty();
        for (int i = 0; i < num; i++) {
            ReactiveSeq<T> seq = Spouts.<T>async(s1 -> {
                subs.add(s1.asSubscriber());
                if (subs.size() == num) {
                    this.forEach(e -> subs.forEach(s -> s.onNext(e)), ex -> subs.forEach(s -> s.onError(ex)), () -> subs.forEach(s -> s.onComplete()));
                }
            });
            result = result.plus(seq);
        }
        return result;
    }
    if (this.async == BACKPRESSURE) {
        ConcurrentLinkedQueue<Subscriber> subs = new ConcurrentLinkedQueue<>();
        Seq<ReactiveSeq<T>> result = Seq.empty();
        Subscription sub = forEachSubscribe(e -> subs.forEach(s -> s.onNext(e)), ex -> subs.forEach(s -> s.onError(ex)), () -> subs.forEach(s -> s.onComplete()));
        for (int i = 0; i < num; i++) {
            ReactiveSeq<T> seq = new ReactiveStreamX<T>(new PublisherToOperator<T>(new Publisher<T>() {

                @Override
                public void subscribe(Subscriber<? super T> s) {
                    subs.add(s);
                    s.onSubscribe(sub);
                }
            }));
            result = result.plus(seq);
        }
        return result;
    }
    return Streams.toBufferingCopier(() -> iterator(), num, () -> new ArrayDeque<T>(100)).map(ReactiveSeq::fromIterable);
}
Also used : Tuple4(cyclops.data.tuple.Tuple4) Tuple3(cyclops.data.tuple.Tuple3) Connectable(com.oath.cyclops.types.stream.Connectable) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) Continuation(com.oath.cyclops.types.futurestream.Continuation) PersistentCollection(com.oath.cyclops.types.persistent.PersistentCollection) QueueFactories(com.oath.cyclops.async.QueueFactories) Getter(lombok.Getter) Topic(com.oath.cyclops.async.adapters.Topic) Wither(lombok.experimental.Wither) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) com.oath.cyclops.internal.stream.spliterators.push(com.oath.cyclops.internal.stream.spliterators.push) Future(cyclops.control.Future) Vector(cyclops.data.Vector) Seq(cyclops.data.Seq) Tuple(cyclops.data.tuple.Tuple) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StreamSupport(java.util.stream.StreamSupport) Collector(java.util.stream.Collector) Subscriber(org.reactivestreams.Subscriber) cyclops.control(cyclops.control) Spouts(cyclops.reactive.Spouts) Zipping3Spliterator(com.oath.cyclops.internal.stream.spliterators.Zipping3Spliterator) Streams(cyclops.companion.Streams) Publisher(org.reactivestreams.Publisher) OneToOneConcurrentArrayQueue(org.agrona.concurrent.OneToOneConcurrentArrayQueue) Monoid(cyclops.function.Monoid) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) LockSupport(java.util.concurrent.locks.LockSupport) ReactiveSeq(cyclops.reactive.ReactiveSeq) Queue(com.oath.cyclops.async.adapters.Queue) Stream(java.util.stream.Stream) Function3(cyclops.function.Function3) Function4(cyclops.function.Function4) Subscription(org.reactivestreams.Subscription) IterableX(com.oath.cyclops.types.traversable.IterableX) SYNC(com.oath.cyclops.internal.stream.ReactiveStreamX.Type.SYNC) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) BACKPRESSURE(com.oath.cyclops.internal.stream.ReactiveStreamX.Type.BACKPRESSURE) QueueFactory(com.oath.cyclops.async.adapters.QueueFactory) java.util.function(java.util.function) Signal(com.oath.cyclops.async.adapters.Signal) AllArgsConstructor(lombok.AllArgsConstructor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Publisher(org.reactivestreams.Publisher) ReactiveSeq(cyclops.reactive.ReactiveSeq) Subscriber(org.reactivestreams.Subscriber) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Subscription(org.reactivestreams.Subscription)

Aggregations

QueueFactories (com.oath.cyclops.async.QueueFactories)1 Queue (com.oath.cyclops.async.adapters.Queue)1 QueueFactory (com.oath.cyclops.async.adapters.QueueFactory)1 Signal (com.oath.cyclops.async.adapters.Signal)1 Topic (com.oath.cyclops.async.adapters.Topic)1 BACKPRESSURE (com.oath.cyclops.internal.stream.ReactiveStreamX.Type.BACKPRESSURE)1 SYNC (com.oath.cyclops.internal.stream.ReactiveStreamX.Type.SYNC)1 Zipping3Spliterator (com.oath.cyclops.internal.stream.spliterators.Zipping3Spliterator)1 com.oath.cyclops.internal.stream.spliterators.push (com.oath.cyclops.internal.stream.spliterators.push)1 Continuation (com.oath.cyclops.types.futurestream.Continuation)1 PersistentCollection (com.oath.cyclops.types.persistent.PersistentCollection)1 Connectable (com.oath.cyclops.types.stream.Connectable)1 IterableX (com.oath.cyclops.types.traversable.IterableX)1 ExceptionSoftener (com.oath.cyclops.util.ExceptionSoftener)1 Streams (cyclops.companion.Streams)1 cyclops.control (cyclops.control)1 Future (cyclops.control.Future)1 Seq (cyclops.data.Seq)1 Vector (cyclops.data.Vector)1 Tuple (cyclops.data.tuple.Tuple)1