Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sellmair/disposer
Easily dispose rxJava streams with Android's Lifecycle
https://github.com/sellmair/disposer
android disposable kotlin lifecycle rxjava rxjava2 rxjava3 rxlifecycle
Last synced: 12 days ago
JSON representation
Easily dispose rxJava streams with Android's Lifecycle
- Host: GitHub
- URL: https://github.com/sellmair/disposer
- Owner: sellmair
- License: mit
- Created: 2018-09-29T12:03:40.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-11-17T20:29:33.000Z (almost 4 years ago)
- Last Synced: 2023-11-07T19:17:25.989Z (about 1 year ago)
- Topics: android, disposable, kotlin, lifecycle, rxjava, rxjava2, rxjava3, rxlifecycle
- Language: Kotlin
- Homepage:
- Size: 173 KB
- Stars: 174
- Watchers: 6
- Forks: 11
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## Disposer
Easily dispose RxJava streams with Android's Lifecycle![GitHub top language](https://img.shields.io/github/languages/top/sellmair/disposer.svg)
[![Build Status](https://travis-ci.org/sellmair/disposer.svg?branch=master)](https://travis-ci.org/sellmair/disposer)
![Bintray](https://img.shields.io/bintray/v/sellmair/sellmair/disposer.svg)Checkout my [Medium](https://medium.com/@sellmair/disposing-on-android-the-right-way-97bd55cbf970) article.
### Usage
##### Gradle
```groovy
dependencies {
// Non AndroidX projects
implementation 'io.sellmair:disposer:1.1.0'
// AndroidX projects rxjava2
implementation 'io.sellmair:disposer:2.0.0'
// AndroidX projects rxjava3
implementation 'io.sellmair:disposer:3.0.0'
}
```
##### Disposer
A ```Disposer``` is the object managing multiple ```Disposable``` instances and disposes them
at the correct time.You can easily add a given disposable to a ```Disposer```:
```kotlin
val disposer: Disposer = /* ... */
val disposable = Service.queryAwesomeData().subscribe()
disposer.add(disposable) // The disposable will now managed by the disposer
```Or a much sweeter apis, that might look familiar to rxKotlin users:
```kotlin
val disposer: Disposer = /* ... */
disposer += Service.queryAwesomeData().subscribe() // Managed by the disposer
``````kotlin
val disposer: Disposer = /* ... */
Service.queryAwesomeData().subscribe().disposeBy(disposer) // Managed by the disposer
```
##### Get the correct ```Disposer```
RxLifecycle makes it easy to get a disposer for each lifecycle hook like
```onCreate```, ```onStart```, ```onResume```, ```onPause```, ```onStop``` and ```onDestroy```One can simple call the extension function:
```kotlin
val onStopDisposer: Disposer = lifecycle.disposers.onStop
```Much more shorter and convenient extensions are also offered, like for LifecycleOwner:
```kotlin
class MyCoolComponent: LifecycleOwner {
// ...
private val onStopDisposer: Disposer = this.onStop}
```Which leads to very concise and readable API's inside your ```Activity``` or ```Fragment``` classes:
##### Example:
```kotlin
class MyCoolFragment {
// awesome other code
fun onStart(){
super.onStart()
awesomDataProvider.query()
.flatMap(::pepareForAwesomeness)
.filter(::isAwesome)
.subscribe(::displayAwesomeData)
.disposeBy(onStop) // <--- Will automatically be disposed when onStop() is called.
}
}
```##### Advanced: Upstream dispose
It is also possible to put the ```.disposeBy``` call before the ```.subscribe```.
But be aware, that this will only dispose the upstream not the downstream, which is
often okay, but should only be used with caution!```kotlin
awesomDataProvider.query()
.flatMap(::pepareForAwesomeness)
.filter(::isAwesome)
.disposeBy(onStop) // <--- Will dispose everything above it when .onStop() is called
.subscribe(::displayAwesomeData)
```##### Create you own Disposer
You can easily create your own ```Disposer``` by calling
```kotlin
val disposer = Disposer.create()
```Each call of
```kotlin
disposer.dispose()
```Will dispose all currently managed disposables and reset the ```Disposer```
⚠️ Be aware: This behaviour differs from ```CompositeDisposable``` and actually
is more like ```CompositeDisposable.clear```.