1+ /**
2+ * Copyright 2013 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 .operators ;
18+
19+ import rx .Observable ;
20+ import rx .Observer ;
21+ import rx .Scheduler ;
22+ import rx .Subscription ;
23+ import rx .subscriptions .CompositeSubscription ;
24+ import rx .subscriptions .MultipleAssignmentSubscription ;
25+ import rx .util .functions .Action0 ;
26+ import rx .util .functions .Action1 ;
27+
28+ public class OperationRepeat <T > implements Observable .OnSubscribeFunc <T > {
29+
30+ private final Observable <T > source ;
31+ private final Scheduler scheduler ;
32+
33+ public static <T > Observable .OnSubscribeFunc <T > repeat (Observable <T > source , Scheduler scheduler ) {
34+ return new OperationRepeat <T >(source , scheduler );
35+ }
36+
37+ private OperationRepeat (Observable <T > source , Scheduler scheduler ) {
38+ this .source = source ;
39+ this .scheduler = scheduler ;
40+ }
41+
42+ @ Override
43+ public Subscription onSubscribe (final Observer <? super T > observer ) {
44+ final CompositeSubscription compositeSubscription = new CompositeSubscription ();
45+ final MultipleAssignmentSubscription innerSubscription = new MultipleAssignmentSubscription ();
46+ compositeSubscription .add (innerSubscription );
47+ compositeSubscription .add (scheduler .schedule (new Action1 <Action0 >() {
48+ @ Override
49+ public void call (final Action0 self ) {
50+ innerSubscription .set (source .subscribe (new Observer <T >() {
51+
52+ @ Override
53+ public void onCompleted () {
54+ self .call ();
55+ }
56+
57+ @ Override
58+ public void onError (Throwable error ) {
59+ observer .onError (error );
60+ }
61+
62+ @ Override
63+ public void onNext (T value ) {
64+ observer .onNext (value );
65+ }
66+ }));
67+ }
68+ }));
69+ return compositeSubscription ;
70+ }
71+ }
0 commit comments