-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OperatorMulticast.connect(connection) should not return null #2779
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
382d6df
OperatorMulticast.connect(connection) should return first subscriptio…
davidmoten 3b6707a
fix typo in comment
davidmoten a85a11e
revert visibility of fields and add == this check for guarded subscri…
davidmoten 379f07d
shouldSubscribe boolean variable not required
davidmoten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,14 +38,16 @@ | |
* the result value type | ||
*/ | ||
public final class OperatorMulticast<T, R> extends ConnectableObservable<R> { | ||
final Observable<? extends T> source; | ||
final Object guard; | ||
final Func0<? extends Subject<? super T, ? extends R>> subjectFactory; | ||
private final Observable<? extends T> source; | ||
private final Object guard; | ||
private final Func0<? extends Subject<? super T, ? extends R>> subjectFactory; | ||
private final AtomicReference<Subject<? super T, ? extends R>> connectedSubject; | ||
private final List<Subscriber<? super R>> waitingForConnect; | ||
|
||
/** Guarded by guard. */ | ||
Subscriber<T> subscription; | ||
private Subscriber<T> subscription; | ||
// wraps subscription above for unsubscription using guard | ||
private Subscription guardedSubscription; | ||
|
||
public OperatorMulticast(Observable<? extends T> source, final Func0<? extends Subject<? super T, ? extends R>> subjectFactory) { | ||
this(new Object(), new AtomicReference<Subject<? super T, ? extends R>>(), new ArrayList<Subscriber<? super R>>(), source, subjectFactory); | ||
|
@@ -82,7 +84,8 @@ public void connect(Action1<? super Subscription> connection) { | |
// subscription is the state of whether we are connected or not | ||
synchronized (guard) { | ||
if (subscription != null) { | ||
// already connected, return as there is nothing to do | ||
// already connected | ||
connection.call(guardedSubscription); | ||
return; | ||
} else { | ||
shouldSubscribe = true; | ||
|
@@ -106,6 +109,21 @@ public void onNext(T args) { | |
subject.onNext(args); | ||
} | ||
}; | ||
guardedSubscription = Subscriptions.create(new Action0() { | ||
@Override | ||
public void call() { | ||
Subscription s; | ||
synchronized (guard) { | ||
s = subscription; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd check if Subscription s1 = src.connect();
Subscription s2 = src.connect();
s1.unsubscribe();
Subscription s3 = src.connect();
s2.unsubscribe();
// src should be still connected |
||
subscription = null; | ||
guardedSubscription = null; | ||
connectedSubject.set(null); | ||
} | ||
if (s != null) { | ||
s.unsubscribe(); | ||
} | ||
} | ||
}); | ||
|
||
// register any subscribers that are waiting with this new subject | ||
for(Subscriber<? super R> s : waitingForConnect) { | ||
|
@@ -116,34 +134,22 @@ public void onNext(T args) { | |
// record the Subject so OnSubscribe can see it | ||
connectedSubject.set(subject); | ||
} | ||
|
||
} | ||
|
||
// in the lock above we determined we should subscribe, do it now outside the lock | ||
if (shouldSubscribe) { | ||
// register a subscription that will shut this down | ||
connection.call(Subscriptions.create(new Action0() { | ||
@Override | ||
public void call() { | ||
Subscription s; | ||
synchronized (guard) { | ||
s = subscription; | ||
subscription = null; | ||
connectedSubject.set(null); | ||
} | ||
if (s != null) { | ||
s.unsubscribe(); | ||
} | ||
} | ||
})); | ||
connection.call(guardedSubscription); | ||
|
||
// now that everything is hooked up let's subscribe | ||
// as long as the subscription is not null (which can happen if already unsubscribed) | ||
boolean subscriptionIsNull; | ||
synchronized(guard) { | ||
subscriptionIsNull = subscription == null; | ||
Subscriber<T> sub; | ||
synchronized (guard) { | ||
sub = subscription; | ||
} | ||
if (!subscriptionIsNull) | ||
source.subscribe(subscription); | ||
if (sub != null) | ||
source.subscribe(sub); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I try to avoid private because the compiler generates accessor methods which slows down the interpreter phase and makes an extra hop while debugging methods.