Search in sources :

Example 1 with Counter

use of com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter in project cyclops by aol.

the class ReactiveSeq method enqueuedAll.

static <T> ReactiveSeq<T> enqueuedAll(Consumer<? super Subscriber<T>>... subs) {
    final Counter c = new Counter();
    c.active.set(subs.length);
    QueueBasedSubscriber<T> s = QueueBasedSubscriber.subscriber(c, subs.length);
    for (Consumer<? super Subscriber<T>> next : subs) next.accept(s);
    s.close();
    return s.reactiveSeq();
}
Also used : Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter)

Example 2 with Counter

use of com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter in project cyclops by aol.

the class ReactiveSeq method enqueued.

static <T> ReactiveSeq<T> enqueued(Queue<T> q, Consumer<? super Subscriber<T>> sub) {
    final Counter c = new Counter();
    c.active.set(1);
    QueueBasedSubscriber<T> s = QueueBasedSubscriber.subscriber(q, c, 1);
    sub.accept(s);
    return s.reactiveSeq();
}
Also used : Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter)

Example 3 with Counter

use of com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter 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());
    });
}
Also used : PersistentCollection(com.oath.cyclops.types.persistent.PersistentCollection) Continuation(com.oath.cyclops.types.futurestream.Continuation) DataWitness.reactiveSeq(com.oath.cyclops.hkt.DataWitness.reactiveSeq) HashMap(cyclops.data.HashMap) Seq(cyclops.data.Seq) Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecoverableTraversable(com.oath.cyclops.types.traversable.RecoverableTraversable) ReversingLongArraySpliterator(com.oath.cyclops.internal.stream.spliterators.longs.ReversingLongArraySpliterator) Unit(com.oath.cyclops.types.factory.Unit) PrintWriter(java.io.PrintWriter) Contains(com.oath.cyclops.types.foldable.Contains) Monoid(cyclops.function.Monoid) com.oath.cyclops.async.adapters(com.oath.cyclops.async.adapters) ReversingDoubleArraySpliterator(com.oath.cyclops.internal.stream.spliterators.doubles.ReversingDoubleArraySpliterator) com.oath.cyclops.types.stream(com.oath.cyclops.types.stream) ReversingRangeLongSpliterator(com.oath.cyclops.internal.stream.spliterators.longs.ReversingRangeLongSpliterator) Queue(com.oath.cyclops.async.adapters.Queue) ReversingRangeIntSpliterator(com.oath.cyclops.internal.stream.spliterators.ints.ReversingRangeIntSpliterator) Function3(cyclops.function.Function3) Function4(cyclops.function.Function4) com.oath.cyclops.internal.stream.spliterators(com.oath.cyclops.internal.stream.spliterators) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) java.util.function(java.util.function) QueueBasedSubscriber(com.oath.cyclops.types.reactive.QueueBasedSubscriber) Tuple4(cyclops.data.tuple.Tuple4) Higher(com.oath.cyclops.hkt.Higher) To(com.oath.cyclops.types.foldable.To) Tuple3(cyclops.data.tuple.Tuple3) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) ReversingIntArraySpliterator(com.oath.cyclops.internal.stream.spliterators.ints.ReversingIntArraySpliterator) java.util.stream(java.util.stream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Vector(cyclops.data.Vector) AtomicReference(java.util.concurrent.atomic.AtomicReference) Tuple(cyclops.data.tuple.Tuple) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) com.oath.cyclops.async(com.oath.cyclops.async) Subscriber(org.reactivestreams.Subscriber) ReactiveTransformable(com.oath.cyclops.types.functor.ReactiveTransformable) OnEmptySwitch(com.oath.cyclops.types.recoverable.OnEmptySwitch) PrintStream(java.io.PrintStream) cyclops.control(cyclops.control) Streams(cyclops.companion.Streams) Executor(java.util.concurrent.Executor) Publisher(org.reactivestreams.Publisher) lombok.val(lombok.val) TimeUnit(java.util.concurrent.TimeUnit) Reducer(cyclops.function.Reducer) AtomicLong(java.util.concurrent.atomic.AtomicLong) ForkJoinPool(java.util.concurrent.ForkJoinPool) OneShotStreamX(com.oath.cyclops.internal.stream.OneShotStreamX) Curry(cyclops.function.Curry) Enumeration(cyclops.data.Enumeration) Continuation(com.oath.cyclops.types.futurestream.Continuation) Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter) Publisher(org.reactivestreams.Publisher)

Example 4 with Counter

use of com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter 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());
    });
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter) Publisher(org.reactivestreams.Publisher)

Example 5 with Counter

use of com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter in project cyclops by aol.

the class ReactiveSeq method enqueued.

/**
 * Create a Stream that accepts data via the Subsriber passed into the supplied Consumer.
 * reactive-streams susbscription can be used to determine demand (or ignored and data passed
 * via onNext, onError) excess supply over demand is enqueued
 *
 * <pre>
 *     {@code
 *      ReactiveSeq<Integer> input = ReactiveSeq.enqueued(subscriber->{
 *                                                          listener.onEvent(subscriber::onNext);
 *                                                          listener.onError(susbscriber::onError);
 *                                                          closeListener.onEvent(subscriber::onClose);
 *                                                      });
 *      }
 * </pre>
 *
 * @param sub
 * @param <T>
 * @return
 */
static <T> ReactiveSeq<T> enqueued(Consumer<? super Subscriber<T>> sub) {
    final Counter c = new Counter();
    c.active.set(1);
    QueueBasedSubscriber<T> s = QueueBasedSubscriber.subscriber(c, 1);
    sub.accept(s);
    s.close();
    return s.reactiveSeq();
}
Also used : Counter(com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter)

Aggregations

Counter (com.oath.cyclops.types.reactive.QueueBasedSubscriber.Counter)6 Continuation (com.oath.cyclops.types.futurestream.Continuation)2 Publisher (org.reactivestreams.Publisher)2 com.oath.cyclops.async (com.oath.cyclops.async)1 com.oath.cyclops.async.adapters (com.oath.cyclops.async.adapters)1 Queue (com.oath.cyclops.async.adapters.Queue)1 DataWitness.reactiveSeq (com.oath.cyclops.hkt.DataWitness.reactiveSeq)1 Higher (com.oath.cyclops.hkt.Higher)1 OneShotStreamX (com.oath.cyclops.internal.stream.OneShotStreamX)1 com.oath.cyclops.internal.stream.spliterators (com.oath.cyclops.internal.stream.spliterators)1 ReversingDoubleArraySpliterator (com.oath.cyclops.internal.stream.spliterators.doubles.ReversingDoubleArraySpliterator)1 ReversingIntArraySpliterator (com.oath.cyclops.internal.stream.spliterators.ints.ReversingIntArraySpliterator)1 ReversingRangeIntSpliterator (com.oath.cyclops.internal.stream.spliterators.ints.ReversingRangeIntSpliterator)1 ReversingLongArraySpliterator (com.oath.cyclops.internal.stream.spliterators.longs.ReversingLongArraySpliterator)1 ReversingRangeLongSpliterator (com.oath.cyclops.internal.stream.spliterators.longs.ReversingRangeLongSpliterator)1 Unit (com.oath.cyclops.types.factory.Unit)1 Contains (com.oath.cyclops.types.foldable.Contains)1 To (com.oath.cyclops.types.foldable.To)1 ReactiveTransformable (com.oath.cyclops.types.functor.ReactiveTransformable)1 PersistentCollection (com.oath.cyclops.types.persistent.PersistentCollection)1