@Override public ReactiveSeq<T> stream(final Continueable s) { return connect(q -> q.stream(s)); }
/** * @return Infinite (until Queue is closed) Stream of CompletableFutures * that can be used as input into a SimpleReact concurrent dataflow * * This Stream itself is Sequential, SimpleReact will applyHKT * concurrency / parralellism via the constituent CompletableFutures * */ @Override public ReactiveSeq<CompletableFuture<T>> streamCompletableFutures() { return stream().map(CompletableFuture::completedFuture); }
/** * Generating a stream will register the Stream as a reactiveSubscriber to this topic. * It will be provided with an internal Queue as a mailbox. @see Topic.disconnect to disconnect from the topic * @return Stream of data */ @Override public ReactiveSeq<T> stream() { return connect(q -> q.stream()); }
private Stream<T> genJdkStream() { final Continueable subscription = new com.oath.cyclops.react.async.subscription.Subscription(); return queue.stream(subscription); }
@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); }
@Override public Iterator<T> iterator() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); AtomicBoolean wip = new AtomicBoolean(false); Subscription[] sub = {null}; 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().iterator(); } return new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).iterator(); }
@Test public void publishTest() { for (int k = 0; k < ITERATIONS; k++) { Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { try { System.out.println("Sleeping!"); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Waking!"); System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); of(1, 2, 3).peek(i -> System.out.println("publishing " + i)) .publishTo(queue) .forEach(System.out::println); assertThat(queue.stream().collect(Collectors.toList()), equalTo(Arrays.asList(1, 2, 3))); t = null; System.gc(); } }
return queue.stream();
return queue.stream();
/** * @return Infinite (until Queue is closed) Stream of CompletableFutures * that can be used as input into a SimpleReact concurrent dataflow * * This Stream itself is Sequential, SimpleReact will applyHKT * concurrency / parralellism via the constituent CompletableFutures * */ @Override public ReactiveSeq<CompletableFuture<T>> streamCompletableFutures() { return stream().map(CompletableFuture::completedFuture); }
/** * Generating a stream will register the Stream as a reactiveSubscriber to this topic. * It will be provided with an internal Queue as a mailbox. @see Topic.disconnect to disconnect from the topic * @return Stream of data */ @Override public ReactiveSeq<T> stream() { return connect(q -> q.stream()); }
@Override public ReactiveSeq<T> stream(final Continueable s) { return connect(q -> q.stream(s)); }
private Stream<T> genJdkStream() { final Continueable subscription = new com.oath.cyclops.react.async.subscription.Subscription(); return queue.stream(subscription); }
private Stream<T> genJdkStream() { final Continueable subscription = new com.oath.cyclops.react.async.subscription.Subscription(); return queue.stream(subscription); }
/** * Wrap a Stream into a SimpleReactStream. */ static <T> SimpleReactStream<T> simpleReactStream(Stream<T> stream) { if (stream instanceof FutureStream) stream = ((FutureStream) stream).toQueue() .stream(((FutureStream) stream).getSubscription()); final SimpleReact sr = new SimpleReact( ThreadPools.getCurrentThreadExecutor(), false); return new SimpleReactStreamImpl<T>( sr, stream.map(CompletableFuture::completedFuture)); }
private FutureStream<T> genStream() { final Continueable subscription = new com.oath.cyclops.react.async.subscription.Subscription(); return new LazyReact().of() .withSubscription(subscription) .fromStream(queue.stream(subscription)); }
default Iterator<U> iterator() { final Queue<U> q = toQueue(); if (getSubscription().closed()) return new CloseableIterator<>( Arrays.<U> asList() .iterator(), getSubscription(), null); return new CloseableIterator<>( q.stream(getSubscription()) .iterator(), getSubscription(), q); }
@Override default FutureStream<U> limit(final long maxSize) { final Continueable sub = this.getSubscription(); sub.registerLimit(maxSize); return fromStream(ReactiveSeq.oneShotStream(toQueue().stream(sub)) .limit(maxSize)); }
@Override default FutureStream<U> skip(final long n) { final Continueable sub = this.getSubscription(); sub.registerSkip(n); return fromStream(ReactiveSeq.oneShotStream(toQueue().stream(sub)) .skip(n)); }