| 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.maybe; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.*; |
| 19 |
import io.reactivex.rxjava3.functions.*; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* Peeks into the lifecycle of a Maybe and MaybeObserver. |
| 25 |
* |
| 26 |
* @param <T> the value type |
| 27 |
*/ |
| 28 |
public final class MaybePeek<T> extends AbstractMaybeWithUpstream<T, T> { |
| 29 |
|
| 30 |
final Consumer<? super Disposable> onSubscribeCall; |
| 31 |
|
| 32 |
final Consumer<? super T> onSuccessCall; |
| 33 |
|
| 34 |
final Consumer<? super Throwable> onErrorCall; |
| 35 |
|
| 36 |
final Action onCompleteCall; |
| 37 |
|
| 38 |
final Action onAfterTerminate; |
| 39 |
|
| 40 |
final Action onDisposeCall; |
| 41 |
|
| 42 |
public MaybePeek(MaybeSource<T> source, Consumer<? super Disposable> onSubscribeCall, |
| 43 |
Consumer<? super T> onSuccessCall, Consumer<? super Throwable> onErrorCall, Action onCompleteCall, |
| 44 |
Action onAfterTerminate, Action onDispose) { |
| 45 |
super(source); |
| 46 |
this.onSubscribeCall = onSubscribeCall; |
| 47 |
this.onSuccessCall = onSuccessCall; |
| 48 |
this.onErrorCall = onErrorCall; |
| 49 |
this.onCompleteCall = onCompleteCall; |
| 50 |
this.onAfterTerminate = onAfterTerminate; |
| 51 |
this.onDisposeCall = onDispose; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected void subscribeActual(MaybeObserver<? super T> observer) { |
| 56 |
source.subscribe(new MaybePeekObserver<>(observer, this)); |
| 57 |
} |
| 58 |
|
| 59 |
static final class MaybePeekObserver<T> implements MaybeObserver<T>, Disposable { |
| 60 |
final MaybeObserver<? super T> downstream; |
| 61 |
|
| 62 |
final MaybePeek<T> parent; |
| 63 |
|
| 64 |
Disposable upstream; |
| 65 |
|
| 66 |
MaybePeekObserver(MaybeObserver<? super T> actual, MaybePeek<T> parent) { |
| 67 |
this.downstream = actual; |
| 68 |
this.parent = parent; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void dispose() { |
| 73 |
try { |
| 74 |
parent.onDisposeCall.run(); |
| 75 |
} catch (Throwable ex) { |
| 76 |
Exceptions.throwIfFatal(ex); |
| 77 |
RxJavaPlugins.onError(ex); |
| 78 |
} |
| 79 |
|
| 80 |
upstream.dispose(); |
| 81 |
upstream = DisposableHelper.DISPOSED; |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public boolean isDisposed() { |
| 86 |
return upstream.isDisposed(); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onSubscribe(Disposable d) { |
| 91 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 92 |
try { |
| 93 |
parent.onSubscribeCall.accept(d); |
| 94 |
} catch (Throwable ex) { |
| 95 |
Exceptions.throwIfFatal(ex); |
| 96 |
d.dispose(); |
| 97 |
this.upstream = DisposableHelper.DISPOSED; |
| 98 |
EmptyDisposable.error(ex, downstream); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
this.upstream = d; |
| 103 |
|
| 104 |
downstream.onSubscribe(this); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void onSuccess(T value) { |
| 110 |
if (this.upstream == DisposableHelper.DISPOSED) { |
| 111 |
return; |
| 112 |
} |
| 113 |
try { |
| 114 |
parent.onSuccessCall.accept(value); |
| 115 |
} catch (Throwable ex) { |
| 116 |
Exceptions.throwIfFatal(ex); |
| 117 |
onErrorInner(ex); |
| 118 |
return; |
| 119 |
} |
| 120 |
this.upstream = DisposableHelper.DISPOSED; |
| 121 |
|
| 122 |
downstream.onSuccess(value); |
| 123 |
|
| 124 |
onAfterTerminate(); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public void onError(Throwable e) { |
| 129 |
if (this.upstream == DisposableHelper.DISPOSED) { |
| 130 |
RxJavaPlugins.onError(e); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
onErrorInner(e); |
| 135 |
} |
| 136 |
|
| 137 |
void onErrorInner(Throwable e) { |
| 138 |
try { |
| 139 |
parent.onErrorCall.accept(e); |
| 140 |
} catch (Throwable ex) { |
| 141 |
Exceptions.throwIfFatal(ex); |
| 142 |
e = new CompositeException(e, ex); |
| 143 |
} |
| 144 |
|
| 145 |
this.upstream = DisposableHelper.DISPOSED; |
| 146 |
|
| 147 |
downstream.onError(e); |
| 148 |
|
| 149 |
onAfterTerminate(); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void onComplete() { |
| 154 |
if (this.upstream == DisposableHelper.DISPOSED) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
try { |
| 159 |
parent.onCompleteCall.run(); |
| 160 |
} catch (Throwable ex) { |
| 161 |
Exceptions.throwIfFatal(ex); |
| 162 |
onErrorInner(ex); |
| 163 |
return; |
| 164 |
} |
| 165 |
this.upstream = DisposableHelper.DISPOSED; |
| 166 |
|
| 167 |
downstream.onComplete(); |
| 168 |
|
| 169 |
onAfterTerminate(); |
| 170 |
} |
| 171 |
|
| 172 |
void onAfterTerminate() { |
| 173 |
try { |
| 174 |
parent.onAfterTerminate.run(); |
| 175 |
} catch (Throwable ex) { |
| 176 |
Exceptions.throwIfFatal(ex); |
| 177 |
RxJavaPlugins.onError(ex); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |