Package rx.plugins

Class RxJavaHooks


  • @Experimental
    public final class RxJavaHooks
    extends java.lang.Object
    Utility class that holds hooks for various Observable, Single and Completable lifecycle-related points as well as Scheduler hooks.

    The class features a lockdown state, see lockdown() and isLockdown(), to prevent further changes to the hooks.

    • Constructor Detail

      • RxJavaHooks

        private RxJavaHooks()
        Utility class.
    • Method Detail

      • init

        static void init()
        Initialize the hooks via delegating to RxJavaPlugins.
      • initCreate

        static void initCreate()
      • reset

        public static void reset()
        Reset all hook callbacks to those of the current RxJavaPlugins handlers.
        See Also:
        clear()
      • clear

        public static void clear()
        Clears all hooks to be no-operations (and passthroughs) and onError hook to signal errors to the caller thread's UncaughtExceptionHandler.
        See Also:
        reset()
      • lockdown

        public static void lockdown()
        Prevents changing the hooks.
      • isLockdown

        public static boolean isLockdown()
        Returns true if the hooks can no longer be changed.
        Returns:
        true if the hooks can no longer be changed
      • onError

        public static void onError​(java.lang.Throwable ex)
        Consume undeliverable Throwables (acts as a global catch).
        Parameters:
        ex - the exception to handle
      • signalUncaught

        static void signalUncaught​(java.lang.Throwable ex)
      • onCreate

        public static <T> Observable.OnSubscribe<T> onCreate​(Observable.OnSubscribe<T> onSubscribe)
        Hook to call when an Observable is created.
        Type Parameters:
        T - the value type
        Parameters:
        onSubscribe - the original OnSubscribe logic
        Returns:
        the original or replacement OnSubscribe instance
      • onCreate

        public static <T> Single.OnSubscribe<T> onCreate​(Single.OnSubscribe<T> onSubscribe)
        Hook to call when a Single is created.
        Type Parameters:
        T - the value type
        Parameters:
        onSubscribe - the original OnSubscribe logic
        Returns:
        the original or replacement OnSubscribe instance
      • onComputationScheduler

        public static Scheduler onComputationScheduler​(Scheduler scheduler)
        Hook to call when the Schedulers.computation() is called.
        Parameters:
        scheduler - the default computation scheduler
        Returns:
        the default of alternative scheduler
      • onIOScheduler

        public static Scheduler onIOScheduler​(Scheduler scheduler)
        Hook to call when the Schedulers.io() is called.
        Parameters:
        scheduler - the default io scheduler
        Returns:
        the default of alternative scheduler
      • onNewThreadScheduler

        public static Scheduler onNewThreadScheduler​(Scheduler scheduler)
        Hook to call when the Schedulers.newThread() is called.
        Parameters:
        scheduler - the default new thread scheduler
        Returns:
        the default of alternative scheduler
      • onScheduledAction

        public static Action0 onScheduledAction​(Action0 action)
        Hook to call before the action is scheduled, allows decorating the original action.
        Parameters:
        action - the original action
        Returns:
        the original or alternative action
      • onObservableStart

        public static <T> Observable.OnSubscribe<T> onObservableStart​(Observable<T> instance,
                                                                      Observable.OnSubscribe<T> onSubscribe)
        Hook to call before the child subscriber is subscribed to the OnSubscribe action.
        Type Parameters:
        T - the value type
        Parameters:
        instance - the parent Observable instance
        onSubscribe - the original OnSubscribe action
        Returns:
        the original or alternative action that will be subscribed to
      • onObservableReturn

        public static Subscription onObservableReturn​(Subscription subscription)
        Hook to call before the Observable.subscribe() method is about to return a Subscription.
        Parameters:
        subscription - the original subscription
        Returns:
        the original or alternative subscription that will be returned
      • onObservableError

        public static java.lang.Throwable onObservableError​(java.lang.Throwable error)
        Hook to call if the Observable.subscribe() crashes for some reason.
        Parameters:
        error - the error
        Returns:
        the original error or alternative Throwable to be thrown
      • onObservableLift

        public static <T,​R> Observable.Operator<R,​T> onObservableLift​(Observable.Operator<R,​T> operator)
        Hook to call before the child subscriber would subscribe to an Operator.
        Type Parameters:
        T - the input value type
        R - the output value type
        Parameters:
        operator - the original operator
        Returns:
        the original or alternative operator that will be subscribed to
      • onSingleStart

        public static <T> Observable.OnSubscribe<T> onSingleStart​(Single<T> instance,
                                                                  Observable.OnSubscribe<T> onSubscribe)
        Hook to call before the child subscriber is subscribed to the OnSubscribe action.
        Type Parameters:
        T - the value type
        Parameters:
        instance - the parent Single instance
        onSubscribe - the original OnSubscribe action
        Returns:
        the original or alternative action that will be subscribed to
      • onSingleReturn

        public static Subscription onSingleReturn​(Subscription subscription)
        Hook to call before the Single.subscribe() method is about to return a Subscription.
        Parameters:
        subscription - the original subscription
        Returns:
        the original or alternative subscription that will be returned
      • onSingleError

        public static java.lang.Throwable onSingleError​(java.lang.Throwable error)
        Hook to call if the Single.subscribe() crashes for some reason.
        Parameters:
        error - the error
        Returns:
        the original error or alternative Throwable to be thrown
      • onSingleLift

        public static <T,​R> Observable.Operator<R,​T> onSingleLift​(Observable.Operator<R,​T> operator)
        Hook to call before the child subscriber would subscribe to an Operator.
        Type Parameters:
        T - the input value type
        R - the output value type
        Parameters:
        operator - the original operator
        Returns:
        the original or alternative operator that will be subscribed to
      • onCompletableStart

        public static <T> Completable.CompletableOnSubscribe onCompletableStart​(Completable instance,
                                                                                Completable.CompletableOnSubscribe onSubscribe)
        Hook to call before the child subscriber is subscribed to the OnSubscribe action.
        Type Parameters:
        T - the value type
        Parameters:
        instance - the parent Completable instance
        onSubscribe - the original OnSubscribe action
        Returns:
        the original or alternative action that will be subscribed to
      • onCompletableError

        public static java.lang.Throwable onCompletableError​(java.lang.Throwable error)
        Hook to call if the Completable.subscribe() crashes for some reason.
        Parameters:
        error - the error
        Returns:
        the original error or alternative Throwable to be thrown
      • onCompletableLift

        public static <T,​R> Completable.CompletableOperator onCompletableLift​(Completable.CompletableOperator operator)
        Hook to call before the child subscriber would subscribe to an Operator.
        Type Parameters:
        T - the input value type
        R - the output value type
        Parameters:
        operator - the original operator
        Returns:
        the original or alternative operator that will be subscribed to
      • setOnError

        public static void setOnError​(Action1<java.lang.Throwable> onError)
        Sets the global error consumer action unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: errors are routed to the current thread's Thread.UncaughtExceptionHandler.

        Parameters:
        onError - the action that will receive undeliverable Throwables
      • setOnCompletableCreate

        public static void setOnCompletableCreate​(Func1<Completable.CompletableOnSubscribe,​Completable.CompletableOnSubscribe> onCompletableCreate)
        Sets the Completable's onCreate hook function unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onCompletableCreate - the function that takes the original CompletableOnSubscribe and should return a CompletableOnSubscribe.
      • setOnObservableCreate

        public static void setOnObservableCreate​(Func1<Observable.OnSubscribe,​Observable.OnSubscribe> onObservableCreate)
        Sets the Observable onCreate hook function unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onObservableCreate - the function that takes the original OnSubscribe and should return a OnSubscribe.
      • setOnSingleCreate

        public static void setOnSingleCreate​(Func1<Single.OnSubscribe,​Single.OnSubscribe> onSingleCreate)
        Sets the Single onCreate hook function unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onSingleCreate - the function that takes the original OnSubscribe and should return a OnSubscribe.
      • setOnComputationScheduler

        public static void setOnComputationScheduler​(Func1<Scheduler,​Scheduler> onComputationScheduler)
        Sets the hook function for returning a scheduler when the Schedulers.computation() is called unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onComputationScheduler - the function that receives the original computation scheduler and should return a scheduler.
      • setOnIOScheduler

        public static void setOnIOScheduler​(Func1<Scheduler,​Scheduler> onIOScheduler)
        Sets the hook function for returning a scheduler when the Schedulers.io() is called unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onIOScheduler - the function that receives the original io scheduler and should return a scheduler.
      • setOnNewThreadScheduler

        public static void setOnNewThreadScheduler​(Func1<Scheduler,​Scheduler> onNewThreadScheduler)
        Sets the hook function for returning a scheduler when the Schedulers.newThread() is called unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onNewThreadScheduler - the function that receives the original new thread scheduler and should return a scheduler.
      • setOnScheduleAction

        public static void setOnScheduleAction​(Func1<Action0,​Action0> onScheduleAction)
        Sets the hook function that is called before an action is scheduled, allowing decorating that function, unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onScheduleAction - the function that receives the original action and should return an Action0.
      • setOnCompletableStart

        public static void setOnCompletableStart​(Func2<Completable,​Completable.CompletableOnSubscribe,​Completable.CompletableOnSubscribe> onCompletableStart)
        Sets the hook function that is called when a subscriber subscribes to a Completable unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same CompletableOnSubscribe object.

        Parameters:
        onCompletableStart - the function that is called with the current Completable instance, its CompletableOnSubscribe function and should return a CompletableOnSubscribe function that gets actually subscribed to.
      • setOnObservableStart

        public static void setOnObservableStart​(Func2<Observable,​Observable.OnSubscribe,​Observable.OnSubscribe> onObservableStart)
        Sets the hook function that is called when a subscriber subscribes to a Observable unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same OnSubscribe object.

        Parameters:
        onObservableStart - the function that is called with the current Observable instance, its OnSubscribe function and should return a OnSubscribe function that gets actually subscribed to.
      • setOnSingleStart

        public static void setOnSingleStart​(Func2<Single,​Observable.OnSubscribe,​Observable.OnSubscribe> onSingleStart)
        Sets the hook function that is called when a subscriber subscribes to a Single unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same OnSubscribe object.

        Parameters:
        onSingleStart - the function that is called with the current Single instance, its OnSubscribe function and should return a OnSubscribe function that gets actually subscribed to.
      • setOnObservableReturn

        public static void setOnObservableReturn​(Func1<Subscription,​Subscription> onObservableReturn)
        Sets a hook function that is called when the Observable.subscribe() call is about to return a Subscription unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onObservableReturn - the function that is called with the Subscriber that has been subscribed to the OnSubscribe function and returns a Subscription that will be returned by subscribe().
      • setOnSingleReturn

        public static void setOnSingleReturn​(Func1<Subscription,​Subscription> onSingleReturn)
        Sets a hook function that is called when the Single.subscribe() call is about to return a Subscription unless a lockdown is in effect.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onSingleReturn - the function that is called with the SingleSubscriber that has been subscribed to the OnSubscribe function and returns a Subscription that will be returned by subscribe().
      • setOnSingleSubscribeError

        public static void setOnSingleSubscribeError​(Func1<java.lang.Throwable,​java.lang.Throwable> onSingleSubscribeError)
        Sets a hook function that is called when the Single.subscribe() call fails with an exception.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onSingleSubscribeError - the function that is called with the crash exception and should return an exception.
      • getOnSingleSubscribeError

        public static Func1<java.lang.Throwable,​java.lang.Throwable> getOnSingleSubscribeError()
        Returns the current Single onSubscribeError hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • setOnCompletableSubscribeError

        public static void setOnCompletableSubscribeError​(Func1<java.lang.Throwable,​java.lang.Throwable> onCompletableSubscribeError)
        Sets a hook function that is called when the Completable.subscribe() call fails with an exception.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onCompletableSubscribeError - the function that is called with the crash exception and should return an exception.
      • getOnCompletableSubscribeError

        public static Func1<java.lang.Throwable,​java.lang.Throwable> getOnCompletableSubscribeError()
        Returns the current Completable onSubscribeError hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • setOnObservableSubscribeError

        public static void setOnObservableSubscribeError​(Func1<java.lang.Throwable,​java.lang.Throwable> onObservableSubscribeError)
        Sets a hook function that is called when the Observable.subscribe() call fails with an exception.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onObservableSubscribeError - the function that is called with the crash exception and should return an exception.
      • getOnObservableSubscribeError

        public static Func1<java.lang.Throwable,​java.lang.Throwable> getOnObservableSubscribeError()
        Returns the current Observable onSubscribeError hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • setOnObservableLift

        public static void setOnObservableLift​(Func1<Observable.Operator,​Observable.Operator> onObservableLift)
        Sets a hook function that is called with an operator when an Observable operator built with lift() gets subscribed to.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onObservableLift - the function that is called with original Operator and should return an Operator instance.
      • getOnObservableLift

        public static Func1<Observable.Operator,​Observable.Operator> getOnObservableLift()
        Returns the current Observable onLift hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • setOnSingleLift

        public static void setOnSingleLift​(Func1<Observable.Operator,​Observable.Operator> onSingleLift)
        Sets a hook function that is called with an operator when an Single operator built with lift() gets subscribed to.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onSingleLift - the function that is called with original Operator and should return an Operator instance.
      • getOnSingleLift

        public static Func1<Observable.Operator,​Observable.Operator> getOnSingleLift()
        Returns the current Single onLift hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • setOnCompletableLift

        public static void setOnCompletableLift​(Func1<Completable.CompletableOperator,​Completable.CompletableOperator> onCompletableLift)
        Sets a hook function that is called with an operator when an Completable operator built with lift() gets subscribed to.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: the hook returns the same object.

        Parameters:
        onCompletableLift - the function that is called with original Operator and should return an Operator instance.
      • getOnComputationScheduler

        public static Func1<Scheduler,​Scheduler> getOnComputationScheduler()
        Returns the current computation scheduler hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnError

        public static Action1<java.lang.Throwable> getOnError()
        Returns the current global error handler hook action or null if it is set to the default one that signals errors to the current threads UncaughtExceptionHandler.

        This operation is threadsafe.

        Returns:
        the current hook action
      • getOnIOScheduler

        public static Func1<Scheduler,​Scheduler> getOnIOScheduler()
        Returns the current io scheduler hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnNewThreadScheduler

        public static Func1<Scheduler,​Scheduler> getOnNewThreadScheduler()
        Returns the current new thread scheduler hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnObservableCreate

        public static Func1<Observable.OnSubscribe,​Observable.OnSubscribe> getOnObservableCreate()
        Returns the current Observable onCreate hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnScheduleAction

        public static Func1<Action0,​Action0> getOnScheduleAction()
        Returns the current schedule action hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnSingleCreate

        public static Func1<Single.OnSubscribe,​Single.OnSubscribe> getOnSingleCreate()
        Returns the current Single onCreate hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnObservableStart

        public static Func2<Observable,​Observable.OnSubscribe,​Observable.OnSubscribe> getOnObservableStart()
        Returns the current Observable onStart hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnSingleStart

        public static Func2<Single,​Observable.OnSubscribe,​Observable.OnSubscribe> getOnSingleStart()
        Returns the current Single onStart hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnObservableReturn

        public static Func1<Subscription,​Subscription> getOnObservableReturn()
        Returns the current Observable onReturn hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • getOnSingleReturn

        public static Func1<Subscription,​Subscription> getOnSingleReturn()
        Returns the current Single onReturn hook function or null if it is set to the default pass-through.

        This operation is threadsafe.

        Returns:
        the current hook function
      • resetAssemblyTracking

        public static void resetAssemblyTracking()
        Resets the assembly tracking hooks to their default delegates to RxJavaPlugins.
      • clearAssemblyTracking

        public static void clearAssemblyTracking()
        Clears the assembly tracking hooks to their default pass-through behavior.
      • enableAssemblyTracking

        public static void enableAssemblyTracking()
        Sets up hooks that capture the current stacktrace when a source or an operator is instantiated, keeping it in a field for debugging purposes and alters exceptions passign along to hold onto this stacktrace.
      • setOnGenericScheduledExecutorService

        public static void setOnGenericScheduledExecutorService​(Func0<? extends java.util.concurrent.ScheduledExecutorService> factory)
        Sets the hook function for returning a ScheduledExecutorService used by the GenericScheduledExecutorService for background tasks.

        This operation is threadsafe.

        Calling with a null parameter restores the default behavior: create the default with Executors.newScheduledThreadPool(int, java.util.concurrent.ThreadFactory).

        For the changes to take effect, the Schedulers has to be restarted.

        Parameters:
        factory - the supplier that is called when the GenericScheduledExecutorService is (re)started
      • getOnGenericScheduledExecutorService

        public static Func0<? extends java.util.concurrent.ScheduledExecutorService> getOnGenericScheduledExecutorService()
        Returns the current factory for creating ScheduledExecutorServices in GenericScheduledExecutorService utility.

        This operation is threadsafe.

        Returns:
        the current factory function