/** * Create a Stream that accepts data via the Subsriber passed into the supplied Consumer. * reactive-streams susbscription is ignored (i.e. this Stream is backpressure free) * * <pre> * {@code * ReactiveSeq<Integer> input = Spouts.async(subscriber->{ * listener.onEvent(subscriber::onNext); * listener.onError(susbscriber::onError); * closeListener.onEvent(subscriber::onClose); * }); * } * </pre> * * @param sub * @param <T> * @return */ static <T> ReactiveSeq<T> async(Consumer<? super PushSubscriber<T>> sub){ AsyncSubscriber<T> s = asyncSubscriber(); return s.registerAndstream(()->{ while(!s.isInitialized()){ LockSupport.parkNanos(1l); } sub.accept(s); }); }
/** * Create an Subscriber for Observable style asynchronous push based Streams. * Streams generated from AsyncSubscribers are not backpressure aware (in cases * where backpressue is not needed they may perform better). * For backpressure aware Streams see {@link Spouts#reactiveSubscriber} * * @param <T> Stream data type * @return Async Stream Subscriber */ static <T> AsyncSubscriber<T> asyncSubscriber(){ return new AsyncSubscriber<T>(); }
@Override public void onError(final Throwable t) { Objects.requireNonNull(t); val cons = getAction().getError(); // if(cons!=null) cons.accept(t); }
private ReactiveSeq<Integer> nextAsync() { AsyncSubscriber<Integer> sub = Spouts.asyncSubscriber(); new Thread(()->{ sub.awaitInitialization(); try { //not a reactive-stream so we don't know with certainty when demand signalled Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sub.onNext(1); sub.onNext(2); sub.onComplete(); }).start(); return sub.stream(); } }
Thread t = new Thread(() -> { while (!sub.isInitialized()) { sub.onNext(1); sub.onComplete(); }); System.out.println("Setting up Stream!"); assertThat(sub.stream().peek(System.out::println).collect(Collectors.toList()).size(), equalTo(100)); sub.onNext(1); System.out.println("End!");
public void awaitInitialization(){ while(!isInitialized()){ LockSupport.parkNanos(0l); } }
@Test @Ignore public void limitLast(){ AsyncSubscriber<String> pushable = Spouts.asyncSubscriber(); ReactiveSeq<String> stream = pushable.stream(); pushable.onNext("hello1"); pushable.onNext("hello2"); pushable.onNext("hello3"); pushable.onComplete(); // stream.printOut(); stream.takeRight(2).zipWithStream(Stream.of(1,2)).printOut(); }
private Stream<Integer> nextAsync() { AsyncSubscriber<Integer> sub = Spouts.asyncSubscriber(); new Thread(()->{ sub.awaitInitialization(); try { //not a reactive-stream so we don't know with certainty when demand signalled Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sub.onNext(1); sub.onNext(2); sub.onComplete(); }).start(); return sub.stream(); } }
public void awaitInitialization(){ while(!isInitialized()){ LockSupport.parkNanos(0l); } }
private ReactiveSeq<Integer> nextAsync() { AsyncSubscriber<Integer> sub = Spouts.asyncSubscriber(); new Thread(()->{ sub.awaitInitialization(); try { //not a reactive-stream so we don't know with certainty when demand signalled Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sub.onNext(1); sub.onNext(2); sub.onComplete(); }).start(); return sub.stream(); }
/** * Create a Stream that accepts data via the Subsriber passed into the supplied Consumer. * reactive-streams susbscription is ignored (i.e. this Stream is backpressure free) * * <pre> * {@code * ReactiveSeq<Integer> input = Spouts.async(subscriber->{ * listener.onEvent(subscriber::onNext); * listener.onError(susbscriber::onError); * closeListener.onEvent(subscriber::onClose); * }); * } * </pre> * * @param sub * @param <T> * @return */ static <T> ReactiveSeq<T> async(Consumer<? super PushSubscriber<T>> sub){ AsyncSubscriber<T> s = asyncSubscriber(); return s.registerAndstream(()->{ while(!s.isInitialized()){ LockSupport.parkNanos(1l); } sub.accept(s); }); }
@Override public void onNext(final T t) { val cons = getAction().getAction(); //if (cons != null) cons.accept(t); }
/** * Create an Subscriber for Observable style asynchronous push based Streams. * Streams generated from AsyncSubscribers are not backpressure aware (in cases * where backpressue is not needed they may perform better). * For backpressure aware Streams see {@link Spouts#reactiveSubscriber} * * @param <T> Stream data type * @return Async Stream Subscriber */ static <T> AsyncSubscriber<T> asyncSubscriber(){ return new AsyncSubscriber<T>(); }
private Stream<Integer> nextAsync() { AsyncSubscriber<Integer> sub = Spouts.asyncSubscriber(); new Thread(()->{ sub.awaitInitialization(); try { //not a reactive-stream so we don't know with certainty when demand signalled Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sub.onNext(1); sub.onNext(2); sub.onComplete(); }).start(); return sub.stream(); } }
@Override public void onComplete() { val run = getAction().getOnComplete(); // if(run!=null) run.run(); } public boolean isInitialized() {
private ReactiveSeq<Integer> nextAsync() { AsyncSubscriber<Integer> sub = Spouts.asyncSubscriber(); new Thread(()->{ sub.awaitInitialization(); try { //not a reactive-stream so we don't know with certainty when demand signalled Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sub.onNext(1); sub.onNext(2); sub.onComplete(); }).start(); return sub.stream(); } }
public ReactiveSeq<T> registerAndstream(Runnable r){ streamCreated = true; return Spouts.asyncStream(getAction(r)); }
private ReactiveSeq<Integer> nextAsync() { AsyncSubscriber<Integer> sub = Spouts.asyncSubscriber(); new Thread(()->{ sub.awaitInitialization(); try { //not a reactive-stream so we don't know with certainty when demand signalled Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sub.onNext(1); sub.onNext(2); sub.onComplete(); }).start(); return sub.stream(); } }
public boolean isInitialized() { return getAction().isInitialized(); }
/** * <pre> * {@code * PushSubscriber<Integer> sub = Spouts.asyncSubscriber(); * * //on a seperate thread * for(int i=0;i<100;i++) sub.onNext(i); sub.onComplete(); * * //on the main thread * * sub.stream() * .peek(System.out::println) * .collect(CyclopsCollectors.toList()); * * //note JDK Stream based terminal operations may block the current thread * //see ReactiveSeq#collectStream ReactiveSeq#foldAll for non-blocking alternatives * } * </pre> * * @return A push-based asychronous event driven Observable-style Stream that avoids the overhead of backpressure support **/ public ReactiveSeq<T> stream(){ streamCreated = true; return Spouts.asyncStream(getAction(()->{})); } public ReactiveSeq<T> registerAndstream(Runnable r){