49
49
import rx .operators .OperationMergeDelayError ;
50
50
import rx .operators .OperationMostRecent ;
51
51
import rx .operators .OperationNext ;
52
+ import rx .operators .OperationObserveOn ;
52
53
import rx .operators .OperationOnErrorResumeNextViaFunction ;
53
54
import rx .operators .OperationOnErrorResumeNextViaObservable ;
54
55
import rx .operators .OperationOnErrorReturn ;
55
56
import rx .operators .OperationScan ;
56
57
import rx .operators .OperationSkip ;
58
+ import rx .operators .OperationSubscribeOn ;
57
59
import rx .operators .OperationSynchronize ;
58
60
import rx .operators .OperationTake ;
59
61
import rx .operators .OperationTakeLast ;
@@ -189,6 +191,37 @@ public Subscription subscribe(Observer<T> observer) {
189
191
}
190
192
}
191
193
194
+ /**
195
+ * an {@link Observer} must call an Observable's <code>subscribe</code> method in order to register itself
196
+ * to receive push-based notifications from the Observable. A typical implementation of the
197
+ * <code>subscribe</code> method does the following:
198
+ * <p>
199
+ * It stores a reference to the Observer in a collection object, such as a <code>List<T></code>
200
+ * object.
201
+ * <p>
202
+ * It returns a reference to the {@link Subscription} interface. This enables
203
+ * Observers to unsubscribe (that is, to stop receiving notifications) before the Observable has
204
+ * finished sending them and has called the Observer's {@link Observer#onCompleted()} method.
205
+ * <p>
206
+ * At any given time, a particular instance of an <code>Observable<T></code> implementation is
207
+ * responsible for accepting all subscriptions and notifying all subscribers. Unless the
208
+ * documentation for a particular <code>Observable<T></code> implementation indicates otherwise,
209
+ * Observers should make no assumptions about the <code>Observable<T></code> implementation, such
210
+ * as the order of notifications that multiple Observers will receive.
211
+ * <p>
212
+ * For more information see the <a href="https://github.com/Netflix/RxJava/wiki/Observable">RxJava Wiki</a>
213
+ *
214
+ *
215
+ * @param observer
216
+ * @param scheduler
217
+ * The {@link Scheduler} that the sequence is subscribed to on.
218
+ * @return a {@link Subscription} reference that allows observers
219
+ * to stop receiving notifications before the provider has finished sending them
220
+ */
221
+ public Subscription subscribe (Observer <T > observer , Scheduler scheduler ) {
222
+ return subscribeOn (scheduler ).subscribe (observer );
223
+ }
224
+
192
225
/**
193
226
* Used for protecting against errors being thrown from Observer implementations and ensuring onNext/onError/onCompleted contract compliance.
194
227
* <p>
@@ -237,6 +270,10 @@ public void onNext(Object args) {
237
270
});
238
271
}
239
272
273
+ public Subscription subscribe (final Map <String , Object > callbacks , Scheduler scheduler ) {
274
+ return subscribeOn (scheduler ).subscribe (callbacks );
275
+ }
276
+
240
277
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
241
278
public Subscription subscribe (final Object o ) {
242
279
if (o instanceof Observer ) {
@@ -273,6 +310,10 @@ public void onNext(Object args) {
273
310
});
274
311
}
275
312
313
+ public Subscription subscribe (final Object o , Scheduler scheduler ) {
314
+ return subscribeOn (scheduler ).subscribe (o );
315
+ }
316
+
276
317
public Subscription subscribe (final Action1 <T > onNext ) {
277
318
278
319
/**
@@ -301,6 +342,10 @@ public void onNext(T args) {
301
342
});
302
343
}
303
344
345
+ public Subscription subscribe (final Action1 <T > onNext , Scheduler scheduler ) {
346
+ return subscribeOn (scheduler ).subscribe (onNext );
347
+ }
348
+
304
349
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
305
350
public Subscription subscribe (final Object onNext , final Object onError ) {
306
351
// lookup and memoize onNext
@@ -334,6 +379,10 @@ public void onNext(Object args) {
334
379
});
335
380
}
336
381
382
+ public Subscription subscribe (final Object onNext , final Object onError , Scheduler scheduler ) {
383
+ return subscribeOn (scheduler ).subscribe (onNext , onError );
384
+ }
385
+
337
386
public Subscription subscribe (final Action1 <T > onNext , final Action1 <Exception > onError ) {
338
387
339
388
/**
@@ -364,6 +413,10 @@ public void onNext(T args) {
364
413
});
365
414
}
366
415
416
+ public Subscription subscribe (final Action1 <T > onNext , final Action1 <Exception > onError , Scheduler scheduler ) {
417
+ return subscribeOn (scheduler ).subscribe (onNext , onError );
418
+ }
419
+
367
420
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
368
421
public Subscription subscribe (final Object onNext , final Object onError , final Object onComplete ) {
369
422
// lookup and memoize onNext
@@ -399,6 +452,10 @@ public void onNext(Object args) {
399
452
});
400
453
}
401
454
455
+ public Subscription subscribe (final Object onNext , final Object onError , final Object onComplete , Scheduler scheduler ) {
456
+ return subscribeOn (scheduler ).subscribe (onNext , onError , onComplete );
457
+ }
458
+
402
459
public Subscription subscribe (final Action1 <T > onNext , final Action1 <Exception > onError , final Action0 onComplete ) {
403
460
404
461
/**
@@ -429,6 +486,10 @@ public void onNext(T args) {
429
486
});
430
487
}
431
488
489
+ public Subscription subscribe (final Action1 <T > onNext , final Action1 <Exception > onError , final Action0 onComplete , Scheduler scheduler ) {
490
+ return subscribeOn (scheduler ).subscribe (onNext , onError , onComplete );
491
+ }
492
+
432
493
/**
433
494
* Invokes an action for each element in the observable sequence, and blocks until the sequence is terminated.
434
495
* <p>
@@ -831,6 +892,36 @@ public static Observable<Integer> range(int start, int count) {
831
892
return from (Range .createWithCount (start , count ));
832
893
}
833
894
895
+ /**
896
+ * Asynchronously subscribes and unsubscribes observers on the specified scheduler.
897
+ *
898
+ * @param source
899
+ * the source observable.
900
+ * @param scheduler
901
+ * the scheduler to perform subscription and unsubscription actions on.
902
+ * @param <T>
903
+ * the type of observable.
904
+ * @return the source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.
905
+ */
906
+ public static <T > Observable <T > subscribeOn (Observable <T > source , Scheduler scheduler ) {
907
+ return create (OperationSubscribeOn .subscribeOn (source , scheduler ));
908
+ }
909
+
910
+ /**
911
+ * Asynchronously notify observers on the specified scheduler.
912
+ *
913
+ * @param source
914
+ * the source observable.
915
+ * @param scheduler
916
+ * the scheduler to notify observers on.
917
+ * @param <T>
918
+ * the type of observable.
919
+ * @return the source sequence whose observations happen on the specified scheduler.
920
+ */
921
+ public static <T > Observable <T > observeOn (Observable <T > source , Scheduler scheduler ) {
922
+ return create (OperationObserveOn .observeOn (source , scheduler ));
923
+ }
924
+
834
925
/**
835
926
* Returns an observable sequence that invokes the observable factory whenever a new observer subscribes.
836
927
* The Defer operator allows you to defer or delay the creation of the sequence until the time when an observer
@@ -1242,7 +1333,7 @@ public static <T> Observable<T> concat(Observable<T>... source) {
1242
1333
* @return an Observable that emits the same objects, then calls the action.
1243
1334
* @see <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN: Observable.Finally Method</a>
1244
1335
*/
1245
- public static <T > Observable <T > finallyDo (Observable source , Action0 action ) {
1336
+ public static <T > Observable <T > finallyDo (Observable < T > source , Action0 action ) {
1246
1337
return create (OperationFinally .finallyDo (source , action ));
1247
1338
}
1248
1339
@@ -1756,6 +1847,7 @@ public static <T> Observable<Boolean> all(final Observable<T> sequence, final Fu
1756
1847
* @return true if all elements of an observable sequence satisfies a condition; otherwise, false.
1757
1848
*/
1758
1849
public static <T > Observable <Boolean > all (final Observable <T > sequence , Object predicate ) {
1850
+ @ SuppressWarnings ("rawtypes" )
1759
1851
final FuncN _f = Functions .from (predicate );
1760
1852
1761
1853
return all (sequence , new Func1 <T , Boolean >() {
@@ -2150,7 +2242,7 @@ public static <T> Observable<T> toObservable(Future<T> future) {
2150
2242
*
2151
2243
* @param future
2152
2244
* the source {@link Future}
2153
- * @param time
2245
+ * @param timeout
2154
2246
* the maximum time to wait
2155
2247
* @param unit
2156
2248
* the time unit of the time argument
@@ -2159,8 +2251,8 @@ public static <T> Observable<T> toObservable(Future<T> future) {
2159
2251
* Observable
2160
2252
* @return an Observable that emits the item from the source Future
2161
2253
*/
2162
- public static <T > Observable <T > toObservable (Future <T > future , long time , TimeUnit unit ) {
2163
- return create (OperationToObservableFuture .toObservableFuture (future , time , unit ));
2254
+ public static <T > Observable <T > toObservable (Future <T > future , long timeout , TimeUnit unit ) {
2255
+ return create (OperationToObservableFuture .toObservableFuture (future , timeout , unit ));
2164
2256
}
2165
2257
2166
2258
/**
@@ -2736,6 +2828,28 @@ public Observable<Notification<T>> materialize() {
2736
2828
return materialize (this );
2737
2829
}
2738
2830
2831
+ /**
2832
+ * Asynchronously subscribes and unsubscribes observers on the specified scheduler.
2833
+ *
2834
+ * @param scheduler
2835
+ * the scheduler to perform subscription and unsubscription actions on.
2836
+ * @return the source sequence whose subscriptions and unsubscriptions happen on the specified scheduler.
2837
+ */
2838
+ public Observable <T > subscribeOn (Scheduler scheduler ) {
2839
+ return subscribeOn (this , scheduler );
2840
+ }
2841
+
2842
+ /**
2843
+ * Asynchronously notify observers on the specified scheduler.
2844
+ *
2845
+ * @param scheduler
2846
+ * the scheduler to notify observers on.
2847
+ * @return the source sequence whose observations happen on the specified scheduler.
2848
+ */
2849
+ public Observable <T > observeOn (Scheduler scheduler ) {
2850
+ return observeOn (this , scheduler );
2851
+ }
2852
+
2739
2853
/**
2740
2854
* Dematerializes the explicit notification values of an observable sequence as implicit notifications.
2741
2855
*
@@ -3656,6 +3770,7 @@ public void testMaterializeDematerializeChaining() {
3656
3770
Observable <Integer > obs = Observable .just (1 );
3657
3771
Observable <Integer > chained = obs .materialize ().dematerialize ();
3658
3772
3773
+ @ SuppressWarnings ("unchecked" )
3659
3774
Observer <Integer > observer = mock (Observer .class );
3660
3775
chained .subscribe (observer );
3661
3776
0 commit comments