Exemple #1
0
  @Override
  public void subscribe(final Subscriber<? super T> subscriber) {
    try {
      if (supplier != null) {
        subscriber.onSubscribe(
            new PushSubscription<T>(this, subscriber) {

              @Override
              public void request(long elements) {
                try {
                  supplyValue(subscriber);
                } catch (Throwable throwable) {
                  subscriber.onError(throwable);
                }
              }
            });

      } else {
        subscriber.onComplete();
      }
    } catch (Throwable throwable) {
      Exceptions.throwIfFatal(throwable);
      subscriber.onError(throwable);
    }
  }
  @Override
  public void subscribe(Subscriber<? super T> s) {
    Consumer<Runnable> scheduler;

    try {
      scheduler = schedulerFactory.call();
    } catch (Throwable e) {
      Exceptions.throwIfFatal(e);
      EmptySubscription.error(s, e);
      return;
    }

    if (scheduler == null) {
      EmptySubscription.error(
          s, new NullPointerException("The schedulerFactory returned a null Function"));
      return;
    }

    if (value == null) {
      FluxPublishOn.ScheduledEmptySubscriptionEager parent =
          new FluxPublishOn.ScheduledEmptySubscriptionEager(s, scheduler);
      s.onSubscribe(parent);
      scheduler.accept(parent);
    } else {
      s.onSubscribe(new FluxPublishOn.ScheduledSubscriptionEagerCancel<>(s, value, scheduler));
    }
  }
    @Override
    public void onSubscribe(Subscription s) {
      if (SubscriptionHelper.validateSubscription(this.s, s)) {
        return;
      }
      this.s = s;

      Subscriber<? super U> actual = this.actual;

      U b;

      try {
        b = bufferSupplier.get();
      } catch (Throwable e) {
        cancelled = true;
        s.cancel();
        EmptySubscription.error(e, actual);
        return;
      }

      if (b == null) {
        cancelled = true;
        s.cancel();
        EmptySubscription.error(new NullPointerException("The buffer supplied is null"), actual);
        return;
      }
      buffer = b;

      Publisher<B> boundary;

      try {
        boundary = boundarySupplier.get();
      } catch (Throwable ex) {
        cancelled = true;
        s.cancel();
        EmptySubscription.error(ex, actual);
        return;
      }

      if (boundary == null) {
        cancelled = true;
        s.cancel();
        EmptySubscription.error(
            new NullPointerException("The boundary publisher supplied is null"), actual);
        return;
      }

      BufferBoundarySubscriber<T, U, B> bs = new BufferBoundarySubscriber<T, U, B>(this);
      other = bs;

      actual.onSubscribe(this);

      if (!cancelled) {
        s.request(Long.MAX_VALUE);

        boundary.subscribe(bs);
      }
    }
 @Override
 public void subscribe(final Subscriber<? super QueueSignal> subscriber) {
   try {
     if (refCount.getAndIncrement() == 0) {
       this.channel = lapin.createChannel();
     }
     subscriber.onSubscribe(createSubscription(subscriber, null, null));
   } catch (Exception e) {
     refCount.incrementAndGet();
     subscriber.onError(e);
   }
 }
    void drain() {
      if (WIP.getAndIncrement(this) != 0) {
        return;
      }
      final Subscriber<? super T> a = actual;

      int missed = 1;

      for (; ; ) {

        if (checkTerminated(done, value == null, a)) {
          return;
        }

        long r = requested;
        long e = 0L;

        while (r != e) {
          boolean d = done;

          @SuppressWarnings("unchecked")
          T v = (T) VALUE.getAndSet(this, null);

          boolean empty = v == null;

          if (checkTerminated(d, empty, a)) {
            return;
          }

          if (empty) {
            break;
          }

          a.onNext(v);

          e++;
        }

        if (r == e && checkTerminated(done, value == null, a)) {
          return;
        }

        if (e != 0L && r != Long.MAX_VALUE) {
          REQUESTED.addAndGet(this, -e);
        }

        missed = WIP.addAndGet(this, -missed);
        if (missed == 0) {
          break;
        }
      }
    }
 @Override
 public void run() {
   if (requested) {
     if (cancel != CANCELLED) {
       s.onNext(0L);
     }
     asyncExecutor.apply(null);
     if (cancel != CANCELLED) {
       s.onComplete();
     }
   } else {
     s.onError(new IllegalStateException("Could not emit value due to lack of requests"));
   }
 }
Exemple #7
0
    void slowPath(long n) {
      final T[] a = array;
      final int len = a.length;
      final Subscriber<? super T> s = actual;

      int i = index;
      int e = 0;

      for (; ; ) {
        if (cancelled) {
          return;
        }

        while (i != len && e != n) {
          T t = a[i];

          if (t == null) {
            s.onError(new NullPointerException("The " + i + "th array element was null"));
            return;
          }

          s.onNext(t);

          if (cancelled) {
            return;
          }

          i++;
          e++;
        }

        if (i == len) {
          s.onComplete();
          return;
        }

        n = requested;

        if (n == e) {
          index = i;
          n = REQUESTED.addAndGet(this, -e);
          if (n == 0) {
            return;
          }
          e = 0;
        }
      }
    }
 @Override
 public void onError(final Throwable e) {
   // TODO throw if fatal ?;
   FastList list;
   synchronized (this) {
     if (terminated) {
       return;
     }
     if (emitting) {
       if (queue == null) {
         queue = new FastList();
       }
       queue.add(new ErrorSentinel(e));
       return;
     }
     emitting = true;
     list = queue;
     queue = null;
   }
   drainQueue(list);
   delegate.onError(e);
   synchronized (this) {
     emitting = false;
   }
 }
    @Override
    public void next(Object value) {
      if (value == null) {
        fail(new NullPointerException("value is null"));
        return;
      }
      if (done) {
        Exceptions.onNextDropped(value);
        return;
      }
      if (caughtUp && actual != null) {
        try {
          actual.onNext(value);
        } finally {
          ch.read();
          ReferenceCountUtil.release(value);
        }

      } else {
        Queue<Object> q = queue;
        if (q == null) {
          q = QueueSupplier.unbounded().get();
          queue = q;
        }
        q.offer(value);
        if (drain()) {
          caughtUp = true;
        }
      }
    }
  @Override
  public void subscribe(Subscriber<? super Object> s) {
    if (log.isDebugEnabled()) {
      log.debug(
          "Subscribing inbound receiver [pending: "
              + ""
              + inboundEmitter.getPending()
              + ", done: "
              + inboundEmitter.done
              + "]");
    }
    if (inboundEmitter.actual == null) {
      if (inboundEmitter.done) {
        if (inboundEmitter.error != null) {
          Operators.error(s, inboundEmitter.error);
          return;
        } else if (inboundEmitter.getPending() == 0) {
          Operators.complete(s);
          return;
        }
      }

      inboundEmitter.init(s);
      s.onSubscribe(inboundEmitter);
    } else {
      Operators.error(
          s, new IllegalStateException("Only one connection receive subscriber allowed."));
    }
  }
  @Override
  public void subscribe(Subscriber<? super Long> s) {
    PublisherTimerRunnable r = new PublisherTimerRunnable(s, asyncExecutor);

    s.onSubscribe(r);

    r.setCancel(asyncExecutor.apply(r));
  }
 @Override
 public void onSubscribe(Subscription s) {
   if (SubscriptionHelper.validateSubscription(this.s, s)) {
     return;
   }
   this.s = s;
   actual.onSubscribe(s);
 }
Exemple #13
0
 @Override
 public Subscriber<? super IN> apply(Subscriber<? super IN> subscriber) {
   long newId = uniqueId++;
   if ((options & SUBSCRIBE) == SUBSCRIBE && log.isInfoEnabled()) {
     log.trace("subscribe: [{}] {}", newId, subscriber.getClass().getSimpleName());
   }
   return new LoggerBarrier<>(this, newId, subscriber);
 }
Exemple #14
0
 @Override
 public void subscribe(Subscriber<? super T> s) {
   if (array.length == 0) {
     EmptySubscription.complete(s);
     return;
   }
   s.onSubscribe(new FluxArraySubscription<>(s, array));
 }
    @Override
    public void request(long n) {
      if (SubscriptionHelper.validateRequest(n)) {
        return;
      }
      if (compareAndSet(false, true)) {
        int i = 0;
        final Subscriber<? super Integer> a = s;
        final AtomicInteger c = counter;

        while (!cancelled) {
          a.onNext(i++);
          c.incrementAndGet();
        }
        System.out.println("unsubscribed after: " + i);
      }
    }
    @Override
    public void onComplete() {
      if (done) {
        return;
      }
      done = true;

      actual.onComplete();
    }
    @Override
    public void onSubscribe(Subscription s) {
      if (Operators.validate(this.s, s)) {
        this.s = s;

        actual.onSubscribe(this);

        s.request(Long.MAX_VALUE);
      }
    }
    @Override
    public void onError(Throwable t) {
      if (done) {
        RxJavaPlugins.onError(t);
        return;
      }
      done = true;

      actual.onError(t);
    }
 @Override
 public void complete() {
   if (isCancelled() || done) {
     return;
   }
   done = true;
   if (caughtUp && actual != null) {
     actual.onComplete();
   }
   drain();
 }
 @Override
 void subscribe(
     AbstractRequestBodyPublisher publisher, Subscriber<? super DataBuffer> subscriber) {
   Objects.requireNonNull(subscriber);
   if (publisher.changeState(this, NO_DEMAND)) {
     Subscription subscription = new RequestBodySubscription(publisher);
     publisher.subscriber = subscriber;
     subscriber.onSubscribe(subscription);
   } else {
     throw new IllegalStateException(toString());
   }
 }
    boolean checkTerminated(boolean d, boolean empty, Subscriber<? super T> a) {
      if (cancelled) {
        VALUE.lazySet(this, null);
        return true;
      }

      if (d) {
        Throwable e = error;
        if (e != null) {
          VALUE.lazySet(this, null);

          a.onError(e);
          return true;
        } else if (empty) {
          a.onComplete();
          return true;
        }
      }

      return false;
    }
    @Override
    public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
      if (subscriber == null) {
        throw Exceptions.spec_2_13_exception();
      }
      if (this.subscriber != null) {
        subscriber.onError(new IllegalStateException("Only one subscriber allowed"));
      }

      this.subscriber = subscriber;
      this.subscriber.onSubscribe(new RequestBodySubscription());
    }
    @Override
    public void onNext(T t) {
      if (done) {
        return;
      }
      try {
        onNext.accept(t);
      } catch (Throwable e) {
        s.cancel();
        onError(e);
        return;
      }

      actual.onNext(t);
    }
 void drainQueue(FastList list) {
   if (list == null || list.size == 0) {
     return;
   }
   Object v;
   for (int i = 0; i < list.size; i++) {
     v = list.array[i];
     if (v == null) {
       break;
     }
     if (v == NULL_SENTINEL) {
       delegate.onNext(null);
     } else if (v == COMPLETE_SENTINEL) {
       delegate.onComplete();
       return;
     } else if (v.getClass() == ErrorSentinel.class) {
       delegate.onError(((ErrorSentinel) v).e);
     } else {
       @SuppressWarnings("unchecked")
       T t = (T) v;
       delegate.onNext(t);
     }
   }
 }
Exemple #25
0
    void fastPath() {
      final T[] a = array;
      final int len = a.length;
      final Subscriber<? super T> s = actual;

      for (int i = index; i != len; i++) {
        if (cancelled) {
          return;
        }

        T t = a[i];

        if (t == null) {
          s.onError(new NullPointerException("The " + i + "th array element was null"));
          return;
        }

        s.onNext(t);
      }
      if (cancelled) {
        return;
      }
      s.onComplete();
    }
    void fastPath() {
      final Iterator<? extends T> a = iterator;
      final Subscriber<? super T> s = actual;

      for (; ; ) {

        if (cancelled) {
          return;
        }

        T t;

        try {
          t = a.next();
        } catch (Exception ex) {
          s.onError(ex);
          return;
        }

        if (cancelled) {
          return;
        }

        if (t == null) {
          s.onError(new NullPointerException("The iterator returned a null value"));
          return;
        }

        s.onNext(t);

        if (cancelled) {
          return;
        }

        boolean b;

        try {
          b = a.hasNext();
        } catch (Exception ex) {
          s.onError(ex);
          return;
        }

        if (cancelled) {
          return;
        }

        if (!b) {
          s.onComplete();
          return;
        }
      }
    }
 @Override
 public void fail(Throwable error) {
   if (error == null) {
     error = new NullPointerException("error is null");
   }
   if (isCancelled() || done) {
     Exceptions.onErrorDropped(error);
     return;
   }
   done = true;
   if (caughtUp && actual != null) {
     actual.onError(error);
   } else {
     this.error = error;
     done = true;
     drain();
   }
 }
 @Override
 public void onNext(Try<Optional<T>> t) {
   if (done) {
     return;
   }
   if (t.hasError()) {
     s.cancel();
     onError(t.error());
   } else {
     Optional<T> o = t.value();
     if (o.isPresent()) {
       actual.onNext(o.get());
     } else {
       s.cancel();
       onComplete();
     }
   }
 }
    @Override
    public void onError(Throwable t) {
      if (done) {
        RxJavaPlugins.onError(t);
        return;
      }
      done = true;
      try {
        onError.accept(t);
      } catch (Throwable e) {
        t.addSuppressed(e);
      }
      actual.onError(t);

      try {
        onAfterTerminate.run();
      } catch (Throwable e) {
        RxJavaPlugins.onError(e);
      }
    }
  /**
   * Common method to take an Iterator as a source of values.
   *
   * @param s
   * @param it
   */
  static <T> void subscribe(Subscriber<? super T> s, Iterator<? extends T> it) {
    if (it == null) {
      EmptySubscription.error(s, new NullPointerException("The iterator is null"));
      return;
    }

    boolean b;

    try {
      b = it.hasNext();
    } catch (Throwable e) {
      EmptySubscription.error(s, e);
      return;
    }
    if (!b) {
      EmptySubscription.complete(s);
      return;
    }

    s.onSubscribe(new PublisherIterableSubscription<>(s, it));
  }