io.reactivex.internal.subscriptions.SubscriptionHelper Java Examples

The following examples show how to use io.reactivex.internal.subscriptions.SubscriptionHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlowableRepeat.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        if (BackpressureHelper.add(this, n) == 0) {
            long requested = n;
            long emitted = 0;
            do {
                emitted = requested;
                while (requested-- > 0 && !cancelled && (count == -1 || counter-- > 0)) {
                    child.onNext(value);
                }
            } while ((requested = this.addAndGet(-emitted)) > 0);
            if (count >= 0 && !cancelled) {
                child.onComplete();
            }
        }
    }
}
 
Example #2
Source File: FlowableInsertMaybe.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        // modify request to upstream to account for inserted values
        // use a CAS loop because request can be called from any thread
        while (true) {
            long ins = inserted.get();
            long d = Math.min(ins, n);
            if (inserted.compareAndSet(ins, ins - d)) {
                if (n - d > 0) {
                    upstream.request(n - d);
                }
                break;
            }
        }
        drain();
    }
}
 
Example #3
Source File: FlowableInsertTimeout.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        // modify request to upstream to account for inserted values
        // use a CAS loop because request can be called from any thread
        while (true) {
            long ins = inserted.get();
            long d = Math.min(ins, n);
            if (inserted.compareAndSet(ins, ins - d)) {
                if (n - d > 0) {
                    upstream.request(n - d);
                }
                break;
            }
        }
        drain();
    }
}
 
Example #4
Source File: FlowableRepeatingTransform.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
protected void subscribeActual(Subscriber<? super T> child) {

    Flowable<T> f;
    try {
        f = transform.apply(source);
    } catch (Exception e) {
        Exceptions.throwIfFatal(e);
        child.onSubscribe(SubscriptionHelper.CANCELLED);
        child.onError(e);
        return;
    }
    AtomicReference<Chain<T>> chainRef = new AtomicReference<Chain<T>>();
    DestinationSerializedSubject<T> destination = new DestinationSerializedSubject<T>(child,
            chainRef);
    Chain<T> chain = new Chain<T>(transform, destination, maxIterations, maxChained, tester);
    chainRef.set(chain);
    // destination is not initially subscribed to the chain but will be when
    // tester function result completes
    destination.subscribe(child);
    ChainedReplaySubject<T> sub = ChainedReplaySubject.create(destination, chain, tester);
    chain.initialize(sub);
    f.onTerminateDetach() //
            .subscribe(sub);
}
 
Example #5
Source File: FlowableRepeatingTransform.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Override
public void request(long n) {
    debug(this + " request " + n);
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        while (true) {
            Subscription p = parent.get();
            long d = deferredRequests.get();
            if (d == -1) {
                // parent exists so can request of it
                debug(this + " requesting from parent " + n);
                p.request(n);
                break;
            } else {
                long d2 = d + n;
                if (d2 < 0) {
                    d2 = Long.MAX_VALUE;
                }
                if (deferredRequests.compareAndSet(d, d2)) {
                    break;
                }
            }
        }
        drain();
    }
}
 
Example #6
Source File: AbstractSubscriber.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(Throwable t) {
    if (SubscriptionHelper.cancel(subscription)) {
        hookOnError(t);
        downstreamSubscriber.onError(t);
    }
}
 
Example #7
Source File: FlowableSignalMapper.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
    if (SubscriptionHelper.validate(this.upstream, s)) {
        this.upstream = s;
        actual.onSubscribe(this);
    }
}
 
Example #8
Source File: ResourceFlowableObserveOn.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
    if (SubscriptionHelper.validate(this.upstream, s)) {
        this.upstream = s;

        actual.onSubscribe(this);

        s.request(bufferSize);
    }
}
 
Example #9
Source File: ResourceFlowableObserveOn.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        schedule();
    }
}
 
Example #10
Source File: FlowableStringInputStream.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws IOException {
    SubscriptionHelper.cancel(upstream);
    synchronized (this) {
        notifyAll();
    }
}
 
Example #11
Source File: FlowableStringInputStream.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
byte[] awaitBufferIfNecessary() throws IOException {
    byte[] a = bytes;
    if (a == null) {
        synchronized (this) {
            for (;;) {
                boolean d = done;
                a = bytes;
                if (a != null) {
                    break;
                }
                if (d || upstream.get() == SubscriptionHelper.CANCELLED) {
                    break;
                }
                try {
                    wait();
                } catch (InterruptedException ex) {
                    if (upstream.get() != SubscriptionHelper.CANCELLED) {
                        InterruptedIOException exc = new InterruptedIOException();
                        exc.initCause(ex);
                        throw exc;
                    }
                    break;
                }
            } 
        }
    }
    return a;
}
 
Example #12
Source File: FlowableCollectWhile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        parent.request(n);
        drain();
    }
}
 
Example #13
Source File: FlowableStateMachine.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription parent) {
    if (SubscriptionHelper.validate(this.parent, parent)) {
        this.parent = parent;
        child.onSubscribe(this);
    }
}
 
Example #14
Source File: FlowableStateMachine.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        drain();
    }
}
 
Example #15
Source File: FlowableCollectWhile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription parent) {
    if (SubscriptionHelper.validate(this.parent, parent)) {
        this.parent = parent;
        child.onSubscribe(this);
    }
}
 
Example #16
Source File: FlowableStringInputStream.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws IOException {
    SubscriptionHelper.cancel(upstream);
    synchronized (this) {
        notifyAll();
    }
}
 
Example #17
Source File: FlowableDoNothing.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription upstream) {
    // this method is responsible for notifying downstream
    // where to send requests and cancellation by calling
    // downstream.onSubscribe with a Subscription object
    // (we reuse `this`)

    if (SubscriptionHelper.validate(this.upstream, upstream)) {
        downstream.onSubscribe(this);
    }
}
 
Example #18
Source File: FlowableDoNothing.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    // requests received from downstream are passed to upstream

    // note that upstream is non-null because by specification
    // onSubscribe must terminate before request or cancel is
    // called on the Subscription passed into onSubscribe.
    if (SubscriptionHelper.validate(n)) {
        upstream.request(n);
    }
}
 
Example #19
Source File: AbstractSubscriber.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Override
public void onComplete() {
    if (SubscriptionHelper.cancel(subscription)) {
        hookOnComplete();
        downstreamSubscriber.onComplete();
    }
}
 
Example #20
Source File: WriteStreamSubscriberImpl.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
  Objects.requireNonNull(subscription, "subscription");
  if (!setSubscription(subscription)) {
    subscription.cancel();
    SubscriptionHelper.reportSubscriptionSet();
    return;
  }
  writeStream.exceptionHandler(t -> {
    if (!setDone()) {
      RxJavaPlugins.onError(t);
      return;
    }
    getSubscription().cancel();
    Consumer<? super Throwable> c;
    synchronized (this) {
      c = this.writeStreamExceptionHandler;
    }
    if (c != null) {
      try {
        c.accept(t);
      } catch (Exception e) {
        RxJavaPlugins.onError(e);
      }
    }
  });
  writeStream.drainHandler(v -> requestMore());
  requestMore();
}
 
Example #21
Source File: FlowableMergeInterleave.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
void cancel() {
    while (true) {
        Subscription s = subscription.get();
        if (subscription.compareAndSet(s, SubscriptionHelper.CANCELLED)) {
            s.cancel();
            break;
        }
    }
}
 
Example #22
Source File: FlowableMergeInterleave.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        if (once.compareAndSet(false, true)) {
            sources.subscribe(this);
            subscription.request(maxConcurrent);
        }
        drain();
    }
}
 
Example #23
Source File: FlowableInsertMaybe.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription upstream) {
    if (SubscriptionHelper.validate(this.upstream, upstream)) {
        this.upstream = upstream;
        downstream.onSubscribe(this);
    }
}
 
Example #24
Source File: FlowableRepeatingTransform.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    debug(this + " request " + n);
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        while (true) {
            Requests<T> r = requests.get();
            Requests<T> r2;
            if (r.parent == null) {
                long d = r.deferred + n;
                if (d < 0) {
                    d = Long.MAX_VALUE;
                }
                r2 = new Requests<T>(r.parent, r.unreconciled, d, r.child);
                if (requests.compareAndSet(r, r2)) {
                    break;
                }
            } else {
                long x = n + r.deferred - r.unreconciled;
                long u = Math.max(0, -x);
                r2 = new Requests<T>(r.parent, u, 0, r.child);
                if (requests.compareAndSet(r, r2)) {
                    if (x > 0) {
                        r.parent.request(x);
                    }
                    break;
                }
            }
        }
        drain();
    }
}
 
Example #25
Source File: FlowableOnBackpressureBufferToFile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        scheduleDrain();
    }
}
 
Example #26
Source File: FlowableOnBackpressureBufferToFile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        parent.request(n);
        scheduleDrain();
    }
}
 
Example #27
Source File: FlowableOnBackpressureBufferToFile.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription parent) {
    if (SubscriptionHelper.validate(this.parent, parent)) {
        this.parent = parent;
        child.onSubscribe(this);
    }
}
 
Example #28
Source File: LifeSubscriber.java    From rxjava-RxLife with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
    if (SubscriptionHelper.setOnce(this, s)) {
        try {
            addObserver();
            downstream.onSubscribe(s);
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            s.cancel();
            onError(ex);
        }
    }
}
 
Example #29
Source File: FlowableStringInputStream.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
byte[] awaitBufferIfNecessary() throws IOException {
    byte[] a = bytes;
    if (a == null) {
        synchronized (this) {
            for (;;) {
                boolean d = done;
                a = bytes;
                if (a != null) {
                    break;
                }
                if (d || upstream.get() == SubscriptionHelper.CANCELLED) {
                    break;
                }
                try {
                    wait();
                } catch (InterruptedException ex) {
                    if (upstream.get() != SubscriptionHelper.CANCELLED) {
                        InterruptedIOException exc = new InterruptedIOException();
                        exc.initCause(ex);
                        throw exc;
                    }
                    break;
                }
            } 
        }
    }
    return a;
}
 
Example #30
Source File: FlowableMatch.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Override
public void request(long n) {
    if (SubscriptionHelper.validate(n)) {
        BackpressureHelper.add(requested, n);
        drain();
    }
}