/** * Returns the currently contained Disposable or null if this container is empty. * @return the current Disposable, may be null */ @Nullable public Disposable get() { Disposable d = resource.get(); if (d == DisposableHelper.DISPOSED) { return Disposables.disposed(); } return d; }
@Override public void subscribe(Observer<? super String> innerObserver) { innerObserver.onSubscribe(Disposables.empty()); push(innerObserver, "one", 10); push(innerObserver, "two", 60); push(innerObserver, "three", 110); push(innerObserver, "four", 160); push(innerObserver, "five", 210); complete(innerObserver, 250); } });
/** * Construct a Disposable by wrapping a Future that is * cancelled exactly once when the Disposable is disposed. * @param future the Future to wrap * @return the new Disposable instance */ @NonNull public static Disposable fromFuture(@NonNull Future<?> future) { ObjectHelper.requireNonNull(future, "future is null"); return fromFuture(future, true); }
@Override public Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit) { ScheduledExecutorService exec = pick(); if (exec == REJECTING) { return Disposables.disposed(); } try { return Disposables.fromFuture(exec.schedule(RxJavaPlugins.onSchedule(run), delay, unit)); } catch (RejectedExecutionException ex) { return Disposables.disposed(); } }
@Override public Disposable call() { return Disposables.fromRunnable(unsubscribe); } };
@Test public void disposeThrowsCheckedException() { CompositeDisposable cd = new CompositeDisposable(); cd.add(Disposables.fromAction(new Action() { @Override public void run() throws Exception { throw new IOException(); } })); Disposable d1 = Disposables.empty(); cd.add(d1); try { cd.dispose(); fail("Failed to throw"); } catch (RuntimeException ex) { // expected if (!(ex.getCause() instanceof IOException)) { fail(ex.toString() + " should have thrown RuntimeException(IOException)"); } } assertTrue(d1.isDisposed()); }
private Disposable disposeInUiThread(final Action action) { return Disposables.fromAction(new Action() { @Override public void run() throws Exception { if (Looper.getMainLooper() == Looper.myLooper()) { action.run(); } else { final Scheduler.Worker inner = AndroidSchedulers.mainThread().createWorker(); inner.schedule(new Runnable() { @Override public void run() { try { action.run(); } catch (Exception e) { onError("Could not unregister receiver in UI Thread", e); } inner.dispose(); } }); } } }); } }
@Override public Disposable schedulePeriodicallyDirect(Runnable run, long initialDelay, long period, TimeUnit unit) { ScheduledExecutorService exec = pick(); if (exec == REJECTING) { return Disposables.disposed(); } try { return Disposables.fromFuture(exec.scheduleAtFixedRate(RxJavaPlugins.onSchedule(run), initialDelay, period, unit)); } catch (RejectedExecutionException ex) { return Disposables.disposed(); } }
@Override public Disposable call() { return Disposables.fromRunnable(unsubscribe); } };
@Test public void disposeThrowsIAE() { CompositeDisposable cd = new CompositeDisposable(); cd.add(Disposables.fromAction(new Action() { @Override public void run() throws Exception { throw new IllegalArgumentException(); } })); Disposable d1 = Disposables.empty(); cd.add(d1); try { cd.dispose(); fail("Failed to throw"); } catch (IllegalArgumentException ex) { // expected } assertTrue(d1.isDisposed()); }
@Override protected void subscribeActual(final Observer<? super BleAdapterState> observer) { final BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); observer.onNext(mapToBleAdapterState(state)); } } }; observer.onSubscribe(Disposables.fromAction(new Action() { @Override public void run() throws Exception { try { context.unregisterReceiver(receiver); } catch (IllegalArgumentException exception) { Log.d(TAG, "The receiver is already not registered."); } } })); context.registerReceiver(receiver, createFilter()); }
@Override protected void subscribeActual(Observer<? super Object> observer) { observer.onSubscribe(Disposables.empty()); ref.set(observer); } }))
/** * Returns the currently contained Disposable or null if this container is empty. * @return the current Disposable, may be null */ @Nullable public Disposable get() { Disposable d = resource.get(); if (d == DisposableHelper.DISPOSED) { return Disposables.disposed(); } return d; }
@Override public Disposable scheduleDirect(Runnable run) { ScheduledExecutorService exec = pick(); if (exec == REJECTING) { return Disposables.disposed(); } try { return Disposables.fromFuture(exec.submit(RxJavaPlugins.onSchedule(run))); } catch (RejectedExecutionException ex) { return Disposables.disposed(); } }
/** * Returns a new, non-disposed Disposable instance. * @return a new, non-disposed Disposable instance */ @NonNull public static Disposable empty() { return fromRunnable(Functions.EMPTY_RUNNABLE); }
@Test public void disposeThrowsError() { CompositeDisposable cd = new CompositeDisposable(); cd.add(Disposables.fromAction(new Action() { @Override public void run() throws Exception { throw new AssertionError(); } })); Disposable d1 = Disposables.empty(); cd.add(d1); try { cd.dispose(); fail("Failed to throw"); } catch (AssertionError ex) { // expected } assertTrue(d1.isDisposed()); }
@Override public <T> Maybe<T> prepare(Request request, Supplier<? extends AsyncHandler<T>> handlerSupplier) { requireNonNull(request); requireNonNull(handlerSupplier); return Maybe.create(emitter -> { final AsyncHandler<?> bridge = createBridge(emitter, handlerSupplier.get()); final Future<?> responseFuture = asyncHttpClient.executeRequest(request, bridge); emitter.setDisposable(Disposables.fromFuture(responseFuture)); }); }
@Test public void fromAction() { class AtomicAction extends AtomicBoolean implements Action { private static final long serialVersionUID = -1517510584253657229L; @Override public void run() throws Exception { set(true); } } AtomicAction aa = new AtomicAction(); Disposables.fromAction(aa).dispose(); assertTrue(aa.get()); }
@Override protected void subscribeActual(Observer<? super Integer> observer) { observer.onSubscribe(Disposables.empty()); observerRef.set(observer); } }
@Override protected void subscribeActual(MaybeObserver<? super T> observer) { observer.onSubscribe(Disposables.disposed()); observer.onSuccess(value); }