Ejemplo n.º 1
0
      @Override
      public void onCompleted() {
        parentCompleted = true;
        // this *can* occur before the children are done, so if it does we won't send onCompleted
        // but will let the child worry about it
        // if however this completes and there are no children processing, then we will send
        // onCompleted

        if (childObservers.size() == 0) {
          if (!stopped.get()) {
            if (ourSubscription.stop()) {
              if (onErrorReceived.size() == 1) {
                // an onError was received from 1 ChildObserver so we now send it as a delayed error
                actualObserver.onError(onErrorReceived.peek());
              } else if (onErrorReceived.size() > 1) {
                // an onError was received from more than 1 ChildObserver so we now send it as a
                // delayed error
                actualObserver.onError(new CompositeException(onErrorReceived));
              } else {
                // no delayed error so send onCompleted
                actualObserver.onCompleted();
              }
            }
          }
        }
      }
Ejemplo n.º 2
0
  public static <T> boolean invokeStages(
      final Observer<StageChange> stageObserver,
      final Class<T> type,
      final T target,
      final Collection<String> stages)
      throws Throwable {
    if (stages == null || stages.isEmpty()) return true;

    boolean success = true;
    for (String stage : stages) {
      if (stageObserver != null)
        stageObserver.onNext(new StageChangeImpl(type, target, Stage.STARTING, stage));
      success &= invokeEventHandlers(stageObserver, type, target, stage, StageEvent.BEFORE_STAGE);

      if (stageObserver != null)
        stageObserver.onNext(new StageChangeImpl(type, target, Stage.STARTED, stage));
      final List<String> nextStages = invokeStageHandlers(stageObserver, type, target, stage);
      success &= nextStages != null;

      if (stageObserver != null)
        stageObserver.onNext(new StageChangeImpl(type, target, Stage.STOPPING, stage));
      success &= invokeEventHandlers(stageObserver, type, target, stage, StageEvent.AFTER_STAGE);

      if (nextStages != null && !nextStages.isEmpty()) {
        LOG.trace("Starting *nested* stage: " + nextStages);
        invokeStages(stageObserver, type, target, nextStages);
      }
    }
    return success;
  }
Ejemplo n.º 3
0
 @Override
 public Subscription onSubscribe(Observer<? super TResult> t1) {
   VirtualList<TIntermediate> values;
   Throwable error;
   state.lock();
   try {
     if (!state.done) {
       state.onSubscription.call();
       return state.addReplayer(t1);
     }
     values = state.values;
     error = state.error;
   } finally {
     state.unlock();
   }
   // fully replay the subject
   for (int i = values.start(); i < values.end(); i++) {
     try {
       t1.onNext(state.resultSelector.call(values.get(i)));
     } catch (Throwable t) {
       t1.onError(t);
       return Subscriptions.empty();
     }
   }
   if (error != null) {
     t1.onError(error);
   } else {
     t1.onCompleted();
   }
   return Subscriptions.empty();
 }
Ejemplo n.º 4
0
      @Override
      public Subscription subscribe(Observer<String> observer) {

        observer.onNext("hello");
        observer.onCompleted();

        return new Subscription() {

          @Override
          public void unsubscribe() {
            // unregister ... will never be called here since we are executing synchronously
          }
        };
      }
Ejemplo n.º 5
0
 @Override
 public void onNext(T args) {
   final int count = counter.incrementAndGet();
   if (count <= num) {
     observer.onNext(args);
     if (count == num) {
       observer.onCompleted();
     }
   }
   if (count >= num) {
     // this will work if the sequence is asynchronous, it will have no effect on a synchronous
     // observable
     subscription.unsubscribe();
   }
 }
Ejemplo n.º 6
0
 @Override
 public void onNext(T args) {
   // skip them until we reach the 'num' value
   if (counter.incrementAndGet() > num) {
     observer.onNext(args);
   }
 }
Ejemplo n.º 7
0
 @Override
 public void onNext(T args) {
   // in case the Observable is poorly behaved and doesn't listen to the unsubscribe request
   // we'll ignore anything that comes in after we've unsubscribed
   if (!stopped.get()) {
     actualObserver.onNext(args);
   }
 }
Ejemplo n.º 8
0
 @Override
 protected void channelRead0(
     ChannelHandlerContext ctx, io.netty.channel.socket.DatagramPacket message)
     throws Exception {
   byte[] bytes = new byte[message.content().readableBytes()];
   message.content().readBytes(bytes);
   obs.onNext(bytes);
 }
Ejemplo n.º 9
0
 @Override
 public void onError(Exception e) {
   if (!stopped.get()) {
     if (ourSubscription.stop()) {
       // this thread 'won' the race to unsubscribe/stop so let's send the error
       actualObserver.onError(e);
     }
   }
 }
Ejemplo n.º 10
0
 /**
  * onComplete and onError when called need to check for the parent being complete and if so
  * send the onCompleted or onError to the actualObserver.
  *
  * <p>This does NOT get invoked if synchronous execution occurs, but will when asynchronously
  * executing.
  *
  * <p>TestCase testErrorDelayed4WithThreading specifically tests this use case.
  */
 private void finishObserver() {
   if (childObservers.size() == 0 && parentCompleted) {
     if (ourSubscription.stop()) {
       // this thread 'won' the race to unsubscribe/stop so let's send onError or onCompleted
       if (onErrorReceived.size() == 1) {
         // an onError was received from 1 ChildObserver so we now send it as a delayed error
         actualObserver.onError(onErrorReceived.peek());
       } else if (onErrorReceived.size() > 1) {
         // an onError was received from more than 1 ChildObserver so we now send it as a
         // delayed error
         actualObserver.onError(new CompositeException(onErrorReceived));
       } else {
         // no delayed error so send onCompleted
         actualObserver.onCompleted();
       }
     }
   }
 }
Ejemplo n.º 11
0
 @Override
 public void onNext(T args) {
   // in case the Observable is poorly behaved and doesn't listen to the unsubscribe request
   // we'll ignore anything that comes in after we've unsubscribed or an onError has been
   // received and delayed
   if (!stopped.get() && !finished) {
     actualObserver.onNext(args);
   }
 }
Ejemplo n.º 12
0
 @Override
 public void onNext(T args) {
   if (predicate.call(args, counter.getAndIncrement())) {
     observer.onNext(args);
   } else {
     // this will work if the sequence is asynchronous, it will have no effect on a synchronous
     // observable
     subscription.unsubscribe();
   }
 }
Ejemplo n.º 13
0
 @Override
 public void onError(Throwable e) {
   /*
    * Cancel previous subscription if it has not already executed.
    * Expected that some race-condition will occur as this is crossing over thread boundaries
    * We are using SynchronizedObserver around 'observer' to handle interleaving and out-of-order calls.
    */
   lastScheduledNotification.get().unsubscribe();
   observer.onError(e);
 }
Ejemplo n.º 14
0
      @Override
      public Subscription subscribe(Observer<String> observer) {

        for (String s : valuesToReturn) {
          if (s == null) {
            System.out.println("throwing exception");
            observer.onError(new NullPointerException());
          } else {
            observer.onNext(s);
          }
        }
        observer.onCompleted();

        return new Subscription() {

          @Override
          public void unsubscribe() {
            // unregister ... will never be called here since we are executing synchronously
          }
        };
      }
Ejemplo n.º 15
0
  /**
   * @param provider
   * @param stageObserver
   * @return
   * @throws Throwable
   */
  public static <T> T inject(final Provider<T> provider, final Observer<StageChange> stageObserver)
      throws Throwable {
    @SuppressWarnings("unchecked")
    final Class<T> type =
        (Class<T>) ClassUtil.getTypeArguments(Provider.class, provider.getClass()).get(0);

    boolean success = true;

    if (stageObserver != null)
      stageObserver.onNext(new StageChangeImpl(type, null, Stage.PREPARING, null));

    success &= invokeEventHandlers(stageObserver, type, null, null, StageEvent.BEFORE_PROVIDE);

    success &=
        invokeStages(
            stageObserver, type, null, findStages(type, InjectStaged.BEFORE_PROVIDE_SELECTOR));

    if (stageObserver != null)
      stageObserver.onNext(new StageChangeImpl(type, null, Stage.PROVIDING, null));

    final FinalizeDecorator<T> subject =
        FinalizeDecorator.from(stageObserver, type, provider.get());

    if (stageObserver != null)
      stageObserver.onNext(new StageChangeImpl(type, subject.getTarget(), Stage.PROVIDED, null));
    success &=
        invokeEventHandlers(
            stageObserver, type, subject.getTarget(), null, StageEvent.AFTER_PROVIDE);

    success &=
        invokeStages(
            stageObserver,
            type,
            subject.getTarget(),
            findStages(type, InjectStaged.AFTER_PROVIDE_SELECTOR));

    if (stageObserver != null && success)
      stageObserver.onNext(new StageChangeImpl(type, subject.getTarget(), Stage.STOPPED, null));
    return subject.getTarget();
  }
Ejemplo n.º 16
0
 /**
  * Replay up to the given index
  *
  * @param limit
  */
 void replayTill(int limit) {
   int si = values.start();
   if (index < si) {
     index = si;
   }
   while (index < limit) {
     TIntermediate value = values.get(index);
     index++;
     try {
       wrapped.onNext(resultSelector.call(value));
     } catch (Throwable t) {
       replayers.remove(cancel);
       wrapped.onError(t);
       return;
     }
   }
   if (done) {
     if (error != null) {
       wrapped.onError(error);
     } else {
       wrapped.onCompleted();
     }
   }
 }
Ejemplo n.º 17
0
      @Override
      public void onCompleted() {
        parentCompleted = true;
        // this *can* occur before the children are done, so if it does we won't send onCompleted
        // but will let the child worry about it
        // if however this completes and there are no children processing, then we will send
        // onCompleted

        if (childObservers.size() == 0) {
          if (!stopped.get()) {
            if (ourSubscription.stop()) {
              actualObserver.onCompleted();
            }
          }
        }
      }
Ejemplo n.º 18
0
 @Override
 public void onCompleted() {
   // remove self from map of Observers
   childObservers.remove(this);
   // if there are now 0 Observers left, so if the parent is also completed we send the
   // onComplete to the actualObserver
   // if the parent is not complete that means there is another sequence (and child Observer)
   // to come
   if (!stopped.get()) {
     if (childObservers.size() == 0 && parentCompleted) {
       if (ourSubscription.stop()) {
         // this thread 'won' the race to unsubscribe/stop so let's send onCompleted
         actualObserver.onCompleted();
       }
     }
   }
 }
Ejemplo n.º 19
0
  private WeakObserver(
      final Observable<T> observable, Observer<T> observer, final boolean unsubscribeOnError) {
    weakRefObserver = new WeakReference<>(observer);
    observerClass = observer.getClass();

    subscription =
        observable.subscribe(
            new Subscriber<T>() {
              @Override
              public void onCompleted() {
                subscription.unsubscribe();
                WeakObserver.remove(observerClass, WeakObserver.this);
                Observer obs = dereference();
                if (obs != null) {
                  obs.onCompleted();
                }
              }

              @Override
              public void onError(Exception e) {
                if (unsubscribeOnError) {
                  subscription.unsubscribe();
                  WeakObserver.remove(observerClass, WeakObserver.this);
                }
                Observer obs = dereference();
                if (obs != null) {
                  obs.onError(e);
                }
              }

              @Override
              public void onNext(T t) {
                Observer obs = dereference();
                if (obs != null) {
                  obs.onNext(t);
                }
              }
            });
  }
Ejemplo n.º 20
0
    @Override
    public Subscription call(Observer<T> observer) {
      if (num < 1) {
        items
            .subscribe(
                new Observer<T>() {
                  @Override
                  public void onCompleted() {}

                  @Override
                  public void onError(Throwable e) {}

                  @Override
                  public void onNext(T args) {}
                })
            .unsubscribe();
        observer.onCompleted();
        return Subscriptions.empty();
      }

      return subscription.wrap(items.subscribe(new ItemObserver(observer)));
    }
Ejemplo n.º 21
0
 @Override
 public void onError(Throwable e) {
   drainIfNeededAndSwitchToActual();
   actual.onError(e);
 }
 /* used to simulate subscription */
 public void sendOnError(Throwable e) {
   observer.onError(e);
 }
 /* used to simulate subscription */
 public void sendOnNext(String value) {
   observer.onNext(value);
 }
 /* used to simulate subscription */
 public void sendOnCompleted() {
   observer.onCompleted();
 }
Ejemplo n.º 25
0
 @Override
 public void onCompleted() {
   observer.onCompleted();
 }
Ejemplo n.º 26
0
 @Override
 public void onError(Throwable e) {
   observer.onError(e);
 }
Ejemplo n.º 27
0
 protected static <T> void emitValueToObserver(Notification<T> n, Observer<? super T> o) {
   n.accept(o);
   if (n.isOnNext()) {
     o.onCompleted();
   }
 }
Ejemplo n.º 28
0
 @Override
 public void onNext(T t) {
   drainIfNeededAndSwitchToActual();
   actual.onNext(t);
 }
Ejemplo n.º 29
0
 @Override
 public void onCompleted() {
   drainIfNeededAndSwitchToActual();
   actual.onCompleted();
 }
Ejemplo n.º 30
0
 @Override
 public void onError(Exception e) {
   observer.onError(e);
 }