| 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 java.util.concurrent.TimeUnit; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import io.reactivex.rxjava3.flowables.ConnectableFlowable; |
| 24 |
import io.reactivex.rxjava3.functions.Consumer; |
| 25 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 27 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns an observable sequence that stays connected to the source as long as |
| 31 |
* there is at least one subscription to the observable sequence. |
| 32 |
* |
| 33 |
* @param <T> |
| 34 |
* the value type |
| 35 |
*/ |
| 36 |
public final class FlowableRefCount<T> extends Flowable<T> { |
| 37 |
|
| 38 |
final ConnectableFlowable<T> source; |
| 39 |
|
| 40 |
final int n; |
| 41 |
|
| 42 |
final long timeout; |
| 43 |
|
| 44 |
final TimeUnit unit; |
| 45 |
|
| 46 |
final Scheduler scheduler; |
| 47 |
|
| 48 |
RefConnection connection; |
| 49 |
|
| 50 |
public FlowableRefCount(ConnectableFlowable<T> source) { |
| 51 |
this(source, 1, 0L, TimeUnit.NANOSECONDS, null); |
| 52 |
} |
| 53 |
|
| 54 |
public FlowableRefCount(ConnectableFlowable<T> source, int n, long timeout, TimeUnit unit, |
| 55 |
Scheduler scheduler) { |
| 56 |
this.source = source; |
| 57 |
this.n = n; |
| 58 |
this.timeout = timeout; |
| 59 |
this.unit = unit; |
| 60 |
this.scheduler = scheduler; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 65 |
|
| 66 |
RefConnection conn; |
| 67 |
|
| 68 |
boolean connect = false; |
| 69 |
synchronized (this) { |
| 70 |
conn = connection; |
| 71 |
if (conn == null) { |
| 72 |
conn = new RefConnection(this); |
| 73 |
connection = conn; |
| 74 |
} |
| 75 |
|
| 76 |
long c = conn.subscriberCount; |
| 77 |
if (c == 0L && conn.timer != null) { |
| 78 |
conn.timer.dispose(); |
| 79 |
} |
| 80 |
conn.subscriberCount = c + 1; |
| 81 |
if (!conn.connected && c + 1 == n) { |
| 82 |
connect = true; |
| 83 |
conn.connected = true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
source.subscribe(new RefCountSubscriber<>(s, this, conn)); |
| 88 |
|
| 89 |
if (connect) { |
| 90 |
source.connect(conn); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void cancel(RefConnection rc) { |
| 95 |
SequentialDisposable sd; |
| 96 |
synchronized (this) { |
| 97 |
if (connection == null || connection != rc) { |
| 98 |
return; |
| 99 |
} |
| 100 |
long c = rc.subscriberCount - 1; |
| 101 |
rc.subscriberCount = c; |
| 102 |
if (c != 0L || !rc.connected) { |
| 103 |
return; |
| 104 |
} |
| 105 |
if (timeout == 0L) { |
| 106 |
timeout(rc); |
| 107 |
return; |
| 108 |
} |
| 109 |
sd = new SequentialDisposable(); |
| 110 |
rc.timer = sd; |
| 111 |
} |
| 112 |
|
| 113 |
sd.replace(scheduler.scheduleDirect(rc, timeout, unit)); |
| 114 |
} |
| 115 |
|
| 116 |
void terminated(RefConnection rc) { |
| 117 |
synchronized (this) { |
| 118 |
if (connection == rc) { |
| 119 |
if (rc.timer != null) { |
| 120 |
rc.timer.dispose(); |
| 121 |
rc.timer = null; |
| 122 |
} |
| 123 |
if (--rc.subscriberCount == 0) { |
| 124 |
connection = null; |
| 125 |
source.reset(); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void timeout(RefConnection rc) { |
| 132 |
synchronized (this) { |
| 133 |
if (rc.subscriberCount == 0 && rc == connection) { |
| 134 |
connection = null; |
| 135 |
Disposable connectionObject = rc.get(); |
| 136 |
DisposableHelper.dispose(rc); |
| 137 |
if (connectionObject == null) { |
| 138 |
rc.disconnectedEarly = true; |
| 139 |
} else { |
| 140 |
source.reset(); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
static final class RefConnection extends AtomicReference<Disposable> |
| 147 |
implements Runnable, Consumer<Disposable> { |
| 148 |
|
| 149 |
private static final long serialVersionUID = -4552101107598366241L; |
| 150 |
|
| 151 |
final FlowableRefCount<?> parent; |
| 152 |
|
| 153 |
Disposable timer; |
| 154 |
|
| 155 |
long subscriberCount; |
| 156 |
|
| 157 |
boolean connected; |
| 158 |
|
| 159 |
boolean disconnectedEarly; |
| 160 |
|
| 161 |
RefConnection(FlowableRefCount<?> parent) { |
| 162 |
this.parent = parent; |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void run() { |
| 167 |
parent.timeout(this); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public void accept(Disposable t) { |
| 172 |
DisposableHelper.replace(this, t); |
| 173 |
synchronized (parent) { |
| 174 |
if (disconnectedEarly) { |
| 175 |
parent.source.reset(); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
static final class RefCountSubscriber<T> |
| 182 |
extends AtomicBoolean implements FlowableSubscriber<T>, Subscription { |
| 183 |
|
| 184 |
private static final long serialVersionUID = -7419642935409022375L; |
| 185 |
|
| 186 |
final Subscriber<? super T> downstream; |
| 187 |
|
| 188 |
final FlowableRefCount<T> parent; |
| 189 |
|
| 190 |
final RefConnection connection; |
| 191 |
|
| 192 |
Subscription upstream; |
| 193 |
|
| 194 |
RefCountSubscriber(Subscriber<? super T> actual, FlowableRefCount<T> parent, RefConnection connection) { |
| 195 |
this.downstream = actual; |
| 196 |
this.parent = parent; |
| 197 |
this.connection = connection; |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public void onNext(T t) { |
| 202 |
downstream.onNext(t); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public void onError(Throwable t) { |
| 207 |
if (compareAndSet(false, true)) { |
| 208 |
parent.terminated(connection); |
| 209 |
downstream.onError(t); |
| 210 |
} else { |
| 211 |
RxJavaPlugins.onError(t); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void onComplete() { |
| 217 |
if (compareAndSet(false, true)) { |
| 218 |
parent.terminated(connection); |
| 219 |
downstream.onComplete(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public void request(long n) { |
| 225 |
upstream.request(n); |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public void cancel() { |
| 230 |
upstream.cancel(); |
| 231 |
if (compareAndSet(false, true)) { |
| 232 |
parent.cancel(connection); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public void onSubscribe(Subscription s) { |
| 238 |
if (SubscriptionHelper.validate(upstream, s)) { |
| 239 |
this.upstream = s; |
| 240 |
|
| 241 |
downstream.onSubscribe(this); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |