2525import rx .Observer ;
2626import rx .functions .Action0 ;
2727import rx .functions .Action1 ;
28+ import rx .operators .NotificationLite ;
2829import rx .subjects .SubjectSubscriptionManager .SubjectObserver ;
2930
3031/**
@@ -127,7 +128,7 @@ public void onCompleted() {
127128
128129 @ Override
129130 public void call () {
130- state .history .complete (Notification .< T > createOnCompleted () );
131+ state .history .complete ();
131132 }
132133 });
133134 if (observers != null ) {
@@ -145,7 +146,7 @@ public void onError(final Throwable e) {
145146
146147 @ Override
147148 public void call () {
148- state .history .complete (Notification .< T > createOnError ( e ) );
149+ state .history .complete (e );
149150 }
150151 });
151152 if (observers != null ) {
@@ -159,7 +160,7 @@ public void call() {
159160
160161 @ Override
161162 public void onNext (T v ) {
162- if (state .history .terminalValue . get () != null ) {
163+ if (state .history .terminated ) {
163164 return ;
164165 }
165166 state .history .next (v );
@@ -200,12 +201,9 @@ private void replayObserver(SubjectObserver<? super T> observer) {
200201
201202 private static <T > int replayObserverFromIndex (History <T > history , Integer l , SubjectObserver <? super T > observer ) {
202203 while (l < history .index .get ()) {
203- observer . onNext ( history .list . get ( l ) );
204+ history .accept ( observer , l );
204205 l ++;
205206 }
206- if (history .terminalValue .get () != null ) {
207- history .terminalValue .get ().accept (observer );
208- }
209207
210208 return l ;
211209 }
@@ -217,28 +215,43 @@ private static <T> int replayObserverFromIndex(History<T> history, Integer l, Su
217215 * @param <T>
218216 */
219217 private static class History <T > {
218+ private final NotificationLite <T > nl = NotificationLite .instance ();
220219 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 ;
223222
224223 public History (int initialCapacity ) {
225224 index = new AtomicInteger (0 );
226- list = new ArrayList <T >(initialCapacity );
227- terminalValue = new AtomicReference <Notification <T >>();
225+ list = new ArrayList <Object >(initialCapacity );
228226 }
229227
230228 public boolean next (T n ) {
231- if (terminalValue . get () == null ) {
232- list .add (n );
229+ if (! terminated ) {
230+ list .add (nl . next ( n ) );
233231 index .getAndIncrement ();
234232 return true ;
235233 } else {
236234 return false ;
237235 }
238236 }
239237
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+ }
242255 }
243256 }
244257
0 commit comments