Debian Codegraph @

src/main/java/io/reactivex/rxjava3/subjects/SerializedSubject.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.subjects;
15
16 import io.reactivex.rxjava3.annotations.Nullable;
17 import io.reactivex.rxjava3.core.Observer;
18 import io.reactivex.rxjava3.disposables.Disposable;
19 import io.reactivex.rxjava3.internal.util.*;
20 import io.reactivex.rxjava3.internal.util.AppendOnlyLinkedArrayList.NonThrowingPredicate;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 /**
24 * Serializes calls to the Observer methods.
25 * <p>All other Observable and Subject methods are thread-safe by design.
26 *
27 * @param <T> the item value type
28 */
29 /* public */ final class SerializedSubject<T> extends Subject<T> implements NonThrowingPredicate<Object> {
30 /** The actual subscriber to serialize Subscriber calls to. */
31 final Subject<T> actual;
32 /** Indicates an emission is going on, guarded by this. */
33 boolean emitting;
34 /** If not null, it holds the missed NotificationLite events. */
35 AppendOnlyLinkedArrayList<Object> queue;
36 /** Indicates a terminal event has been received and all further events will be dropped. */
37 volatile boolean done;
38
39 /**
40 * Constructor that wraps an actual subject.
41 * @param actual the subject wrapped
42 */
43 SerializedSubject(final Subject<T> actual) {
44 this.actual = actual;
45 }
46
47 @Override
48 protected void subscribeActual(Observer<? super T> observer) {
49 actual.subscribe(observer);
50 }
51
52 @Override
53 public void onSubscribe(Disposable d) {
54 boolean cancel;
55 if (!done) {
56 synchronized (this) {
57 if (done) {
58 cancel = true;
59 } else {
60 if (emitting) {
61 AppendOnlyLinkedArrayList<Object> q = queue;
62 if (q == null) {
63 q = new AppendOnlyLinkedArrayList<>(4);
64 queue = q;
65 }
66 q.add(NotificationLite.disposable(d));
67 return;
68 }
69 emitting = true;
70 cancel = false;
71 }
72 }
73 } else {
74 cancel = true;
75 }
76 if (cancel) {
77 d.dispose();
78 } else {
79 actual.onSubscribe(d);
80 emitLoop();
81 }
82 }
83
84 @Override
85 public void onNext(T t) {
86 if (done) {
87 return;
88 }
89 synchronized (this) {
90 if (done) {
91 return;
92 }
93 if (emitting) {
94 AppendOnlyLinkedArrayList<Object> q = queue;
95 if (q == null) {
96 q = new AppendOnlyLinkedArrayList<>(4);
97 queue = q;
98 }
99 q.add(NotificationLite.next(t));
100 return;
101 }
102 emitting = true;
103 }
104 actual.onNext(t);
105 emitLoop();
106 }
107
108 @Override
109 public void onError(Throwable t) {
110 if (done) {
111 RxJavaPlugins.onError(t);
112 return;
113 }
114 boolean reportError;
115 synchronized (this) {
116 if (done) {
117 reportError = true;
118 } else {
119 done = true;
120 if (emitting) {
121 AppendOnlyLinkedArrayList<Object> q = queue;
122 if (q == null) {
123 q = new AppendOnlyLinkedArrayList<>(4);
124 queue = q;
125 }
126 q.setFirst(NotificationLite.error(t));
127 return;
128 }
129 reportError = false;
130 emitting = true;
131 }
132 }
133 if (reportError) {
134 RxJavaPlugins.onError(t);
135 return;
136 }
137 actual.onError(t);
138 }
139
140 @Override
141 public void onComplete() {
142 if (done) {
143 return;
144 }
145 synchronized (this) {
146 if (done) {
147 return;
148 }
149 done = true;
150 if (emitting) {
151 AppendOnlyLinkedArrayList<Object> q = queue;
152 if (q == null) {
153 q = new AppendOnlyLinkedArrayList<>(4);
154 queue = q;
155 }
156 q.add(NotificationLite.complete());
157 return;
158 }
159 emitting = true;
160 }
161 actual.onComplete();
162 }
163
164 /** Loops until all notifications in the queue has been processed. */
165 void emitLoop() {
166 for (;;) {
167 AppendOnlyLinkedArrayList<Object> q;
168 synchronized (this) {
169 q = queue;
170 if (q == null) {
171 emitting = false;
172 return;
173 }
174 queue = null;
175 }
176 q.forEachWhile(this);
177 }
178 }
179
180 @Override
181 public boolean test(Object o) {
182 return NotificationLite.acceptFull(o, actual);
183 }
184
185 @Override
186 public boolean hasObservers() {
187 return actual.hasObservers();
188 }
189
190 @Override
191 public boolean hasThrowable() {
192 return actual.hasThrowable();
193 }
194
195 @Override
196 @Nullable
197 public Throwable getThrowable() {
198 return actual.getThrowable();
199 }
200
201 @Override
202 public boolean hasComplete() {
203 return actual.hasComplete();
204 }
205 }