|
23 | 23 | import rx.Scheduler;
|
24 | 24 | import rx.Subscription;
|
25 | 25 | import rx.observables.ConnectableObservable;
|
| 26 | +import rx.subscriptions.CompositeSubscription; |
26 | 27 | import rx.subscriptions.SerialSubscription;
|
| 28 | +import rx.subscriptions.Subscriptions; |
27 | 29 | import rx.util.functions.Action0;
|
| 30 | +import rx.util.functions.Func0; |
28 | 31 | import rx.util.functions.Func1;
|
29 | 32 |
|
30 | 33 | public final class OperationDelay {
|
@@ -82,4 +85,213 @@ public void call() {
|
82 | 85 | return ssub;
|
83 | 86 | }
|
84 | 87 | }
|
| 88 | + /** |
| 89 | + * Delay the emission of the source items by a per-item observable that fires its first element. |
| 90 | + */ |
| 91 | + public static <T, U> OnSubscribeFunc<T> delay(Observable<? extends T> source, |
| 92 | + Func1<? super T, ? extends Observable<U>> itemDelay) { |
| 93 | + return new DelayViaObservable<T, Object, U>(source, null, itemDelay); |
| 94 | + } |
| 95 | + /** |
| 96 | + * Delay the subscription and emission of the source items by a per-item observable that fires its first element. |
| 97 | + */ |
| 98 | + public static <T, U, V> OnSubscribeFunc<T> delay(Observable<? extends T> source, |
| 99 | + Func0<? extends Observable<U>> subscriptionDelay, |
| 100 | + Func1<? super T, ? extends Observable<V>> itemDelay) { |
| 101 | + return new DelayViaObservable<T, U, V>(source, subscriptionDelay, itemDelay); |
| 102 | + } |
| 103 | + /** |
| 104 | + * Delay the emission of the source items by a per-item observable that fires its first element. |
| 105 | + */ |
| 106 | + private static final class DelayViaObservable<T, U, V> implements OnSubscribeFunc<T> { |
| 107 | + final Observable<? extends T> source; |
| 108 | + final Func0<? extends Observable<U>> subscriptionDelay; |
| 109 | + final Func1<? super T, ? extends Observable<V>> itemDelay; |
| 110 | + |
| 111 | + public DelayViaObservable(Observable<? extends T> source, |
| 112 | + Func0<? extends Observable<U>> subscriptionDelay, |
| 113 | + Func1<? super T, ? extends Observable<V>> itemDelay) { |
| 114 | + this.source = source; |
| 115 | + this.subscriptionDelay = subscriptionDelay; |
| 116 | + this.itemDelay = itemDelay; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Subscription onSubscribe(Observer<? super T> t1) { |
| 121 | + CompositeSubscription csub = new CompositeSubscription(); |
| 122 | + |
| 123 | + SerialSubscription sosub = new SerialSubscription(); |
| 124 | + csub.add(sosub); |
| 125 | + SourceObserver<T, V> so = new SourceObserver<T, V>(t1, itemDelay, csub, sosub); |
| 126 | + if (subscriptionDelay == null) { |
| 127 | + sosub.set(source.subscribe(so)); |
| 128 | + } else { |
| 129 | + Observable<U> subscriptionSource; |
| 130 | + try { |
| 131 | + subscriptionSource = subscriptionDelay.call(); |
| 132 | + } catch (Throwable t) { |
| 133 | + t1.onError(t); |
| 134 | + return Subscriptions.empty(); |
| 135 | + } |
| 136 | + SerialSubscription ssub = new SerialSubscription(); |
| 137 | + csub.add(ssub); |
| 138 | + ssub.set(subscriptionSource.subscribe(new SubscribeDelay<T, U, V>(source, so, csub, ssub))); |
| 139 | + } |
| 140 | + |
| 141 | + return csub; |
| 142 | + } |
| 143 | + /** Subscribe delay observer. */ |
| 144 | + private static final class SubscribeDelay<T, U, V> implements Observer<U> { |
| 145 | + final Observable<? extends T> source; |
| 146 | + final SourceObserver<T, V> so; |
| 147 | + final CompositeSubscription csub; |
| 148 | + final Subscription self; |
| 149 | + /** Prevent any onError once the first item was delivered. */ |
| 150 | + boolean subscribed; |
| 151 | + |
| 152 | + public SubscribeDelay( |
| 153 | + Observable<? extends T> source, |
| 154 | + SourceObserver<T, V> so, |
| 155 | + CompositeSubscription csub, Subscription self) { |
| 156 | + this.source = source; |
| 157 | + this.so = so; |
| 158 | + this.csub = csub; |
| 159 | + this.self = self; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onNext(U args) { |
| 164 | + onCompleted(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void onError(Throwable e) { |
| 169 | + if (!subscribed) { |
| 170 | + so.observer.onError(e); |
| 171 | + csub.unsubscribe(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void onCompleted() { |
| 177 | + subscribed = true; |
| 178 | + csub.remove(self); |
| 179 | + so.self.set(source.subscribe(so)); |
| 180 | + } |
| 181 | + } |
| 182 | + /** The source observer. */ |
| 183 | + private static final class SourceObserver<T, U> implements Observer<T> { |
| 184 | + final Observer<? super T> observer; |
| 185 | + final Func1<? super T, ? extends Observable<U>> itemDelay; |
| 186 | + final CompositeSubscription csub; |
| 187 | + final SerialSubscription self; |
| 188 | + /** Guard to avoid overlapping events from the various sources. */ |
| 189 | + final Object guard; |
| 190 | + boolean done; |
| 191 | + int wip; |
| 192 | + |
| 193 | + public SourceObserver(Observer<? super T> observer, |
| 194 | + Func1<? super T, ? extends Observable<U>> itemDelay, |
| 195 | + CompositeSubscription csub, |
| 196 | + SerialSubscription self) { |
| 197 | + this.observer = observer; |
| 198 | + this.itemDelay = itemDelay; |
| 199 | + this.csub = csub; |
| 200 | + this.guard = new Object(); |
| 201 | + this.self = self; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public void onNext(T args) { |
| 206 | + Observable<U> delayer; |
| 207 | + try { |
| 208 | + delayer = itemDelay.call(args); |
| 209 | + } catch (Throwable t) { |
| 210 | + onError(t); |
| 211 | + return; |
| 212 | + } |
| 213 | + |
| 214 | + synchronized (guard) { |
| 215 | + wip++; |
| 216 | + } |
| 217 | + |
| 218 | + SerialSubscription ssub = new SerialSubscription(); |
| 219 | + csub.add(ssub); |
| 220 | + ssub.set(delayer.subscribe(new DelayObserver<T, U>(args, this, ssub))); |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public void onError(Throwable e) { |
| 225 | + synchronized (guard) { |
| 226 | + observer.onError(e); |
| 227 | + } |
| 228 | + csub.unsubscribe(); |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public void onCompleted() { |
| 233 | + boolean b; |
| 234 | + synchronized (guard) { |
| 235 | + done = true; |
| 236 | + b = checkDone(); |
| 237 | + } |
| 238 | + if (b) { |
| 239 | + csub.unsubscribe(); |
| 240 | + } else { |
| 241 | + self.unsubscribe(); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + void emit(T value, Subscription token) { |
| 246 | + boolean b; |
| 247 | + synchronized (guard) { |
| 248 | + observer.onNext(value); |
| 249 | + wip--; |
| 250 | + b = checkDone(); |
| 251 | + } |
| 252 | + if (b) { |
| 253 | + csub.unsubscribe(); |
| 254 | + } else { |
| 255 | + csub.remove(token); |
| 256 | + } |
| 257 | + } |
| 258 | + boolean checkDone() { |
| 259 | + if (done && wip == 0) { |
| 260 | + observer.onCompleted(); |
| 261 | + return true; |
| 262 | + } |
| 263 | + return false; |
| 264 | + } |
| 265 | + } |
| 266 | + /** |
| 267 | + * Delay observer. |
| 268 | + */ |
| 269 | + private static final class DelayObserver<T, U> implements Observer<U> { |
| 270 | + final T value; |
| 271 | + final SourceObserver<T, U> parent; |
| 272 | + final Subscription token; |
| 273 | + |
| 274 | + public DelayObserver(T value, SourceObserver<T, U> parent, Subscription token) { |
| 275 | + this.value = value; |
| 276 | + this.parent = parent; |
| 277 | + this.token = token; |
| 278 | + } |
| 279 | + |
| 280 | + @Override |
| 281 | + public void onNext(U args) { |
| 282 | + parent.emit(value, token); |
| 283 | + } |
| 284 | + |
| 285 | + @Override |
| 286 | + public void onError(Throwable e) { |
| 287 | + parent.onError(e); |
| 288 | + } |
| 289 | + |
| 290 | + @Override |
| 291 | + public void onCompleted() { |
| 292 | + parent.emit(value, token); |
| 293 | + } |
| 294 | + |
| 295 | + } |
| 296 | + } |
85 | 297 | }
|
0 commit comments