Search in sources :

Example 11 with Action

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();
    }
}
Also used : Action(io.reactivex.functions.Action) TestException(io.reactivex.exceptions.TestException) TestException(io.reactivex.exceptions.TestException) Test(org.junit.Test)

Example 12 with Action

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();
}
Also used : Disposable(io.reactivex.disposables.Disposable) Action(io.reactivex.functions.Action) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 13 with Action

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();
}
Also used : Action(io.reactivex.functions.Action) Consumer(io.reactivex.functions.Consumer) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 14 with Action

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();
            }
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Action(io.reactivex.functions.Action) Consumer(io.reactivex.functions.Consumer)

Example 15 with Action

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;
}
Also used : Action(io.reactivex.functions.Action) BindView(butterknife.BindView) TextView(android.widget.TextView) View(android.view.View) NonNull(android.support.annotation.NonNull)

Aggregations

Action (io.reactivex.functions.Action)28 Test (org.junit.Test)19 TestException (io.reactivex.exceptions.TestException)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Consumer (io.reactivex.functions.Consumer)3 TestScheduler (io.reactivex.schedulers.TestScheduler)3 View (android.view.View)2 Disposable (io.reactivex.disposables.Disposable)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 NonNull (android.support.annotation.NonNull)1 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 Toolbar (android.support.v7.widget.Toolbar)1 TextView (android.widget.TextView)1 BindView (butterknife.BindView)1 ActionBarProvider (com.bluelinelabs.conductor.demo.ActionBarProvider)1 ActionBarOwner (com.example.mortar.android.ActionBarOwner)1 ChatListScreen (com.example.mortar.screen.ChatListScreen)1 BaseKey (com.example.mortar.util.BaseKey)1 LoginResult (com.facebook.login.LoginResult)1 ModelComponent (io.github.voidzombie.nhglib.runtime.ecs.components.graphics.ModelComponent)1