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+
17+ package rx .internal .operators ;
18+
19+ import rx .Observable .*;
20+ import rx .Subscriber ;
21+ import rx .exceptions .Exceptions ;
22+ import rx .plugins .*;
23+
24+ /**
25+ * Transforms the downstream Subscriber into a Subscriber via an operator
26+ * callback and calls the parent OnSubscribe.call() method with it.
27+ * @param <T> the source value type
28+ * @param <R> the result value type
29+ */
30+ public final class OnSubscribeLift <T , R > implements OnSubscribe <R > {
31+
32+ static final RxJavaObservableExecutionHook hook = RxJavaPlugins .getInstance ().getObservableExecutionHook ();
33+
34+ final OnSubscribe <T > parent ;
35+
36+ final Operator <? extends R , ? super T > operator ;
37+
38+ public OnSubscribeLift (OnSubscribe <T > parent , Operator <? extends R , ? super T > operator ) {
39+ this .parent = parent ;
40+ this .operator = operator ;
41+ }
42+
43+ @ Override
44+ public void call (Subscriber <? super R > o ) {
45+ try {
46+ Subscriber <? super T > st = hook .onLift (operator ).call (o );
47+ try {
48+ // new Subscriber created and being subscribed with so 'onStart' it
49+ st .onStart ();
50+ parent .call (st );
51+ } catch (Throwable e ) {
52+ // localized capture of errors rather than it skipping all operators
53+ // and ending up in the try/catch of the subscribe method which then
54+ // prevents onErrorResumeNext and other similar approaches to error handling
55+ Exceptions .throwIfFatal (e );
56+ st .onError (e );
57+ }
58+ } catch (Throwable e ) {
59+ Exceptions .throwIfFatal (e );
60+ // if the lift function failed all we can do is pass the error to the final Subscriber
61+ // as we don't have the operator available to us
62+ o .onError (e );
63+ }
64+ }
65+ }
0 commit comments