Skip to content

taptappub/Reaction

Repository files navigation

Reaction lib Maven Central

A class that encapsulates a successful result with a value of type [T] or a failure result with an [Throwable] exception

Add library to a project

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

dependencies {
    implementation "io.github.taptappub:reaction:$version"
}

Samples

class MainRepository {
    fun getData(): Reaction<String> = Reaction.on { "some calculations" }
    fun getAnotherData(firstData: String): Reaction<String> = Reaction.on { "some another calculations based on $firstData" }
}
fun getData() {
    viewModelScope.launch(Dispatchers.IO) {
        repository.getData()
            .map { "Convert to another string" }
            .doOnError { Log.d("LOG", "it is an error") }
            .fold(
                success = {
                    State.Success(it)
                },
                error = {
                    State.Error
                }
            )
            .let { liveData.postValue(it) }
    }
}
fun getAnotherData() {
    viewModelScope.launch(Dispatchers.IO) {
        val data = repository.getData()
            .check { it.isNotEmpty() }
            .flatMap { Reaction.of { "Flatmapped data" } }
            .takeOrReturn {
                Log.d("LOG", "it is an error again")
                return@launch
            }
        repository.getAnotherData(data)
            .handle(
                success = { liveData.postValue(State.Success(it)) },
                error = { Log.d("LOG", "Error too") }
            )
    }
}

List of methods

  • on - Construct a safe Reaction from statement
Reaction.on { "something" }
  • onCondition - Construct a Reaction from condition
Reaction.onCondition { it == "what you want" }
  • tryReaction - Construct a safe Reaction from Reaction
Reaction.tryReaction { Reaction.on { "something" } }
  • map - Transform the success result by applying a function to it
repository.getData()
    .map { "Convert to another string" }
  • mapReaction - Transform the result with success and error data by applying a function to it
repository.getData()
    .mapReaction { s, e -> "Convert to another string: $s + $e" }
  • flatMap - Transform the success result by applying a function to it to another Reaction
repository.getData()
    .flatMap { Reaction.of { "Flatmapped data" } }
  • errorMap - Transform the error result by applying a function to it
repository.getData()
    .errorMap { IllegalStateException("something went wrong") }
  • recover - Transform the error result by applying a function to it to another Reaction
repository.getData()
    .recover { "New reaction, much better then old" }
  • doOnSuccess - Register an action to take when Reaction is Success
repository.getData()
    .doOnSuccess { Log.d("Success! Let's dance!") }
  • doOnError - Register an action to take when Reaction is Error
repository.getData()
    .doOnError { Log.d("Error! Let's dance but sadly =(") }
  • doOnComplete - Register an action to take when Reaction is nevermind
repository.getData()
    .doOnComplete { Log.d("Let's dance in any case!") }
  • handle - Handle the Reaction result with on success and on error actions
repository.getData(data)
    .handle(
          success = { liveData.postValue(it) },
          error = { Log.d("LOG", "Error. That's a shame") }
    )
  • flatHandle - Handle the Reaction result with one action with success and error
repository.getData(data)
    .flatHandle { success, error ->
      Log.d("LOG", "Let's combine results ${success.toString() + error.toString()}")
    }
  • fold - Handle the Reaction result with on success and on error actions and transform them to the new object
repository.getData()
    .fold(
        success = { State.Success(it) },
        error = { State.Error }
    )
  • check - Check the success result by a function
repository.getData()
    .check { it.isNotEmpty() }
  • takeOrReturn - Unwrap and receive the success result data or do a function with return
val data = repository.getData()
    .takeOrReturn {
        Log.d("LOG", "it is an error again")
        return
    }
  • takeOrDefault - Unwrap and receive the success result data or receive the default value in error case
val data = repository.getData()
    .takeOrDefault {
        "default data"
    }
  • takeOrThrow - Unwrap and receive the success result data or throw exception from Error in error case
val data = repository.getData()
    .takeOrThrow()
  • isSuccess - Check is result success
val isSuccess = repository.getData()
   .isSuccess()
  • isError - Check is result error
val isError = repository.getData()
   .isError()
  • resumeWithReactionSuccess - Coroutine Continuation for success callback
override fun success() {
    continuation.resumeWithReactionSuccess { true }
}
  • resumeWithReactionError - Coroutine Continuation for failure callback
override fun failure(error: Throwable?) {
    continuation.resumeWithReactionError { error }
}

License

Copyright 2021 Aleksey Potapov

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

A class that encapsulates a successful result with a value of type [T] or a failure result with an [Throwable] exception

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages