-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathObservableConcatMap.java
206 lines (186 loc) · 6.53 KB
/
ObservableConcatMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.internal.operators.observable;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import io.reactivex.*;
import io.reactivex.disposables.*;
import io.reactivex.functions.Function;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.internal.queue.SpscLinkedArrayQueue;
import io.reactivex.observers.SerializedObserver;
import io.reactivex.plugins.RxJavaPlugins;
public final class ObservableConcatMap<T, U> extends ObservableSource<T, U> {
final Function<? super T, ? extends ObservableConsumable<? extends U>> mapper;
final int bufferSize;
public ObservableConcatMap(ObservableConsumable<T> source, Function<? super T, ? extends ObservableConsumable<? extends U>> mapper, int bufferSize) {
super(source);
this.mapper = mapper;
this.bufferSize = Math.max(8, bufferSize);
}
@Override
public void subscribeActual(Observer<? super U> s) {
SerializedObserver<U> ssub = new SerializedObserver<U>(s);
SerialDisposable sa = new SerialDisposable();
ssub.onSubscribe(sa);
source.subscribe(new SourceSubscriber<T, U>(ssub, sa, mapper, bufferSize));
}
static final class SourceSubscriber<T, U> extends AtomicInteger implements Observer<T> {
/** */
private static final long serialVersionUID = 8828587559905699186L;
final Observer<? super U> actual;
final SerialDisposable sa;
final Function<? super T, ? extends ObservableConsumable<? extends U>> mapper;
final Observer<U> inner;
final Queue<T> queue;
final int bufferSize;
Disposable s;
volatile boolean done;
volatile long index;
public SourceSubscriber(Observer<? super U> actual, SerialDisposable sa,
Function<? super T, ? extends ObservableConsumable<? extends U>> mapper, int bufferSize) {
this.actual = actual;
this.sa = sa;
this.mapper = mapper;
this.bufferSize = bufferSize;
this.inner = new InnerSubscriber<U>(actual, sa, this);
this.queue = new SpscLinkedArrayQueue<T>(bufferSize);
}
@Override
public void onSubscribe(Disposable s) {
if (DisposableHelper.validate(this.s, s)) {
this.s = s;
}
}
@Override
public void onNext(T t) {
if (done) {
return;
}
if (!queue.offer(t)) {
cancel();
actual.onError(new IllegalStateException("More values received than requested!"));
return;
}
if (getAndIncrement() == 0) {
drain();
}
}
@Override
public void onError(Throwable t) {
if (done) {
RxJavaPlugins.onError(t);
return;
}
done = true;
cancel();
actual.onError(t);
}
@Override
public void onComplete() {
if (done) {
return;
}
done = true;
if (getAndIncrement() == 0) {
drain();
}
}
void innerComplete() {
if (decrementAndGet() != 0) {
drain();
}
}
void cancel() {
sa.dispose();
s.dispose();
}
void drain() {
boolean d = done;
T o = queue.poll();
if (o == null) {
if (d) {
actual.onComplete();
return;
}
RxJavaPlugins.onError(new IllegalStateException("Queue is empty?!"));
return;
}
ObservableConsumable<? extends U> p;
try {
p = mapper.apply(o);
} catch (Throwable e) {
cancel();
actual.onError(e);
return;
}
if (p == null) {
cancel();
actual.onError(new NullPointerException("The NbpObservable returned is null"));
return;
}
index++;
// this is not RS but since our Subscriber doesn't hold state by itself,
// subscribing it to each source is safe and saves allocation
p.subscribe(inner);
}
}
static final class InnerSubscriber<U> implements Observer<U> {
final Observer<? super U> actual;
final SerialDisposable sa;
final SourceSubscriber<?, ?> parent;
/*
* FIXME this is a workaround for now, but doesn't work
* for async non-conforming sources.
* Such sources require individual instances of InnerSubscriber and a
* done field.
*/
long index;
public InnerSubscriber(Observer<? super U> actual,
SerialDisposable sa, SourceSubscriber<?, ?> parent) {
this.actual = actual;
this.sa = sa;
this.parent = parent;
this.index = 1;
}
@Override
public void onSubscribe(Disposable s) {
if (index == parent.index) {
sa.set(s);
}
}
@Override
public void onNext(U t) {
if (index == parent.index) {
actual.onNext(t);
}
}
@Override
public void onError(Throwable t) {
if (index == parent.index) {
index++;
parent.cancel();
actual.onError(t);
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
if (index == parent.index) {
index++;
parent.innerComplete();
}
}
}
}