@Override
 public void run() {
   System.out.println("Doing something expensive");
   reactiveXBoilerplate.sleep(3000);
   subscriber.onNext("Expensive result");
   subscriber.onCompleted();
 }
    @Override
    public void request(long n) {
      if (once) {
        return;
      }
      if (n < 0L) {
        throw new IllegalStateException("n >= required but it was " + n);
      }
      if (n != 0L) {
        once = true;
        Subscriber<? super T> a = actual;
        if (a.isUnsubscribed()) {
          return;
        }
        T v = value;
        try {
          a.onNext(v);
        } catch (Throwable e) {
          Exceptions.throwOrReport(e, a, v);
          return;
        }

        if (a.isUnsubscribed()) {
          return;
        }
        a.onCompleted();
      }
    }
예제 #3
0
 @Override
 public void call(Subscriber<? super Long> op) {
   long l = 1;
   while (!op.isUnsubscribed()) {
     op.onNext(l++);
   }
   op.onCompleted();
 }
 @Override
 public void onViewDetachedFromWindow(View v) {
   if (!isUnsubscribed()) {
     Subscriber<? super View> originalSubscriber = subscriber;
     unsubscribe();
     originalSubscriber.onNext(v);
     originalSubscriber.onCompleted();
   }
 }
예제 #5
0
  private void onWifiEnabled() {
    Log.d("WifiService", "The Wifi has been enabled.");
    unregisterReceiver(wifiStateChangedReceiver);
    wifiStateChangedReceiver = null;

    while (!wifiStateChangedSubscribers.isEmpty()) {
      Subscriber subscriber = wifiStateChangedSubscribers.poll();
      subscriber.onCompleted();
    }
  }
  @Override
  public void call(final Subscriber<? super T> subscriber) {
    final Realm realm = Realm.getInstance(context);
    thread = Thread.currentThread();
    subscriber.add(
        Subscriptions.create(
            new Action0() {
              @Override
              public void call() {
                if (thread != null && !thread.isInterrupted()) {
                  thread.interrupt();
                }
              }
            }));

    boolean interrupted = false;
    boolean withError = false;

    T object = null;
    try {
      realm.beginTransaction();
      object = get(realm);
      interrupted = thread.isInterrupted();
      if (object != null && !interrupted) {
        realm.commitTransaction();
      } else {
        realm.cancelTransaction();
      }
    } catch (RuntimeException e) {
      realm.cancelTransaction();
      subscriber.onError(new RealmException("Error during transaction.", e));
      withError = true;
    } catch (Error e) {
      realm.cancelTransaction();
      subscriber.onError(e);
      withError = true;
    }
    if (!interrupted && !withError) {
      subscriber.onNext(object);
    }

    try {
      realm.close();
    } catch (RealmException ex) {
      subscriber.onError(ex);
      withError = true;
    }
    thread = null;
    if (!withError) {
      subscriber.onCompleted();
    }
    if (interrupted) {
      Thread.currentThread().interrupt();
    }
  }
 @Override
 public void onCompleted() {
   synchronized (this) {
     if (emitting) {
       missedTerminal = true;
       return;
     }
     emitting = true;
   }
   child.onCompleted();
 }
 private void triggerServiceActive()
 {
   try
   {
     Iterator localIterator = this.networkSubscribers.iterator();
     while (localIterator.hasNext())
     {
       Subscriber localSubscriber = (Subscriber)localIterator.next();
       localSubscriber.onNext(this.mNetworkManager);
       localSubscriber.onCompleted();
       localIterator.remove();
     }
   }
   finally {}
 }
예제 #9
0
 @Override
 public void onCompleted() {
   InboundInterceptor<I, O> nextIn = context.nextIn(request);
   OutboundInterceptor<O> nextOut;
   if (null != nextIn) {
     Observable<Void> interceptorResult = nextIn.in(request, response);
     handleResult(interceptorResult);
   } else if (context.invokeRouter()) {
     handleResult(router.handle(request, response));
   } else if (null != (nextOut = context.nextOut())) {
     handleResult(nextOut.out(response));
   } else {
     child.onCompleted();
   }
 }
 @Override
 public void call() {
   Subscriber<? super T> a = actual;
   if (a.isUnsubscribed()) {
     return;
   }
   T v = value;
   try {
     a.onNext(v);
   } catch (Throwable e) {
     Exceptions.throwOrReport(e, a, v);
     return;
   }
   if (a.isUnsubscribed()) {
     return;
   }
   a.onCompleted();
 }
예제 #11
0
  @Override
  public void call(Subscriber<? super Integer> subscriber) {
    subscriber.onNext(1);
    subscriber.onNext(2);

    if (throwAnErrorCounter > 4) {
      throwAnErrorCounter--;
      subscriber.onError(new FooException());
      return;
    }
    if (throwAnErrorCounter > 0) {
      throwAnErrorCounter--;
      subscriber.onError(new BooException());
      return;
    }
    subscriber.onNext(3);
    subscriber.onNext(4);
    subscriber.onCompleted();
  }
예제 #12
0
    @Override
    public void onNext(T args) {
      child.onNext(args);

      boolean stop = false;
      try {
        stop = stopPredicate.call(args);
      } catch (Throwable e) {
        done = true;
        child.onError(e);
        unsubscribe();
        return;
      }
      if (stop) {
        done = true;
        child.onCompleted();
        unsubscribe();
      }
    }
예제 #13
0
  private static void produceValuesAndAnError(Subscriber s) {
    Subscriber subscriber = (Subscriber) s;

    try {
      for (int ii = 0; ii < 50; ii++) {
        if (!subscriber.isUnsubscribed()) {
          subscriber.onNext("Pushed value " + ii);
        }

        if (ii == 5) {
          throw new Throwable("Something has gone wrong here");
        }
      }

      if (!subscriber.isUnsubscribed()) {
        subscriber.onCompleted();
      }

    } catch (Throwable throwable) {
      subscriber.onError(throwable);
    }
  }
예제 #14
0
 @Override
 public void call(Subscriber<? super String> sub) {
   sub.onNext("Hello, world!");
   sub.onCompleted();
 }
예제 #15
0
    protected void drain() {
      synchronized (this) {
        if (emitting) {
          missedEmitting++;
          return;
        }
        emitting = true;
        missedEmitting = 0;
      }
      final List<SourceSubscriber<T>.MergeItemSubscriber> subs = subscribers;
      final Subscriber<T> child = s;
      Object[] active = new Object[subs.size()];
      do {
        long r;

        outer:
        while ((r = requested) > 0) {
          int idx = lastIndex;
          synchronized (subs) {
            if (subs.size() == active.length) {
              active = subs.toArray(active);
            } else {
              active = subs.toArray();
            }
          }

          int resumeIndex = 0;
          int j = 0;
          for (Object o : active) {
            @SuppressWarnings("unchecked")
            MergeItemSubscriber e = (MergeItemSubscriber) o;
            if (e.index == idx) {
              resumeIndex = j;
              break;
            }
            j++;
          }
          int sumConsumed = 0;
          for (int i = 0; i < active.length; i++) {
            j = (i + resumeIndex) % active.length;

            @SuppressWarnings("unchecked")
            final MergeItemSubscriber e = (MergeItemSubscriber) active[j];
            final RxRingBuffer b = e.buffer;
            lastIndex = e.index;

            if (!e.once && b.peek() == null) {
              subs.remove(e);

              synchronized (guard) {
                this.active--;
              }
              csub.remove(e);

              e.release();

              subscribeNext();

              WIP.decrementAndGet(this);

              continue outer;
            }

            int consumed = 0;
            Object v;
            while (r > 0 && (v = b.poll()) != null) {
              nl.accept(child, v);
              if (child.isUnsubscribed()) {
                return;
              }
              r--;
              consumed++;
            }
            if (consumed > 0) {
              sumConsumed += consumed;
              REQUESTED.addAndGet(this, -consumed);
              e.requestMore(consumed);
            }
            if (r == 0) {
              break outer;
            }
          }
          if (sumConsumed == 0) {
            break;
          }
        }

        if (active.length == 0) {
          if (wip == 0) {
            child.onCompleted();
            return;
          }
        }
        synchronized (this) {
          if (missedEmitting == 0) {
            emitting = false;
            break;
          }
          missedEmitting = 0;
        }
      } while (true);
    }
예제 #16
0
  void emitLoop() {
    final Subscriber<? super T> c = child;

    outer:
    for (; ; ) {
      long localRequested;
      Producer localProducer;
      Object localTerminal;
      List<T> q;
      synchronized (this) {
        localRequested = missedRequested;
        localProducer = missedProducer;
        localTerminal = missedTerminal;
        q = queue;
        if (localRequested == 0L && localProducer == null && q == null && localTerminal == null) {
          emitting = false;
          return;
        }
        missedRequested = 0L;
        missedProducer = null;
        queue = null;
        missedTerminal = null;
      }
      boolean empty = q == null || q.isEmpty();
      if (localTerminal != null) {
        if (localTerminal != Boolean.TRUE) {
          c.onError((Throwable) localTerminal);
          return;
        } else if (empty) {
          c.onCompleted();
          return;
        }
      }
      long e = 0;
      if (q != null) {
        for (T v : q) {
          if (c.isUnsubscribed()) {
            return;
          } else if (hasError) {
            continue outer; // if an error has been set, shortcut the loop and act on it
          }
          try {
            c.onNext(v);
          } catch (Throwable ex) {
            Exceptions.throwOrReport(ex, c, v);
            return;
          }
        }
        e += q.size();
      }
      long r = requested;
      // if requested is max, we don't do any accounting
      if (r != Long.MAX_VALUE) {
        // if there were missing requested, add it up
        if (localRequested != 0L) {
          long u = r + localRequested;
          if (u < 0) {
            u = Long.MAX_VALUE;
          }
          r = u;
        }
        // if there were emissions and we don't run on max since the last check, subtract
        if (e != 0L && r != Long.MAX_VALUE) {
          long u = r - e;
          if (u < 0) {
            throw new IllegalStateException("More produced than requested");
          }
          r = u;
        }
        requested = r;
      }
      if (localProducer != null) {
        if (localProducer == NULL_PRODUCER) {
          currentProducer = null;
        } else {
          currentProducer = localProducer;
          if (r != 0L) {
            localProducer.request(r);
          }
        }
      } else {
        Producer p = currentProducer;
        if (p != null && localRequested != 0L) {
          p.request(localRequested);
        }
      }
    }
  }
예제 #17
0
 @Override
 public void onCompleted() {
   if (!done) {
     child.onCompleted();
   }
 }
 @Override
 public void onCompleted() {
   actual.onCompleted();
 }