|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | + * compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 | + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 | + * the License for the specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package io.reactivex.internal.operators.maybe; |
| 15 | + |
| 16 | +import io.reactivex.*; |
| 17 | +import io.reactivex.annotations.Experimental; |
| 18 | +import io.reactivex.disposables.Disposable; |
| 19 | +import io.reactivex.exceptions.Exceptions; |
| 20 | +import io.reactivex.functions.Consumer; |
| 21 | +import io.reactivex.internal.disposables.DisposableHelper; |
| 22 | +import io.reactivex.plugins.RxJavaPlugins; |
| 23 | + |
| 24 | +/** |
| 25 | + * Calls a consumer after pushing the current item to the downstream. |
| 26 | + * @param <T> the value type |
| 27 | + * @since 2.0.1 - experimental |
| 28 | + */ |
| 29 | +@Experimental |
| 30 | +public final class MaybeDoAfterSuccess<T> extends AbstractMaybeWithUpstream<T, T> { |
| 31 | + |
| 32 | + final Consumer<? super T> onAfterSuccess; |
| 33 | + |
| 34 | + public MaybeDoAfterSuccess(MaybeSource<T> source, Consumer<? super T> onAfterSuccess) { |
| 35 | + super(source); |
| 36 | + this.onAfterSuccess = onAfterSuccess; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void subscribeActual(MaybeObserver<? super T> s) { |
| 41 | + source.subscribe(new DoAfterObserver<T>(s, onAfterSuccess)); |
| 42 | + } |
| 43 | + |
| 44 | + static final class DoAfterObserver<T> implements MaybeObserver<T>, Disposable { |
| 45 | + |
| 46 | + final MaybeObserver<? super T> actual; |
| 47 | + |
| 48 | + final Consumer<? super T> onAfterSuccess; |
| 49 | + |
| 50 | + Disposable d; |
| 51 | + |
| 52 | + DoAfterObserver(MaybeObserver<? super T> actual, Consumer<? super T> onAfterSuccess) { |
| 53 | + this.actual = actual; |
| 54 | + this.onAfterSuccess = onAfterSuccess; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onSubscribe(Disposable d) { |
| 59 | + if (DisposableHelper.validate(this.d, d)) { |
| 60 | + this.d = d; |
| 61 | + |
| 62 | + actual.onSubscribe(this); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onSuccess(T t) { |
| 68 | + actual.onSuccess(t); |
| 69 | + |
| 70 | + try { |
| 71 | + onAfterSuccess.accept(t); |
| 72 | + } catch (Throwable ex) { |
| 73 | + Exceptions.throwIfFatal(ex); |
| 74 | + // remember, onSuccess is a terminal event and we can't call onError |
| 75 | + RxJavaPlugins.onError(ex); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onError(Throwable e) { |
| 81 | + actual.onError(e); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onComplete() { |
| 86 | + actual.onComplete(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void dispose() { |
| 91 | + d.dispose(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean isDisposed() { |
| 96 | + return d.isDisposed(); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments