25
25
import rx .Observer ;
26
26
import rx .functions .Action0 ;
27
27
import rx .functions .Action1 ;
28
+ import rx .operators .NotificationLite ;
28
29
import rx .subjects .SubjectSubscriptionManager .SubjectObserver ;
29
30
30
31
/**
@@ -127,7 +128,7 @@ public void onCompleted() {
127
128
128
129
@ Override
129
130
public void call () {
130
- state .history .complete (Notification .< T > createOnCompleted () );
131
+ state .history .complete ();
131
132
}
132
133
});
133
134
if (observers != null ) {
@@ -145,7 +146,7 @@ public void onError(final Throwable e) {
145
146
146
147
@ Override
147
148
public void call () {
148
- state .history .complete (Notification .< T > createOnError ( e ) );
149
+ state .history .complete (e );
149
150
}
150
151
});
151
152
if (observers != null ) {
@@ -159,7 +160,7 @@ public void call() {
159
160
160
161
@ Override
161
162
public void onNext (T v ) {
162
- if (state .history .terminalValue . get () != null ) {
163
+ if (state .history .terminated ) {
163
164
return ;
164
165
}
165
166
state .history .next (v );
@@ -200,12 +201,9 @@ private void replayObserver(SubjectObserver<? super T> observer) {
200
201
201
202
private static <T > int replayObserverFromIndex (History <T > history , Integer l , SubjectObserver <? super T > observer ) {
202
203
while (l < history .index .get ()) {
203
- observer . onNext ( history .list . get ( l ) );
204
+ history .accept ( observer , l );
204
205
l ++;
205
206
}
206
- if (history .terminalValue .get () != null ) {
207
- history .terminalValue .get ().accept (observer );
208
- }
209
207
210
208
return l ;
211
209
}
@@ -217,28 +215,43 @@ private static <T> int replayObserverFromIndex(History<T> history, Integer l, Su
217
215
* @param <T>
218
216
*/
219
217
private static class History <T > {
218
+ private final NotificationLite <T > nl = NotificationLite .instance ();
220
219
private final AtomicInteger index ;
221
- private final ArrayList <T > list ;
222
- private final AtomicReference < Notification < T >> terminalValue ;
220
+ private final ArrayList <Object > list ;
221
+ private boolean terminated ;
223
222
224
223
public History (int initialCapacity ) {
225
224
index = new AtomicInteger (0 );
226
- list = new ArrayList <T >(initialCapacity );
227
- terminalValue = new AtomicReference <Notification <T >>();
225
+ list = new ArrayList <Object >(initialCapacity );
228
226
}
229
227
230
228
public boolean next (T n ) {
231
- if (terminalValue . get () == null ) {
232
- list .add (n );
229
+ if (! terminated ) {
230
+ list .add (nl . next ( n ) );
233
231
index .getAndIncrement ();
234
232
return true ;
235
233
} else {
236
234
return false ;
237
235
}
238
236
}
239
237
240
- public void complete (Notification <T > n ) {
241
- terminalValue .set (n );
238
+ public void accept (Observer <? super T > o , int idx ) {
239
+ nl .accept (o , list .get (idx ));
240
+ }
241
+
242
+ public void complete () {
243
+ if (!terminated ) {
244
+ terminated = true ;
245
+ list .add (nl .completed ());
246
+ index .getAndIncrement ();
247
+ }
248
+ }
249
+ public void complete (Throwable e ) {
250
+ if (!terminated ) {
251
+ terminated = true ;
252
+ list .add (nl .error (e ));
253
+ index .getAndIncrement ();
254
+ }
242
255
}
243
256
}
244
257
0 commit comments