Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletableTimeout.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.completable;
15
16 import static io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage;
17
18 import java.util.concurrent.*;
19 import java.util.concurrent.atomic.AtomicBoolean;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.*;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 public final class CompletableTimeout extends Completable {
26
27 final CompletableSource source;
28 final long timeout;
29 final TimeUnit unit;
30 final Scheduler scheduler;
31 final CompletableSource other;
32
33 public CompletableTimeout(CompletableSource source, long timeout,
34 TimeUnit unit, Scheduler scheduler, CompletableSource other) {
35 this.source = source;
36 this.timeout = timeout;
37 this.unit = unit;
38 this.scheduler = scheduler;
39 this.other = other;
40 }
41
42 @Override
43 public void subscribeActual(final CompletableObserver observer) {
44 final CompositeDisposable set = new CompositeDisposable();
45 observer.onSubscribe(set);
46
47 final AtomicBoolean once = new AtomicBoolean();
48
49 Disposable timer = scheduler.scheduleDirect(new DisposeTask(once, set, observer), timeout, unit);
50
51 set.add(timer);
52
53 source.subscribe(new TimeOutObserver(set, once, observer));
54 }
55
56 static final class TimeOutObserver implements CompletableObserver {
57
58 private final CompositeDisposable set;
59 private final AtomicBoolean once;
60 private final CompletableObserver downstream;
61
62 TimeOutObserver(CompositeDisposable set, AtomicBoolean once, CompletableObserver observer) {
63 this.set = set;
64 this.once = once;
65 this.downstream = observer;
66 }
67
68 @Override
69 public void onSubscribe(Disposable d) {
70 set.add(d);
71 }
72
73 @Override
74 public void onError(Throwable e) {
75 if (once.compareAndSet(false, true)) {
76 set.dispose();
77 downstream.onError(e);
78 } else {
79 RxJavaPlugins.onError(e);
80 }
81 }
82
83 @Override
84 public void onComplete() {
85 if (once.compareAndSet(false, true)) {
86 set.dispose();
87 downstream.onComplete();
88 }
89 }
90
91 }
92
93 final class DisposeTask implements Runnable {
94 private final AtomicBoolean once;
95 final CompositeDisposable set;
96 final CompletableObserver downstream;
97
98 DisposeTask(AtomicBoolean once, CompositeDisposable set, CompletableObserver observer) {
99 this.once = once;
100 this.set = set;
101 this.downstream = observer;
102 }
103
104 @Override
105 public void run() {
106 if (once.compareAndSet(false, true)) {
107 set.clear();
108 if (other == null) {
109 downstream.onError(new TimeoutException(timeoutMessage(timeout, unit)));
110 } else {
111 other.subscribe(new DisposeObserver());
112 }
113 }
114 }
115
116 final class DisposeObserver implements CompletableObserver {
117
118 @Override
119 public void onSubscribe(Disposable d) {
120 set.add(d);
121 }
122
123 @Override
124 public void onError(Throwable e) {
125 set.dispose();
126 downstream.onError(e);
127 }
128
129 @Override
130 public void onComplete() {
131 set.dispose();
132 downstream.onComplete();
133 }
134
135 }
136 }
137 }