File tree Expand file tree Collapse file tree
rxjava-core/src/main/java/rx/subscriptions Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919
2020import rx .Observable ;
2121import rx .Subscription ;
22+ import rx .util .functions .Action0 ;
2223
2324/**
2425 * Subscription that can be checked for status such as in a loop inside an {@link Observable} to exit the loop if unsubscribed.
2829public class BooleanSubscription implements Subscription {
2930
3031 private final AtomicBoolean unsubscribed = new AtomicBoolean (false );
32+ private final Action0 action ;
33+
34+ public BooleanSubscription () {
35+ action = null ;
36+ }
37+
38+ private BooleanSubscription (Action0 action ) {
39+ this .action = action ;
40+ }
41+
42+ public static BooleanSubscription create () {
43+ return new BooleanSubscription ();
44+ }
45+
46+ public static BooleanSubscription create (Action0 onUnsubscribe ) {
47+ return new BooleanSubscription (onUnsubscribe );
48+ }
3149
3250 public boolean isUnsubscribed () {
3351 return unsubscribed .get ();
3452 }
3553
3654 @ Override
37- public void unsubscribe () {
38- unsubscribed .set (true );
55+ public final void unsubscribe () {
56+ if (unsubscribed .compareAndSet (false , true )) {
57+ if (action != null ) {
58+ action .call ();
59+ }
60+ }
3961 }
4062
4163}
You can’t perform that action at this time.
0 commit comments