|
28 | 28 | import rx.schedulers.Schedulers;
|
29 | 29 | import rx.subscriptions.CompositeSubscription;
|
30 | 30 | import rx.subscriptions.SerialSubscription;
|
| 31 | +import rx.subscriptions.Subscriptions; |
31 | 32 | import rx.util.functions.Action0;
|
32 | 33 | import rx.util.functions.Func0;
|
| 34 | +import rx.util.functions.Func1; |
33 | 35 |
|
34 | 36 | /**
|
35 | 37 | * Applies a timeout policy for each element in the observable sequence, using
|
@@ -154,4 +156,160 @@ public void onCompleted() {
|
154 | 156 | return composite;
|
155 | 157 | }
|
156 | 158 | }
|
| 159 | + |
| 160 | + /** Timeout using a per-item observable sequence. */ |
| 161 | + public static <T, U, V> OnSubscribeFunc<T> timeoutSelector(Observable<? extends T> source, Func0<? extends Observable<U>> firstValueTimeout, Func1<? super T, ? extends Observable<V>> valueTimeout, Observable<? extends T> other) { |
| 162 | + return new TimeoutSelector<T, U, V>(source, firstValueTimeout, valueTimeout, other); |
| 163 | + } |
| 164 | + |
| 165 | + /** Timeout using a per-item observable sequence. */ |
| 166 | + private static final class TimeoutSelector<T, U, V> implements OnSubscribeFunc<T> { |
| 167 | + final Observable<? extends T> source; |
| 168 | + final Func0<? extends Observable<U>> firstValueTimeout; |
| 169 | + final Func1<? super T, ? extends Observable<V>> valueTimeout; |
| 170 | + final Observable<? extends T> other; |
| 171 | + |
| 172 | + public TimeoutSelector(Observable<? extends T> source, Func0<? extends Observable<U>> firstValueTimeout, Func1<? super T, ? extends Observable<V>> valueTimeout, Observable<? extends T> other) { |
| 173 | + this.source = source; |
| 174 | + this.firstValueTimeout = firstValueTimeout; |
| 175 | + this.valueTimeout = valueTimeout; |
| 176 | + this.other = other; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public Subscription onSubscribe(Observer<? super T> t1) { |
| 181 | + CompositeSubscription csub = new CompositeSubscription(); |
| 182 | + |
| 183 | + SourceObserver<T, V> so = new SourceObserver<T, V>(t1, valueTimeout, other, csub); |
| 184 | + if (firstValueTimeout != null) { |
| 185 | + Observable<U> o; |
| 186 | + try { |
| 187 | + o = firstValueTimeout.call(); |
| 188 | + } catch (Throwable t) { |
| 189 | + t1.onError(t); |
| 190 | + return Subscriptions.empty(); |
| 191 | + } |
| 192 | + |
| 193 | + csub.add(o.subscribe(new TimeoutObserver<U>(so))); |
| 194 | + } |
| 195 | + csub.add(source.subscribe(so)); |
| 196 | + return csub; |
| 197 | + } |
| 198 | + |
| 199 | + /** Observe the source. */ |
| 200 | + private static final class SourceObserver<T, V> implements Observer<T>, TimeoutCallback { |
| 201 | + final Observer<? super T> observer; |
| 202 | + final Func1<? super T, ? extends Observable<V>> valueTimeout; |
| 203 | + final Observable<? extends T> other; |
| 204 | + final CompositeSubscription cancel; |
| 205 | + final Object guard; |
| 206 | + boolean done; |
| 207 | + final SerialSubscription tsub; |
| 208 | + final TimeoutObserver<V> to; |
| 209 | + |
| 210 | + public SourceObserver(Observer<? super T> observer, Func1<? super T, ? extends Observable<V>> valueTimeout, Observable<? extends T> other, CompositeSubscription cancel) { |
| 211 | + this.observer = observer; |
| 212 | + this.valueTimeout = valueTimeout; |
| 213 | + this.other = other; |
| 214 | + this.cancel = cancel; |
| 215 | + this.guard = new Object(); |
| 216 | + this.tsub = new SerialSubscription(); |
| 217 | + this.cancel.add(tsub); |
| 218 | + this.to = new TimeoutObserver<V>(this); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public void onNext(T args) { |
| 223 | + tsub.set(Subscriptions.empty()); |
| 224 | + |
| 225 | + synchronized (guard) { |
| 226 | + if (done) { |
| 227 | + return; |
| 228 | + } |
| 229 | + observer.onNext(args); |
| 230 | + } |
| 231 | + |
| 232 | + Observable<V> o; |
| 233 | + try { |
| 234 | + o = valueTimeout.call(args); |
| 235 | + } catch (Throwable t) { |
| 236 | + onError(t); |
| 237 | + return; |
| 238 | + } |
| 239 | + |
| 240 | + SerialSubscription osub = new SerialSubscription(); |
| 241 | + tsub.set(osub); |
| 242 | + |
| 243 | + osub.set(o.subscribe(to)); |
| 244 | + } |
| 245 | + @Override |
| 246 | + public void onError(Throwable e) { |
| 247 | + synchronized (guard) { |
| 248 | + if (done) { |
| 249 | + return; |
| 250 | + } |
| 251 | + done = true; |
| 252 | + observer.onError(e); |
| 253 | + } |
| 254 | + cancel.unsubscribe(); |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public void onCompleted() { |
| 259 | + synchronized (guard) { |
| 260 | + if (done) { |
| 261 | + return; |
| 262 | + } |
| 263 | + done = true; |
| 264 | + observer.onCompleted(); |
| 265 | + } |
| 266 | + cancel.unsubscribe(); |
| 267 | + } |
| 268 | + @Override |
| 269 | + public void timeout() { |
| 270 | + if (other != null) { |
| 271 | + synchronized (guard) { |
| 272 | + if (done) { |
| 273 | + return; |
| 274 | + } |
| 275 | + done = true; |
| 276 | + } |
| 277 | + cancel.clear(); |
| 278 | + cancel.add(other.subscribe(observer)); |
| 279 | + } else { |
| 280 | + onCompleted(); |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + /** The timeout callback. */ |
| 286 | + private interface TimeoutCallback { |
| 287 | + void timeout(); |
| 288 | + void onError(Throwable t); |
| 289 | + } |
| 290 | + |
| 291 | + /** Observe the timeout. */ |
| 292 | + private static final class TimeoutObserver<V> implements Observer<V> { |
| 293 | + final TimeoutCallback parent; |
| 294 | + |
| 295 | + public TimeoutObserver(TimeoutCallback parent) { |
| 296 | + this.parent = parent; |
| 297 | + } |
| 298 | + |
| 299 | + @Override |
| 300 | + public void onNext(V args) { |
| 301 | + parent.timeout(); |
| 302 | + } |
| 303 | + |
| 304 | + @Override |
| 305 | + public void onError(Throwable e) { |
| 306 | + parent.onError(e); |
| 307 | + } |
| 308 | + |
| 309 | + @Override |
| 310 | + public void onCompleted() { |
| 311 | + parent.timeout(); |
| 312 | + } |
| 313 | + } |
| 314 | + } |
157 | 315 | }
|
0 commit comments