All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.transformers.JavaFxTransformers Maven / Gradle / Ivy

package rx.transformers;

import javafx.application.Platform;
import rx.Observable;
import rx.Producer;
import rx.Subscriber;
import rx.exceptions.Exceptions;
import rx.functions.Action0;
import rx.functions.Action1;

public final class JavaFxTransformers {
    private JavaFxTransformers() {}

    /**
     * Performs a given action for each item on the FX thread
     * @param onNext
     * @param 
     */
    public static  Observable.Transformer doOnNextFx(Action1 onNext) {
        return obs -> obs.doOnNext(t -> Platform.runLater(() -> onNext.call(t)));
    }

    /**
     * Performs a given action on a Throwable on the FX thread in the event of an onError
     * @param onError
     * @param 
     */
    public static  Observable.Transformer doOnErrorFx(Action1 onError) {
        return obs -> obs.doOnError(e -> Platform.runLater(() -> onError.call(e)));
    }

    /**
     * Performs a given Action0 on the FX thread when onCompleted is called
     * @param onCompleted
     * @param 
     */
    public static  Observable.Transformer doOnCompletedFx(Action0 onCompleted) {
        return obs -> obs.doOnCompleted(() -> Platform.runLater(onCompleted::call));
    }

    /**
     * Performs a given Action0 on the FX thread when subscribed to
     * @param subscribe
     * @param 
     */
    public static  Observable.Transformer doOnSubscribeFx(Action0 subscribe) {
        return obs -> obs.doOnSubscribe((() -> Platform.runLater(subscribe::call)));
    }

    /**
     * Performs the provided onTerminate action on the FX thread
     * @param onTerminate
     * @param 
     */
    public static  Observable.Transformer doOnTerminateFx(Action0 onTerminate) {
        return obs -> obs.doOnTerminate(() -> Platform.runLater(onTerminate::call));
    }

    /**
     * Performs the provided onTerminate action on the FX thread
     * @param onUnsubscribe
     * @param 
     */
    public static  Observable.Transformer doOnUnsubscribeFx(Action0 onUnsubscribe) {
        return obs -> obs.doOnUnsubscribe(() -> Platform.runLater(onUnsubscribe::call));
    }

    /**
     * Performs an action on onNext with the provided emission count
     * @param onNext
     * @param 
     */
    public static  Observable.Transformer doOnNextCount(Action1 onNext) {
        return obs -> obs.lift(new OperatorEmissionCounter<>(new CountObserver(onNext,null,null)));
    }

    /**
     * Performs an action on onCompleted with the provided emission count
     * @param onCompleted
     * @param 
     */
    public static  Observable.Transformer doOnCompletedCount(Action1 onCompleted) {
        return obs -> obs.lift(new OperatorEmissionCounter<>(new CountObserver(null,onCompleted,null)));
    }

    /**
     * Performs an action on onError with the provided emission count
     * @param onError
     * @param 
     */
    public static  Observable.Transformer doOnErrorCount(Action1 onError) {
        return obs -> obs.lift(new OperatorEmissionCounter<>(new CountObserver(null,null,onError)));
    }

    /**
     * Performs an action on FX thread on onNext with the provided emission count
     * @param onNext
     * @param 
     */
    public static  Observable.Transformer doOnNextCountFx(Action1 onNext) {
        return obs -> obs.compose(doOnNextCount(i -> Platform.runLater(() -> onNext.call(i))));
    }

    /**
     * Performs an action on FX thread on onCompleted with the provided emission count
     * @param onCompleted
     * @param 
     */
    public static  Observable.Transformer doOnCompletedCountFx(Action1 onCompleted) {
        return obs -> obs.compose(doOnCompletedCount(i -> Platform.runLater(() -> onCompleted.call(i))));
    }

    /**
     * Performs an action on FX thread on onError with the provided emission count
     * @param onError
     * @param 
     */
    public static  Observable.Transformer doOnErrorCountFx(Action1 onError) {
        return obs -> obs.compose(doOnErrorCount(i -> Platform.runLater(() -> onError.call(i))));
    }


    private static class OperatorEmissionCounter implements Observable.Operator {

        private final CountObserver ctObserver;

        OperatorEmissionCounter(CountObserver ctObserver) {
            this.ctObserver = ctObserver;
        }

        @Override
        public Subscriber call(Subscriber child) {

            return new Subscriber() {
                private int count = 0;
                private boolean done = false;

                @Override
                public void onCompleted() {
                    if (done)
                        return;

                    try {
                        if (ctObserver.doOnCompletedCountAction != null)
                            ctObserver.doOnCompletedCountAction.call(count);
                    } catch (Exception e) {
                        Exceptions.throwIfFatal(e);
                        onError(e);
                        return;
                    }
                    done = true;
                    child.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    if (done)
                        return;
                    try {
                        if (ctObserver.doOnErrorCountAction != null)
                            ctObserver.doOnErrorCountAction.call(count);
                    } catch(Exception e1) {
                        Exceptions.throwIfFatal(e1);
                        child.onError(e1);
                    }
                }

                @Override
                public void onNext(T t) {
                    if (done)
                        return;
                    try {
                        if (ctObserver.doOnNextCountAction != null)
                            ctObserver.doOnNextCountAction.call(++count);
                    } catch(Exception e) {
                        Exceptions.throwIfFatal(e);
                        onError(e);
                        return;
                    }
                    child.onNext(t);
                }

                @Override
                public void setProducer(Producer p) {
                    child.setProducer(p);
                }
            };
        }
    }
    private static final class CountObserver {
        private final Action1 doOnNextCountAction;
        private final Action1 doOnCompletedCountAction;
        private final Action1 doOnErrorCountAction;

        CountObserver(Action1 doOnNextCountAction, Action1 doOnCompletedCountAction, Action1 doOnErrorCountAction) {
            this.doOnNextCountAction = doOnNextCountAction;
            this.doOnCompletedCountAction = doOnCompletedCountAction;
            this.doOnErrorCountAction = doOnErrorCountAction;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy