15
15
*/
16
16
package rx .subjects ;
17
17
18
- import java .util .Collection ;
19
- import java .util .concurrent .atomic .AtomicReference ;
20
-
21
- import rx .Notification ;
22
18
import rx .Observer ;
23
- import rx .functions .Action0 ;
24
19
import rx .functions .Action1 ;
20
+ import rx .operators .NotificationLite ;
25
21
import rx .subjects .SubjectSubscriptionManager .SubjectObserver ;
26
22
27
23
/**
56
52
public final class AsyncSubject <T > extends Subject <T , T > {
57
53
58
54
public static <T > AsyncSubject <T > create () {
59
- final SubjectSubscriptionManager <T > subscriptionManager = new SubjectSubscriptionManager <T >();
60
- final AtomicReference <Notification <T >> lastNotification = new AtomicReference <Notification <T >>(Notification .<T >createOnCompleted ());
61
-
62
- OnSubscribe <T > onSubscribe = subscriptionManager .getOnSubscribeFunc (
63
- /**
64
- * This function executes at beginning of subscription.
65
- *
66
- * This will always run, even if Subject is in terminal state.
67
- */
68
- new Action1 <SubjectObserver <? super T >>() {
69
-
70
- @ Override
71
- public void call (SubjectObserver <? super T > o ) {
72
- // nothing to do if not terminated
73
- }
74
- },
75
- /**
76
- * This function executes if the Subject is terminated.
77
- */
78
- new Action1 <SubjectObserver <? super T >>() {
79
-
80
- @ Override
81
- public void call (SubjectObserver <? super T > o ) {
82
- // we want the last value + completed so add this extra logic
83
- // to send onCompleted if the last value is an onNext
84
- emitValueToObserver (lastNotification .get (), o );
85
- }
86
- }, null );
87
-
88
- return new AsyncSubject <T >(onSubscribe , subscriptionManager , lastNotification );
55
+ final SubjectSubscriptionManager <T > state = new SubjectSubscriptionManager <T >();
56
+ state .onTerminated = new Action1 <SubjectObserver <T >>() {
57
+ @ Override
58
+ public void call (SubjectObserver <T > o ) {
59
+ Object v = state .get ();
60
+ o .accept (v );
61
+ o .completeSingle (v );
62
+ }
63
+ };
64
+ return new AsyncSubject <T >(state , state );
89
65
}
90
66
91
- protected static <T > void emitValueToObserver (Notification <T > n , Observer <? super T > o ) {
92
- n .accept (o );
93
- if (n .isOnNext ()) {
94
- o .onCompleted ();
95
- }
96
- }
67
+ final SubjectSubscriptionManager <T > state ;
68
+ volatile Object lastValue ;
69
+ private final NotificationLite <T > nl = NotificationLite .instance ();
97
70
98
- private final SubjectSubscriptionManager <T > subscriptionManager ;
99
- final AtomicReference <Notification <T >> lastNotification ;
100
71
101
- protected AsyncSubject (OnSubscribe <T > onSubscribe , SubjectSubscriptionManager <T > subscriptionManager , AtomicReference < Notification < T >> lastNotification ) {
72
+ protected AsyncSubject (OnSubscribe <T > onSubscribe , SubjectSubscriptionManager <T > state ) {
102
73
super (onSubscribe );
103
- this .subscriptionManager = subscriptionManager ;
104
- this .lastNotification = lastNotification ;
74
+ this .state = state ;
105
75
}
106
76
107
77
@ Override
108
78
public void onCompleted () {
109
- Collection < SubjectObserver <? super T >> observers = subscriptionManager . terminate ( new Action0 ( ) {
110
-
111
- @ Override
112
- public void call () {
79
+ if ( state . active ) {
80
+ Object last = lastValue ;
81
+ if ( last == null ) {
82
+ last = nl . completed ();
113
83
}
114
- });
115
- if (observers != null ) {
116
- for (Observer <? super T > o : observers ) {
117
- emitValueToObserver (lastNotification .get (), o );
84
+ for (SubjectObserver <T > bo : state .terminate (last )) {
85
+ if (last == nl .completed ()) {
86
+ bo .onCompleted ();
87
+ } else {
88
+ bo .onNext (nl .getValue (last ));
89
+ bo .onCompleted ();
90
+ }
118
91
}
119
92
}
120
93
}
121
94
122
95
@ Override
123
96
public void onError (final Throwable e ) {
124
- Collection <SubjectObserver <? super T >> observers = subscriptionManager .terminate (new Action0 () {
125
- @ Override
126
- public void call () {
127
- lastNotification .set (Notification .<T > createOnError (e ));
128
- }
129
- });
130
- if (observers != null ) {
131
- for (Observer <? super T > o : observers ) {
132
- emitValueToObserver (lastNotification .get (), o );
97
+ if (state .active ) {
98
+ Object n = nl .error (e );
99
+ for (SubjectObserver <T > bo : state .terminate (n )) {
100
+ bo .onError (e );
133
101
}
134
102
}
135
-
136
103
}
137
104
138
105
@ Override
139
106
public void onNext (T v ) {
140
- lastNotification . set ( Notification . createOnNext ( v ) );
107
+ lastValue = nl . next ( v );
141
108
}
142
109
143
110
}
0 commit comments