Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDoAfterNext.java

raw
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.flowable;
15
16 import org.reactivestreams.Subscriber;
17
18 import io.reactivex.rxjava3.annotations.Nullable;
19 import io.reactivex.rxjava3.core.Flowable;
20 import io.reactivex.rxjava3.functions.Consumer;
21 import io.reactivex.rxjava3.internal.subscribers.*;
22 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
23
24 /**
25 * Calls a consumer after pushing the current item to the downstream.
26 * <p>History: 2.0.1 - experimental
27 * @param <T> the value type
28 * @since 2.1
29 */
30 public final class FlowableDoAfterNext<T> extends AbstractFlowableWithUpstream<T, T> {
31
32 final Consumer<? super T> onAfterNext;
33
34 public FlowableDoAfterNext(Flowable<T> source, Consumer<? super T> onAfterNext) {
35 super(source);
36 this.onAfterNext = onAfterNext;
37 }
38
39 @Override
40 protected void subscribeActual(Subscriber<? super T> s) {
41 if (s instanceof ConditionalSubscriber) {
42 source.subscribe(new DoAfterConditionalSubscriber<>((ConditionalSubscriber<? super T>) s, onAfterNext));
43 } else {
44 source.subscribe(new DoAfterSubscriber<>(s, onAfterNext));
45 }
46 }
47
48 static final class DoAfterSubscriber<T> extends BasicFuseableSubscriber<T, T> {
49
50 final Consumer<? super T> onAfterNext;
51
52 DoAfterSubscriber(Subscriber<? super T> actual, Consumer<? super T> onAfterNext) {
53 super(actual);
54 this.onAfterNext = onAfterNext;
55 }
56
57 @Override
58 public void onNext(T t) {
59 if (done) {
60 return;
61 }
62 downstream.onNext(t);
63
64 if (sourceMode == NONE) {
65 try {
66 onAfterNext.accept(t);
67 } catch (Throwable ex) {
68 fail(ex);
69 }
70 }
71 }
72
73 @Override
74 public int requestFusion(int mode) {
75 return transitiveBoundaryFusion(mode);
76 }
77
78 @Nullable
79 @Override
80 public T poll() throws Throwable {
81 T v = qs.poll();
82 if (v != null) {
83 onAfterNext.accept(v);
84 }
85 return v;
86 }
87 }
88
89 static final class DoAfterConditionalSubscriber<T> extends BasicFuseableConditionalSubscriber<T, T> {
90
91 final Consumer<? super T> onAfterNext;
92
93 DoAfterConditionalSubscriber(ConditionalSubscriber<? super T> actual, Consumer<? super T> onAfterNext) {
94 super(actual);
95 this.onAfterNext = onAfterNext;
96 }
97
98 @Override
99 public void onNext(T t) {
100 downstream.onNext(t);
101
102 if (sourceMode == NONE) {
103 try {
104 onAfterNext.accept(t);
105 } catch (Throwable ex) {
106 fail(ex);
107 }
108 }
109 }
110
111 @Override
112 public boolean tryOnNext(T t) {
113 boolean b = downstream.tryOnNext(t);
114 try {
115 onAfterNext.accept(t);
116 } catch (Throwable ex) {
117 fail(ex);
118 }
119 return b;
120 }
121
122 @Override
123 public int requestFusion(int mode) {
124 return transitiveBoundaryFusion(mode);
125 }
126
127 @Nullable
128 @Override
129 public T poll() throws Throwable {
130 T v = qs.poll();
131 if (v != null) {
132 onAfterNext.accept(v);
133 }
134 return v;
135 }
136 }
137 }