|
18 | 18 | import java.util.concurrent.ConcurrentHashMap;
|
19 | 19 | import java.util.concurrent.atomic.AtomicBoolean;
|
20 | 20 | import java.util.concurrent.atomic.AtomicReference;
|
| 21 | +import java.util.concurrent.locks.ReentrantLock; |
21 | 22 |
|
| 23 | +import rx.Notification; |
22 | 24 | import rx.Observer;
|
23 | 25 | import rx.Subscription;
|
24 | 26 | import rx.operators.SafeObservableSubscription;
|
| 27 | +import rx.subscriptions.Subscriptions; |
25 | 28 |
|
26 | 29 | /**
|
27 | 30 | * Subject that publishes only the last event to each {@link Observer} that has subscribed when the
|
@@ -60,61 +63,115 @@ public class AsyncSubject<T> extends Subject<T, T> {
|
60 | 63 | * @return a new AsyncSubject
|
61 | 64 | */
|
62 | 65 | public static <T> AsyncSubject<T> create() {
|
63 |
| - final ConcurrentHashMap<Subscription, Observer<? super T>> observers = new ConcurrentHashMap<Subscription, Observer<? super T>>(); |
| 66 | + final AsyncSubjectState<T> state = new AsyncSubjectState<T>(); |
64 | 67 |
|
65 | 68 | OnSubscribeFunc<T> onSubscribe = new OnSubscribeFunc<T>() {
|
66 | 69 | @Override
|
67 | 70 | public Subscription onSubscribe(Observer<? super T> observer) {
|
68 |
| - final SafeObservableSubscription subscription = new SafeObservableSubscription(); |
69 |
| - |
70 |
| - subscription.wrap(new Subscription() { |
71 |
| - @Override |
72 |
| - public void unsubscribe() { |
73 |
| - // on unsubscribe remove it from the map of outbound observers to notify |
74 |
| - observers.remove(subscription); |
| 71 | + /* |
| 72 | + * Subscription needs to be synchronized with terminal states to ensure |
| 73 | + * race conditions are handled. When subscribing we must make sure |
| 74 | + * onComplete/onError is correctly emitted to all observers, even if it |
| 75 | + * comes in while the onComplete/onError is being propagated. |
| 76 | + */ |
| 77 | + state.SUBSCRIPTION_LOCK.lock(); |
| 78 | + try { |
| 79 | + if (state.completed.get()) { |
| 80 | + emitNotificationToObserver(state, observer); |
| 81 | + return Subscriptions.empty(); |
| 82 | + } else { |
| 83 | + // the subject is not completed so we subscribe |
| 84 | + final SafeObservableSubscription subscription = new SafeObservableSubscription(); |
| 85 | + |
| 86 | + subscription.wrap(new Subscription() { |
| 87 | + @Override |
| 88 | + public void unsubscribe() { |
| 89 | + // on unsubscribe remove it from the map of outbound observers to notify |
| 90 | + state.observers.remove(subscription); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + // on subscribe add it to the map of outbound observers to notify |
| 95 | + state.observers.put(subscription, observer); |
| 96 | + |
| 97 | + return subscription; |
75 | 98 | }
|
76 |
| - }); |
| 99 | + } finally { |
| 100 | + state.SUBSCRIPTION_LOCK.unlock(); |
| 101 | + } |
77 | 102 |
|
78 |
| - // on subscribe add it to the map of outbound observers to notify |
79 |
| - observers.put(subscription, observer); |
80 |
| - return subscription; |
81 | 103 | }
|
| 104 | + |
82 | 105 | };
|
83 | 106 |
|
84 |
| - return new AsyncSubject<T>(onSubscribe, observers); |
| 107 | + return new AsyncSubject<T>(onSubscribe, state); |
85 | 108 | }
|
86 | 109 |
|
87 |
| - private final ConcurrentHashMap<Subscription, Observer<? super T>> observers; |
88 |
| - private final AtomicReference<T> currentValue; |
89 |
| - private final AtomicBoolean hasValue = new AtomicBoolean(); |
| 110 | + private static <T> void emitNotificationToObserver(final AsyncSubjectState<T> state, Observer<? super T> observer) { |
| 111 | + Notification<T> finalValue = state.currentValue.get(); |
| 112 | + |
| 113 | + // if null that means onNext was never invoked (no Notification set) |
| 114 | + if (finalValue != null) { |
| 115 | + if (finalValue.isOnNext()) { |
| 116 | + observer.onNext(finalValue.getValue()); |
| 117 | + } else if (finalValue.isOnError()) { |
| 118 | + observer.onError(finalValue.getThrowable()); |
| 119 | + } |
| 120 | + } |
| 121 | + observer.onCompleted(); |
| 122 | + } |
90 | 123 |
|
91 |
| - protected AsyncSubject(OnSubscribeFunc<T> onSubscribe, ConcurrentHashMap<Subscription, Observer<? super T>> observers) { |
| 124 | + /** |
| 125 | + * State externally constructed and passed in so the onSubscribe function has access to it. |
| 126 | + * |
| 127 | + * @param <T> |
| 128 | + */ |
| 129 | + private static class AsyncSubjectState<T> { |
| 130 | + private final ConcurrentHashMap<Subscription, Observer<? super T>> observers = new ConcurrentHashMap<Subscription, Observer<? super T>>(); |
| 131 | + private final AtomicReference<Notification<T>> currentValue = new AtomicReference<Notification<T>>(); |
| 132 | + private final AtomicBoolean completed = new AtomicBoolean(); |
| 133 | + private final ReentrantLock SUBSCRIPTION_LOCK = new ReentrantLock(); |
| 134 | + } |
| 135 | + |
| 136 | + private final AsyncSubjectState<T> state; |
| 137 | + |
| 138 | + protected AsyncSubject(OnSubscribeFunc<T> onSubscribe, AsyncSubjectState<T> state) { |
92 | 139 | super(onSubscribe);
|
93 |
| - this.observers = observers; |
94 |
| - this.currentValue = new AtomicReference<T>(); |
| 140 | + this.state = state; |
95 | 141 | }
|
96 | 142 |
|
97 | 143 | @Override
|
98 | 144 | public void onCompleted() {
|
99 |
| - T finalValue = currentValue.get(); |
100 |
| - for (Observer<? super T> observer : observers.values()) { |
101 |
| - if (hasValue.get()) { |
102 |
| - observer.onNext(finalValue); |
103 |
| - } |
104 |
| - observer.onCompleted(); |
105 |
| - } |
| 145 | + terminalState(); |
106 | 146 | }
|
107 | 147 |
|
108 | 148 | @Override
|
109 | 149 | public void onError(Throwable e) {
|
110 |
| - for (Observer<? super T> observer : observers.values()) { |
111 |
| - observer.onError(e); |
112 |
| - } |
| 150 | + state.currentValue.set(new Notification<T>(e)); |
| 151 | + terminalState(); |
113 | 152 | }
|
114 | 153 |
|
115 | 154 | @Override
|
116 |
| - public void onNext(T args) { |
117 |
| - hasValue.set(true); |
118 |
| - currentValue.set(args); |
| 155 | + public void onNext(T v) { |
| 156 | + state.currentValue.set(new Notification<T>(v)); |
| 157 | + } |
| 158 | + |
| 159 | + private void terminalState() { |
| 160 | + /* |
| 161 | + * We can not allow new subscribers to be added while we execute the terminal state. |
| 162 | + */ |
| 163 | + state.SUBSCRIPTION_LOCK.lock(); |
| 164 | + try { |
| 165 | + if (state.completed.compareAndSet(false, true)) { |
| 166 | + for (Subscription s : state.observers.keySet()) { |
| 167 | + // emit notifications to this observer |
| 168 | + emitNotificationToObserver(state, state.observers.get(s)); |
| 169 | + // remove the subscription as it is completed |
| 170 | + state.observers.remove(s); |
| 171 | + } |
| 172 | + } |
| 173 | + } finally { |
| 174 | + state.SUBSCRIPTION_LOCK.unlock(); |
| 175 | + } |
119 | 176 | }
|
120 | 177 | }
|
0 commit comments