@@ -109,6 +109,23 @@ public interface Operator<R, T> extends Func1<Subscriber<? super R>, Subscriber<
109
109
// cover for generics insanity
110
110
}
111
111
112
+ /**
113
+ * Passes all emitted values from {@code this} Observable to the provided {@link ConversionFunc} to be
114
+ * collected and returned as a single value. Note that it is legal for a {@link ConversionFunc} to
115
+ * return an Observable (enabling chaining).
116
+ *
117
+ * @param conversion a function that converts from this {@code Observable<T>} to an {@code R}
118
+ * @return an instance of R created by the provided Conversion
119
+ */
120
+ public <R > R x (ConversionFunc <T , R > conversion ) {
121
+ final Observable <T > self = this ;
122
+ return conversion .convert (new OnSubscribe <T >() {
123
+ @ Override
124
+ public void call (Subscriber <? super T > subscriber ) {
125
+ subscriber .add (Observable .subscribe (subscriber , self ));
126
+ }});
127
+ }
128
+
112
129
/**
113
130
* Lifts a function to the current Observable and returns a new Observable that when subscribed to will pass
114
131
* the values of the current Observable through the Operator function.
@@ -133,11 +150,23 @@ public interface Operator<R, T> extends Func1<Subscriber<? super R>, Subscriber<
133
150
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators">RxJava wiki: Implementing Your Own Operators</a>
134
151
*/
135
152
public final <R > Observable <R > lift (final Operator <? extends R , ? super T > lift ) {
136
- return new Observable <R >(new OnSubscribe <R >() {
153
+ return new Observable <R >(lift (lift , onSubscribe ));
154
+ }
155
+
156
+ /**
157
+ * Wraps the given OnSubscribe in an OnSubscribe that applies the provided operator implementation. The subscription
158
+ * of the provided {@code onSubscribe} will be deferred until the returned OnSubscribe is subscribed.
159
+ *
160
+ * @param operator
161
+ * @param onSubscribe the source OnSubscribe function
162
+ * @return an OnSubscribe that delegates the emitted values from {@code onSubscribe} to the {@code operator}.
163
+ */
164
+ /*package*/ static <R , T > OnSubscribe <R > lift (final Operator <? extends R , ? super T > operator , final OnSubscribe <T > onSubscribe ) {
165
+ return new OnSubscribe <R >() {
137
166
@ Override
138
167
public void call (Subscriber <? super R > o ) {
139
168
try {
140
- Subscriber <? super T > st = hook .onLift (lift ).call (o );
169
+ Subscriber <? super T > st = hook .onLift (operator ).call (o );
141
170
try {
142
171
// new Subscriber created and being subscribed with so 'onStart' it
143
172
st .onStart ();
@@ -160,10 +189,9 @@ public void call(Subscriber<? super R> o) {
160
189
o .onError (e );
161
190
}
162
191
}
163
- }) ;
192
+ };
164
193
}
165
194
166
-
167
195
/**
168
196
* Transform an Observable by applying a particular Transformer function to it.
169
197
* <p>
@@ -7737,11 +7765,15 @@ public final Subscription unsafeSubscribe(Subscriber<? super T> subscriber) {
7737
7765
* @see <a href="http://reactivex.io/documentation/operators/subscribe.html">ReactiveX operators documentation: Subscribe</a>
7738
7766
*/
7739
7767
public final Subscription subscribe (Subscriber <? super T > subscriber ) {
7740
- // validate and proceed
7768
+ return Observable .subscribe (subscriber , this );
7769
+ }
7770
+
7771
+ private static <T > Subscription subscribe (Subscriber <? super T > subscriber , Observable <T > observable ) {
7772
+ // validate and proceed
7741
7773
if (subscriber == null ) {
7742
7774
throw new IllegalArgumentException ("observer can not be null" );
7743
7775
}
7744
- if (onSubscribe == null ) {
7776
+ if (observable . onSubscribe == null ) {
7745
7777
throw new IllegalStateException ("onSubscribe function can not be null." );
7746
7778
/*
7747
7779
* the subscribe function can also be overridden but generally that's not the appropriate approach
@@ -7765,7 +7797,7 @@ public final Subscription subscribe(Subscriber<? super T> subscriber) {
7765
7797
// The code below is exactly the same an unsafeSubscribe but not used because it would add a sigificent depth to alreay huge call stacks.
7766
7798
try {
7767
7799
// allow the hook to intercept and/or decorate
7768
- hook .onSubscribeStart (this , onSubscribe ).call (subscriber );
7800
+ hook .onSubscribeStart (observable , observable . onSubscribe ).call (subscriber );
7769
7801
return hook .onSubscribeReturn (subscriber );
7770
7802
} catch (Throwable e ) {
7771
7803
// special handling for certain Throwable/Error/Exception types
0 commit comments