| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 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.rxjava3.internal.operators.observable; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 17 |
import io.reactivex.rxjava3.core.*; |
| 18 |
import io.reactivex.rxjava3.functions.Consumer; |
| 19 |
import io.reactivex.rxjava3.internal.observers.BasicFuseableObserver; |
| 20 |
|
| 21 |
/** |
| 22 |
* Calls a consumer after pushing the current item to the downstream. |
| 23 |
* <p>History: 2.0.1 - experimental |
| 24 |
* @param <T> the value type |
| 25 |
* @since 2.1 |
| 26 |
*/ |
| 27 |
public final class ObservableDoAfterNext<T> extends AbstractObservableWithUpstream<T, T> { |
| 28 |
|
| 29 |
final Consumer<? super T> onAfterNext; |
| 30 |
|
| 31 |
public ObservableDoAfterNext(ObservableSource<T> source, Consumer<? super T> onAfterNext) { |
| 32 |
super(source); |
| 33 |
this.onAfterNext = onAfterNext; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void subscribeActual(Observer<? super T> observer) { |
| 38 |
source.subscribe(new DoAfterObserver<>(observer, onAfterNext)); |
| 39 |
} |
| 40 |
|
| 41 |
static final class DoAfterObserver<T> extends BasicFuseableObserver<T, T> { |
| 42 |
|
| 43 |
final Consumer<? super T> onAfterNext; |
| 44 |
|
| 45 |
DoAfterObserver(Observer<? super T> actual, Consumer<? super T> onAfterNext) { |
| 46 |
super(actual); |
| 47 |
this.onAfterNext = onAfterNext; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void onNext(T t) { |
| 52 |
downstream.onNext(t); |
| 53 |
|
| 54 |
if (sourceMode == NONE) { |
| 55 |
try { |
| 56 |
onAfterNext.accept(t); |
| 57 |
} catch (Throwable ex) { |
| 58 |
fail(ex); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public int requestFusion(int mode) { |
| 65 |
return transitiveBoundaryFusion(mode); |
| 66 |
} |
| 67 |
|
| 68 |
@Nullable |
| 69 |
@Override |
| 70 |
public T poll() throws Throwable { |
| 71 |
T v = qd.poll(); |
| 72 |
if (v != null) { |
| 73 |
onAfterNext.accept(v); |
| 74 |
} |
| 75 |
return v; |
| 76 |
} |
| 77 |
} |
| 78 |
} |