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