|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.plugins; |
| 17 | + |
| 18 | +import rx.Observable; |
| 19 | +import rx.Single; |
| 20 | +import rx.Subscriber; |
| 21 | +import rx.Subscription; |
| 22 | +import rx.functions.Func1; |
| 23 | + |
| 24 | +/** |
| 25 | + * Abstract ExecutionHook with invocations at different lifecycle points of {@link Single} execution with a |
| 26 | + * default no-op implementation. |
| 27 | + * <p> |
| 28 | + * See {@link RxJavaPlugins} or the RxJava GitHub Uncyclo for information on configuring plugins: |
| 29 | + * <a href="https://github.com/ReactiveX/RxJava/wiki/Plugins">https://github.com/ReactiveX/RxJava/wiki/Plugins</a>. |
| 30 | + * <p> |
| 31 | + * <b>Note on thread-safety and performance:</b> |
| 32 | + * <p> |
| 33 | + * A single implementation of this class will be used globally so methods on this class will be invoked |
| 34 | + * concurrently from multiple threads so all functionality must be thread-safe. |
| 35 | + * <p> |
| 36 | + * Methods are also invoked synchronously and will add to execution time of the single so all behavior |
| 37 | + * should be fast. If anything time-consuming is to be done it should be spawned asynchronously onto separate |
| 38 | + * worker threads. |
| 39 | + * |
| 40 | + */ |
| 41 | +public abstract class RxJavaSingleExecutionHook { |
| 42 | + /** |
| 43 | + * Invoked during the construction by {@link Single#create(Single.OnSubscribe)} |
| 44 | + * <p> |
| 45 | + * This can be used to decorate or replace the <code>onSubscribe</code> function or just perform extra |
| 46 | + * logging, metrics and other such things and pass-thru the function. |
| 47 | + * |
| 48 | + * @param f |
| 49 | + * original {@link Single.OnSubscribe}<{@code T}> to be executed |
| 50 | + * @return {@link Single.OnSubscribe}<{@code T}> function that can be modified, decorated, replaced or just |
| 51 | + * returned as a pass-thru |
| 52 | + */ |
| 53 | + public <T> Single.OnSubscribe<T> onCreate(Single.OnSubscribe<T> f) { |
| 54 | + return f; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Invoked before {@link Single#subscribe(Subscriber)} is about to be executed. |
| 59 | + * <p> |
| 60 | + * This can be used to decorate or replace the <code>onSubscribe</code> function or just perform extra |
| 61 | + * logging, metrics and other such things and pass-thru the function. |
| 62 | + * |
| 63 | + * @param onSubscribe |
| 64 | + * original {@link Observable.OnSubscribe}<{@code T}> to be executed |
| 65 | + * @return {@link Observable.OnSubscribe}<{@code T}> function that can be modified, decorated, replaced or just |
| 66 | + * returned as a pass-thru |
| 67 | + */ |
| 68 | + public <T> Observable.OnSubscribe<T> onSubscribeStart(Single<? extends T> singleInstance, final Observable.OnSubscribe<T> onSubscribe) { |
| 69 | + // pass-thru by default |
| 70 | + return onSubscribe; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Invoked after successful execution of {@link Single#subscribe(Subscriber)} with returned |
| 75 | + * {@link Subscription}. |
| 76 | + * <p> |
| 77 | + * This can be used to decorate or replace the {@link Subscription} instance or just perform extra logging, |
| 78 | + * metrics and other such things and pass-thru the subscription. |
| 79 | + * |
| 80 | + * @param subscription |
| 81 | + * original {@link Subscription} |
| 82 | + * @return {@link Subscription} subscription that can be modified, decorated, replaced or just returned as a |
| 83 | + * pass-thru |
| 84 | + */ |
| 85 | + public <T> Subscription onSubscribeReturn(Subscription subscription) { |
| 86 | + // pass-thru by default |
| 87 | + return subscription; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Invoked after failed execution of {@link Single#subscribe(Subscriber)} with thrown Throwable. |
| 92 | + * <p> |
| 93 | + * This is <em>not</em> errors emitted via {@link Subscriber#onError(Throwable)} but exceptions thrown when |
| 94 | + * attempting to subscribe to a {@link Func1}<{@link Subscriber}{@code <T>}, {@link Subscription}>. |
| 95 | + * |
| 96 | + * @param e |
| 97 | + * Throwable thrown by {@link Single#subscribe(Subscriber)} |
| 98 | + * @return Throwable that can be decorated, replaced or just returned as a pass-thru |
| 99 | + */ |
| 100 | + public <T> Throwable onSubscribeError(Throwable e) { |
| 101 | + // pass-thru by default |
| 102 | + return e; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Invoked just as the operator functions is called to bind two operations together into a new |
| 107 | + * {@link Single} and the return value is used as the lifted function |
| 108 | + * <p> |
| 109 | + * This can be used to decorate or replace the {@link Observable.Operator} instance or just perform extra |
| 110 | + * logging, metrics and other such things and pass-thru the onSubscribe. |
| 111 | + * |
| 112 | + * @param lift |
| 113 | + * original {@link Observable.Operator}{@code <R, T>} |
| 114 | + * @return {@link Observable.Operator}{@code <R, T>} function that can be modified, decorated, replaced or just |
| 115 | + * returned as a pass-thru |
| 116 | + */ |
| 117 | + public <T, R> Observable.Operator<? extends R, ? super T> onLift(final Observable.Operator<? extends R, ? super T> lift) { |
| 118 | + return lift; |
| 119 | + } |
| 120 | +} |
0 commit comments