Skip to content

save allocation in OnSubscribeAutoConnect #4233

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

Merged
merged 1 commit into from
Jul 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/main/java/rx/internal/operators/OnSubscribeAutoConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
*
* @param <T> the value type of the chain
*/
public final class OnSubscribeAutoConnect<T> implements OnSubscribe<T> {
final ConnectableObservable<? extends T> source;
@SuppressWarnings("serial")
public final class OnSubscribeAutoConnect<T> extends AtomicInteger implements OnSubscribe<T> {
// AtomicInteger aspect of `this` represents the number of clients

final ConnectableObservable<? extends T> source;
final int numberOfSubscribers;
final Action1<? super Subscription> connection;
final AtomicInteger clients;

public OnSubscribeAutoConnect(ConnectableObservable<? extends T> source,
int numberOfSubscribers,
Expand All @@ -44,12 +46,12 @@ public OnSubscribeAutoConnect(ConnectableObservable<? extends T> source,
this.source = source;
this.numberOfSubscribers = numberOfSubscribers;
this.connection = connection;
this.clients = new AtomicInteger();
}
@Override
public void call(Subscriber<? super T> child) {
source.unsafeSubscribe(Subscribers.wrap(child));
if (clients.incrementAndGet() == numberOfSubscribers) {
//this.get() represents the number of clients
if (this.incrementAndGet() == numberOfSubscribers) {
source.connect(connection);
}
}
Expand Down