use of io.reactivex.functions.Action in project RxJava by ReactiveX.
the class SingleDoAfterTerminateTest method actionThrows.
@Test
public void actionThrows() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
Single.just(1).doAfterTerminate(new Action() {
@Override
public void run() throws Exception {
throw new TestException();
}
}).test().assertResult(1);
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.functions.Action in project RxDownload by ssseasonnn.
the class UtilsTest method lifecycleTest.
@Test
public void lifecycleTest() throws Exception {
final Semaphore semaphore = new Semaphore(0);
Disposable disposable = Observable.just(1).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
System.out.println("on subscribe...");
System.out.println("acquiring...");
semaphore.acquire();
System.out.println("acquired!");
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
System.out.println("finally");
semaphore.release();
System.out.println("released");
System.out.println(semaphore.availablePermits());
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(integer);
}
});
disposable.dispose();
}
use of io.reactivex.functions.Action in project RxDownload by ssseasonnn.
the class DownloadType method startDownload.
public Observable<DownloadStatus> startDownload() {
return Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription subscription) throws Exception {
log(startLog());
record.start();
}
}).flatMap(new Function<Integer, Publisher<DownloadStatus>>() {
@Override
public Publisher<DownloadStatus> apply(Integer integer) throws Exception {
return download();
}
}).doOnNext(new Consumer<DownloadStatus>() {
@Override
public void accept(DownloadStatus status) throws Exception {
record.update(status);
}
}).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
log(errorLog());
record.error();
}
}).doOnComplete(new Action() {
@Override
public void run() throws Exception {
log(completeLog());
record.complete();
}
}).doOnCancel(new Action() {
@Override
public void run() throws Exception {
log(cancelLog());
record.cancel();
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
log(finishLog());
record.finish();
}
}).toObservable();
}
use of io.reactivex.functions.Action in project RxDownload by ssseasonnn.
the class SingleMission method start.
@Override
public void start(final Semaphore semaphore) throws InterruptedException {
if (isCanceled()) {
return;
}
semaphore.acquire();
if (isCanceled()) {
semaphore.release();
return;
}
disposable = rxdownload.download(bean).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
if (observer != null) {
observer.onSubscribe(disposable);
}
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
log("finally and release...");
semaphore.release();
}
}).subscribe(new Consumer<DownloadStatus>() {
@Override
public void accept(DownloadStatus value) throws Exception {
status = value;
processor.onNext(started(value));
if (observer != null) {
observer.onNext(value);
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
processor.onNext(failed(status, throwable));
if (observer != null) {
observer.onError(throwable);
}
}
}, new Action() {
@Override
public void run() throws Exception {
processor.onNext(completed(status));
setCompleted(true);
if (observer != null) {
observer.onComplete();
}
}
});
}
use of io.reactivex.functions.Action in project Conductor by bluelinelabs.
the class RxLifecycle2Controller method onCreateView.
@NonNull
@Override
protected View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
Log.i(TAG, "onCreateView() called");
View view = inflater.inflate(R.layout.controller_rxlifecycle, container, false);
view.setBackgroundColor(ContextCompat.getColor(container.getContext(), R.color.brown_300));
unbinder = ButterKnife.bind(this, view);
tvTitle.setText(getResources().getString(R.string.rxlifecycle_title, TAG));
Observable.interval(1, TimeUnit.SECONDS).doOnDispose(new Action() {
@Override
public void run() {
Log.i(TAG, "Disposing from onCreateView)");
}
}).compose(this.<Long>bindUntilEvent(ControllerEvent.DESTROY_VIEW)).subscribe(new Consumer<Long>() {
@Override
public void accept(Long num) {
Log.i(TAG, "Started in onCreateView(), running until onDestroyView(): " + num);
}
});
return view;
}
Aggregations