Skip to content
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

3.x: Add subscribe with disposable container #7298

Merged
merged 1 commit into from
Jul 17, 2021

Conversation

akarnokd
Copy link
Member

Purpose

With the existing lambda-subscribe methods, the returned Disposable is often added to a CompositeDisposable but generally is not removed right when the source terminates but when the parent context calls clear() on the composite. This can lead to unintended retention of resources captured by the lambdas as well as the composite growing and growing with now dead consumers.

These new overloads will interact with the container by adding themselves and removing themselves during the consumer's lifecycle.

Resolves #7295

Description

Wraps the given onXXX callbacks into a Disposable Subscriber/Observer/etc.,
adds it to the given DisposableContainer (such as CompositeDisposable) and ensures, that if the upstream
terminates or this particular Disposable is disposed, the Subscriber/Observer/etc. is removed
from the given composite.

The Subscriber/Observer/etc. will be removed after the callback for the terminal event has been invoked.

Example

CompositeDisposable composite = new CompositeDisposable();

Disposable d = Flowable.just(1).subscribe(
    System.out::println, 
     Throwable::printStackTrace, 
     () -> System.out.println("Done"),
     composite
);

assertEquals(0, composite.size());

// --------------------------

Disposable d2 = Flowable.never().subscribe(
    System.out::println, 
    Throwable::printStackTrace, 
    () -> System.out.println("Done"),
    composite
);

assertEquals(1, composite.size());

d2.dispose();

assertEquals(0, composite.size());

Credits

This code was uplifted and adapted from the RxJavaExtensions project of mine: https://github.com/akarnokd/RxJavaExtensions#subscribeautodispose

@akarnokd akarnokd added this to the 3.1 milestone Jul 17, 2021
@codecov
Copy link

codecov bot commented Jul 17, 2021

Codecov Report

Merging #7298 (1f0d3ae) into 3.x (a9e0a8a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##                3.x    #7298    +/-   ##
==========================================
  Coverage     99.53%   99.54%            
- Complexity     6744     6783    +39     
==========================================
  Files           747      751     +4     
  Lines         47347    47489   +142     
  Branches       6376     6378     +2     
==========================================
+ Hits          47129    47275   +146     
+ Misses           98       95     -3     
+ Partials        120      119     -1     
Impacted Files Coverage Δ
...in/java/io/reactivex/rxjava3/core/Completable.java 100.00% <100.00%> (ø)
.../main/java/io/reactivex/rxjava3/core/Flowable.java 100.00% <100.00%> (ø)
src/main/java/io/reactivex/rxjava3/core/Maybe.java 100.00% <100.00%> (ø)
...ain/java/io/reactivex/rxjava3/core/Observable.java 100.00% <100.00%> (ø)
...rc/main/java/io/reactivex/rxjava3/core/Single.java 100.00% <100.00%> (ø)
...ernal/observers/AbstractDisposableAutoRelease.java 100.00% <100.00%> (ø)
.../observers/DisposableAutoReleaseMultiObserver.java 100.00% <100.00%> (ø)
...ernal/observers/DisposableAutoReleaseObserver.java 100.00% <100.00%> (ø)
...l/subscribers/DisposableAutoReleaseSubscriber.java 100.00% <100.00%> (ø)
...l/operators/observable/ObservableFlatMapMaybe.java 91.54% <0.00%> (-3.53%) ⬇️
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a9e0a8a...1f0d3ae. Read the comment docs.

@akarnokd akarnokd merged commit 66fd701 into ReactiveX:3.x Jul 17, 2021
@akarnokd akarnokd deleted the subscribeComposite3x branch July 17, 2021 10:54
sakibguy added a commit to sakibguy/RxJava that referenced this pull request Jul 18, 2021
3.x: Add subscribe with disposable container (ReactiveX#7298)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2.x/3.x: Consumers leaked longer than they have to
2 participants