|
| 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 | +package rx.operators; |
| 17 | + |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +import rx.Observable; |
| 21 | +import rx.Scheduler; |
| 22 | +import rx.observables.ConnectableObservable; |
| 23 | +import rx.util.functions.Func1; |
| 24 | + |
| 25 | +public final class OperationDelay { |
| 26 | + |
| 27 | + public static <T> Observable<T> delay(Observable<T> observable, final long delay, final TimeUnit unit, final Scheduler scheduler) { |
| 28 | + // observable.map(x => Observable.timer(t).map(_ => x).startItAlreadyNow()).concat() |
| 29 | + Observable<Observable<T>> seqs = observable.map(new Func1<T, Observable<T>>() { |
| 30 | + public Observable<T> call(final T x) { |
| 31 | + ConnectableObservable<T> co = Observable.timer(delay, unit, scheduler).map(new Func1<Void, T>() { |
| 32 | + public T call(Void ignored) { |
| 33 | + return x; |
| 34 | + } |
| 35 | + }).replay(); |
| 36 | + co.connect(); |
| 37 | + return co; |
| 38 | + } |
| 39 | + }); |
| 40 | + return Observable.concat(seqs); |
| 41 | + } |
| 42 | +} |
0 commit comments