protected <U> ReactiveSeq<U> of(U... array){ return Spouts.async(s->{ Thread t = new Thread(()-> { for (U next : array) { s.onNext(next); } s.onComplete(); }); t.start(); }); }
static <T> ReactiveSeq<T> asyncBuffer(Queue<T> buffer,BufferOverflowPolicy policy, Consumer<? super PushSubscriber<T>> onNext){ return Spouts.asyncStream(new BufferingSinkOperator<T>(buffer, c-> onNext.accept(PushSubscriber.of(c)), policy)); } static <T> ReactiveSeq<T> reactive(Stream<T> seq, Executor exec){
@Override public void onError(Throwable t) { PushSubscriber.this.onError(t); }
@Override protected <U> ReactiveSeq<U> of(U... array){ int[] index = {0}; return Spouts.async(s->{ System.out.println("S "+ System.identityHashCode(s)); new Thread(()-> { /**try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }**/ while(!s.isInitialized()){ LockSupport.parkNanos(1l); } for (U next : array) { s.onNext(next); if(index[0]++>100) break; } s.onComplete(); }).start(); }); } @Test
@Override public void onComplete() { PushSubscriber.this.onComplete(); } };
@Override public void onNext(T t) { PushSubscriber.this.onNext(t); }
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()));
protected ReactiveSeq<Long> rangeLong(long start,long end){ return Spouts.async(s->{ Thread t = new Thread(()-> { rangeLong(start,end).forEach(s::onNext); s.onComplete(); }); t.start(); }); } @Test
@Test public void asyncBufferBlock() throws InterruptedException { Subscription sub = Spouts.asyncBufferBlock(10, s -> { if (i == 0) { Effect e = () -> { s.onNext("hello " + i++); }; e.cycle(30).runAsync(); } } ).forEach(2, in->count++); Thread.sleep(500); sub.request(30); Thread.sleep(500); assertThat(i,equalTo(30)); assertThat(count,equalTo(30)); } @Test
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()));
protected <U> ReactiveSeq<U> of(U... array){ return Spouts.async(s->{ Thread t = new Thread(()-> { for (U next : array) { s.onNext(next); } s.onComplete(); }); t.start(); }); } protected ReactiveSeq<Integer> range(int start,int end){
protected ReactiveSeq<Integer> range(int start,int end){ return Spouts.async(s->{ Thread t = new Thread(()-> { range(start,end).forEach(s::onNext); s.onComplete(); }); t.start(); }); } protected ReactiveSeq<Long> rangeLong(long start,long end){
@Override public void onNext(T t) { PushSubscriber.this.onNext(t); }
static <T> ReactiveSeq<T> asyncBufferBlock(int buffer, Consumer<? super PushSubscriber<T>> onNext){ return Spouts.asyncStream(new BufferingSinkOperator<T>(new ManyToManyConcurrentArrayQueue<T>(buffer), c-> onNext.accept(PushSubscriber.of(c)), BufferOverflowPolicy.BLOCK)); } static <T> ReactiveSeq<T> asyncBuffer(Queue<T> buffer,BufferOverflowPolicy policy, Consumer<? super PushSubscriber<T>> onNext){
@Override public void onError(Throwable t) { PushSubscriber.this.onError(t); }
protected <U> ReactiveSeq<U> of(U... array){ return Spouts.async(s->{ Thread t = new Thread(()-> { for (U next : array) { s.onNext(next); } s.onComplete(); }); t.start(); }); } protected ReactiveSeq<Integer> range(int start,int end){
protected ReactiveSeq<Integer> range(int start,int end){ return Spouts.async(s->{ Thread t = new Thread(()-> { range(start,end).forEach(s::onNext); s.onComplete(); }); t.start(); }); } @Test @Ignore
static <T> ReactiveSeq<T> asyncBuffer(int buffer, Consumer<? super PushSubscriber<T>> onNext){ return Spouts.asyncStream(new BufferingSinkOperator<T>(new ManyToManyConcurrentArrayQueue<T>(buffer),c-> onNext.accept(PushSubscriber.of(c)), BufferOverflowPolicy.DROP)); } static <T> ReactiveSeq<T> asyncBufferBlock(int buffer, Consumer<? super PushSubscriber<T>> onNext){
protected <U> ReactiveSeq<U> of(U... array){ return Spouts.async(s->{ Thread t = new Thread(()-> { for (U next : array) { s.onNext(next); } s.onComplete(); }); t.start(); }); } @Test
@Override public void onComplete() { PushSubscriber.this.onComplete(); } };