Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pakoito/RxComprehensions
Reduce boilerplate in RxJava by abstracting chained operators like flatMap, concatMap, switchMap, or compose [STABLE]
https://github.com/pakoito/RxComprehensions
Last synced: 24 days ago
JSON representation
Reduce boilerplate in RxJava by abstracting chained operators like flatMap, concatMap, switchMap, or compose [STABLE]
- Host: GitHub
- URL: https://github.com/pakoito/RxComprehensions
- Owner: pakoito
- License: other
- Created: 2016-08-29T17:54:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-24T12:44:23.000Z (almost 8 years ago)
- Last Synced: 2024-11-15T23:50:45.678Z (26 days ago)
- Language: Java
- Homepage:
- Size: 102 KB
- Stars: 85
- Watchers: 7
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-rxjava - RxComprehensions - Reduce boilerplate in RxJava by abstracting chained flatMaps, concatMaps and switchMaps. (Utilities)
README
# RxComprehensions
RxComprehensions is a library to reduce boilerplate and simplify your `Observable` chains.
## Rationale
As your code starts getting more and more functional, you find that you have to chain multiple `Observable` by means of `flatMap()`, `concatMap()`, or `switchMap()`. This causes indentation levels to go quite high, and would often require that you split the code in several methods just to keep it readable.
```java
Observable getUserFriends =
profileClicks()
.flatMap { position ->
getUserFromProfile(position)
.flatMap { user ->
requestFriendListForUser(position, user.id)
.flatMap { friends ->
storeUserAndFriends(user, friends)
.flatMap { result ->
toUserDisplayString(position, user, friends, result)
}
}
}
}
```**Comprehensions** are a language feature that allow you to define such a chain in a way where every observable is a function at topmost indentation, yet still contains all the parameters received in the previous functions.
## Usage
### Map comprehensions
RxComprehensions contains static methods `doFlatMap()` for `flatMap()`, `doConcatMap()` for `concatMap()`, `doSwitchMap()` for `switchMap()`. Each takes from 1 to 9 `FuncN` each with an increasing number of parameters, and returns an `Observable` of the type of the return of the last function.
```java
Observable getUserFriends =
// chained with flatMap()
RxComprehensions.doFlatMap(
() -> profileClicks(),
position -> getUserFromProfile(position),
position, user -> requestFriendListForUser(position, user.id),
position, user, friends -> storeUserAndFriends(user, friends),
position, user, friends, result -> toUserDisplayString(position, user, friends, result)
);
```### Compose comprehensions
RxComprehensions contains static methods `doCompose()` for `compose()`. Each takes from 1 to 9 `Transformer` (RxJava 1.X) or `ObservableTransformer` (RxJava 2.X), and returns an `Observable` of the type of the return of the last one.
```java
Observable> getRelatives =
// chained with compose()
RxComprehensions.doCompose(
() -> requestRelative("12345"),
validate(),
assureThreads(Schedulers.io(), AndroidSchedulers.main()),
respectLifecycle(activity),
toUILayerModel(),
groupSiblings()
);Observable requestRelative(String id) { /* ... */ }
ObservableTransformer validate() { /* ... */ }
ObservableTransformer assureThreads(Scheduler in, Scheduler out) { /* ... */ }
ObservableTransformer respectLifecycle(Activity activity) { /* ... */ }
ObservableTransformer toUILayerModel() { /* ... */ }
ObservableTransformer> groupSiblings() { /* ... */ }
```## Distribution
### RxJava 1.X
Add as a dependency to your `build.gradle`
```groovy
repositories {
...
maven { url "https://jitpack.io" }
...
}
dependencies {
...
compile 'com.github.pakoito.RxComprehensions:rxcomprehensions:1.3.0'
...
}
```
or to your `pom.xml````xml
jitpack.io
https://jitpack.io
com.github.pakoito.RxComprehensions
rxcomprehensions
1.3.0```
### RxJava 2.X
Add as a dependency to your `build.gradle`
```groovy
repositories {
...
maven { url "https://jitpack.io" }
...
}
dependencies {
...
compile 'com.github.pakoito.RxComprehensions:rxcomprehensions2:1.3.0'
...
}
```
or to your `pom.xml````xml
jitpack.io
https://jitpack.io
com.github.pakoito.RxComprehensions
rxcomprehensions2
1.3.0```
## License
Copyright (c) pakoito 2016
The Apache Software License, Version 2.0
See LICENSE.md