24
24
import rx .Observer ;
25
25
import rx .Subscription ;
26
26
import rx .operators .SafeObservableSubscription ;
27
+ import rx .subscriptions .Subscriptions ;
27
28
import rx .util .functions .Action1 ;
28
29
29
30
/* package */ class SubjectSubscriptionManager <T > {
@@ -51,10 +52,12 @@ public void call(Observer<? super T> actualObserver) {
51
52
State <T > current ;
52
53
State <T > newState = null ;
53
54
boolean addedObserver = false ;
55
+ Subscription s ;
54
56
do {
55
57
current = state .get ();
56
58
if (current .terminated ) {
57
59
// we are terminated so don't need to do anything
60
+ s = Subscriptions .empty ();
58
61
addedObserver = false ;
59
62
// break out and don't try to modify state
60
63
newState = current ;
@@ -67,24 +70,22 @@ public void call(Observer<? super T> actualObserver) {
67
70
}
68
71
break ;
69
72
} else {
70
- final SafeObservableSubscription subscription = new SafeObservableSubscription ();
71
- actualObserver .add (subscription ); // add to parent if the Subject itself is unsubscribed
72
73
addedObserver = true ;
73
- subscription . wrap ( new Subscription () {
74
+ s = new Subscription () {
74
75
@ Override
75
76
public void unsubscribe () {
76
77
State <T > current ;
77
78
State <T > newState ;
78
79
do {
79
80
current = state .get ();
80
81
// on unsubscribe remove it from the map of outbound observers to notify
81
- newState = current .removeObserver (subscription );
82
+ newState = current .removeObserver (this );
82
83
} while (!state .compareAndSet (current , newState ));
83
84
}
84
- }) ;
85
+ };
85
86
86
87
// on subscribe add it to the map of outbound observers to notify
87
- newState = current .addObserver (subscription , observer );
88
+ newState = current .addObserver (s , observer );
88
89
}
89
90
} while (!state .compareAndSet (current , newState ));
90
91
@@ -94,12 +95,13 @@ public void unsubscribe() {
94
95
if (newState .terminated && !addedObserver ) {
95
96
onTerminated .call (observer );
96
97
}
98
+
99
+ actualObserver .add (s );
97
100
}
98
101
99
102
};
100
103
}
101
104
102
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
103
105
protected void terminate (Action1 <Collection <SubjectObserver <? super T >>> onTerminate ) {
104
106
State <T > current ;
105
107
State <T > newState = null ;
@@ -134,7 +136,6 @@ protected void terminate(Action1<Collection<SubjectObserver<? super T>>> onTermi
134
136
*
135
137
* @return the array of current observers
136
138
*/
137
- @ SuppressWarnings ("unchecked" )
138
139
public SubjectObserver <Object >[] rawSnapshot () {
139
140
return state .get ().observers ;
140
141
}
@@ -231,6 +232,7 @@ protected static class SubjectObserver<T> extends Observer<T> {
231
232
protected volatile boolean caughtUp = false ;
232
233
233
234
SubjectObserver (Observer <? super T > actual ) {
235
+ super (actual );
234
236
this .actual = actual ;
235
237
}
236
238
@@ -251,4 +253,4 @@ public void onNext(T v) {
251
253
252
254
}
253
255
254
- }
256
+ }
0 commit comments