Skip to content

Commit e7fa614

Browse files
Merge pull request #1802 from simonbasle/hasObservers
add hasObservers method to Subjects (#1772)
2 parents 7924a3e + 8690b18 commit e7fa614

10 files changed

+55
-6
lines changed

src/main/java/rx/internal/operators/BufferUntilSubscriber.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,14 @@ public void onNext(T t) {
184184
emit(state.nl.next(t));
185185
}
186186
}
187-
187+
188+
@Override
189+
public boolean hasObservers() {
190+
synchronized (state.guard) {
191+
return state.observerRef != null;
192+
}
193+
}
194+
188195
@SuppressWarnings("rawtypes")
189196
private final static Observer EMPTY_OBSERVER = new Observer() {
190197

src/main/java/rx/internal/operators/OperatorReplay.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,9 @@ public void onCompleted() {
9999
subject.onCompleted();
100100
}
101101

102+
@Override
103+
public boolean hasObservers() {
104+
return this.subject.hasObservers();
105+
}
102106
}
103107
}

src/main/java/rx/subjects/AsyncSubject.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,8 @@ public void onNext(T v) {
136136
lastValue = nl.next(v);
137137
}
138138

139+
@Override
140+
public boolean hasObservers() {
141+
return state.observers().length > 0;
142+
}
139143
}

src/main/java/rx/subjects/BehaviorSubject.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,13 @@ public void onNext(T v) {
167167
}
168168
}
169169
}
170-
170+
171171
/* test support */ int subscriberCount() {
172172
return state.observers().length;
173173
}
174+
175+
@Override
176+
public boolean hasObservers() {
177+
return state.observers().length > 0;
178+
}
174179
}

src/main/java/rx/subjects/PublishSubject.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ public void onNext(T v) {
121121
bo.onNext(v);
122122
}
123123
}
124+
125+
@Override
126+
public boolean hasObservers() {
127+
return state.observers().length > 0;
128+
}
124129
}

src/main/java/rx/subjects/ReplaySubject.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,12 @@ public void onCompleted() {
347347
/* Support test. */int subscriberCount() {
348348
return ssm.state.observers.length;
349349
}
350-
350+
351+
@Override
352+
public boolean hasObservers() {
353+
return ssm.observers().length > 0;
354+
}
355+
351356
private boolean caughtUp(SubjectObserver<? super T> o) {
352357
if (!o.caughtUp) {
353358
o.caughtUp = true;

src/main/java/rx/subjects/SerializedSubject.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*/
3535
public class SerializedSubject<T, R> extends Subject<T, R> {
3636
private final SerializedObserver<T> observer;
37+
private final Subject<T, R> actual;
3738

3839
public SerializedSubject(final Subject<T, R> actual) {
3940
super(new OnSubscribe<R>() {
@@ -44,6 +45,7 @@ public void call(Subscriber<? super R> child) {
4445
}
4546

4647
});
48+
this.actual = actual;
4749
this.observer = new SerializedObserver<T>(actual);
4850
}
4951

@@ -62,4 +64,8 @@ public void onNext(T t) {
6264
observer.onNext(t);
6365
}
6466

67+
@Override
68+
public boolean hasObservers() {
69+
return actual.hasObservers();
70+
}
6571
}

src/main/java/rx/subjects/Subject.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ public abstract class Subject<T, R> extends Observable<R> implements Observer<T>
2525
protected Subject(OnSubscribe<R> onSubscribe) {
2626
super(onSubscribe);
2727
}
28+
29+
/**
30+
* Indicates whether the {@link Subject} has {@link Observer Observers} subscribed to it.
31+
* @return true if there is at least one Observer subscribed to this Subject, false otherwise
32+
*/
33+
public abstract boolean hasObservers();
2834
}

src/main/java/rx/subjects/TestSubject.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void _onCompleted() {
8686

8787
/**
8888
* Schedule a call to {@code onCompleted} relative to "now()" +n milliseconds in the future.
89-
*
89+
*
9090
* @param timeInMilliseconds
9191
* the number of milliseconds in the future relative to "now()" at which to call {@code onCompleted}
9292
*/
@@ -119,7 +119,7 @@ private void _onError(final Throwable e) {
119119

120120
/**
121121
* Schedule a call to {@code onError} relative to "now()" +n milliseconds in the future.
122-
*
122+
*
123123
* @param e
124124
* the {@code Throwable} to pass to the {@code onError} method
125125
* @param timeInMilliseconds
@@ -152,7 +152,7 @@ private void _onNext(T v) {
152152

153153
/**
154154
* Schedule a call to {@code onNext} relative to "now()" +n milliseconds in the future.
155-
*
155+
*
156156
* @param v
157157
* the item to emit
158158
* @param timeInMilliseconds
@@ -168,4 +168,9 @@ public void call() {
168168

169169
}, timeInMilliseconds, TimeUnit.MILLISECONDS);
170170
}
171+
172+
@Override
173+
public boolean hasObservers() {
174+
return state.observers().length > 0;
175+
}
171176
}

src/test/java/rx/subjects/BehaviorSubjectTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package rx.subjects;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertFalse;
1920
import static org.junit.Assert.fail;
2021
import static org.mockito.Matchers.any;
2122
import static org.mockito.Mockito.inOrder;
@@ -370,6 +371,7 @@ public void testTakeOneSubscriber() {
370371
verify(o, never()).onError(any(Throwable.class));
371372

372373
assertEquals(0, source.subscriberCount());
374+
assertFalse(source.hasObservers());
373375
}
374376

375377
@Test

0 commit comments

Comments
 (0)