{"id":15040926,"url":"https://github.com/taptappub/reaction","last_synced_at":"2025-04-14T19:33:42.296Z","repository":{"id":47467831,"uuid":"340169297","full_name":"taptappub/Reaction","owner":"taptappub","description":"A class that encapsulates a successful result with a value of type [T] or a failure result with an [Throwable] exception","archived":false,"fork":false,"pushed_at":"2022-05-27T19:17:09.000Z","size":168,"stargazers_count":21,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T07:51:14.345Z","etag":null,"topics":["android","coroutines","kotlin","reaction","result"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taptappub.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-18T20:27:44.000Z","updated_at":"2025-01-12T11:40:45.000Z","dependencies_parsed_at":"2022-08-26T01:40:38.496Z","dependency_job_id":null,"html_url":"https://github.com/taptappub/Reaction","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taptappub%2FReaction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taptappub%2FReaction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taptappub%2FReaction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taptappub%2FReaction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taptappub","download_url":"https://codeload.github.com/taptappub/Reaction/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248946326,"owners_count":21187487,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","coroutines","kotlin","reaction","result"],"created_at":"2024-09-24T20:45:17.562Z","updated_at":"2025-04-14T19:33:42.280Z","avatar_url":"https://github.com/taptappub.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reaction lib [![Maven Central](https://img.shields.io/maven-central/v/io.github.taptappub/reaction.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.github.taptappub%22%20AND%20a:%22reaction%22)\nA class that encapsulates a successful result with a value of type [T] or a failure result with an [Throwable] exception\n\n## Add library to a project\n\n```groovy\nallprojects {\n    repositories {\n        google()\n        mavenCentral()\n    }\n}\n\ndependencies {\n    implementation \"io.github.taptappub:reaction:$version\"\n}\n```\n\n## Samples\n\n```kotlin\nclass MainRepository {\n    fun getData(): Reaction\u003cString\u003e = Reaction.on { \"some calculations\" }\n    fun getAnotherData(firstData: String): Reaction\u003cString\u003e = Reaction.on { \"some another calculations based on $firstData\" }\n}\n```\n\n```kotlin\nfun getData() {\n    viewModelScope.launch(Dispatchers.IO) {\n        repository.getData()\n            .map { \"Convert to another string\" }\n            .doOnError { Log.d(\"LOG\", \"it is an error\") }\n            .fold(\n                success = {\n                    State.Success(it)\n                },\n                error = {\n                    State.Error\n                }\n            )\n            .let { liveData.postValue(it) }\n    }\n}\n```\n\n```kotlin\nfun getAnotherData() {\n    viewModelScope.launch(Dispatchers.IO) {\n        val data = repository.getData()\n            .check { it.isNotEmpty() }\n            .flatMap { Reaction.of { \"Flatmapped data\" } }\n            .takeOrReturn {\n                Log.d(\"LOG\", \"it is an error again\")\n                return@launch\n            }\n        repository.getAnotherData(data)\n            .handle(\n                success = { liveData.postValue(State.Success(it)) },\n                error = { Log.d(\"LOG\", \"Error too\") }\n            )\n    }\n}\n```\n\n## List of methods\n - **on** - Construct a safe Reaction from statement \n```kotlin\nReaction.on { \"something\" }\n```\n- **onCondition** - Construct a Reaction from condition\n```kotlin\nReaction.onCondition { it == \"what you want\" }\n```\n - **tryReaction** - Construct a safe Reaction from Reaction \n```kotlin\nReaction.tryReaction { Reaction.on { \"something\" } }\n```\n - **map** - Transform the success result by applying a function to it\n```kotlin\nrepository.getData()\n    .map { \"Convert to another string\" }\n```\n- **mapReaction** - Transform the result with success and error data by applying a function to it\n```kotlin\nrepository.getData()\n    .mapReaction { s, e -\u003e \"Convert to another string: $s + $e\" }\n```\n- **flatMap** - Transform the success result by applying a function to it to another Reaction\n```kotlin\nrepository.getData()\n    .flatMap { Reaction.of { \"Flatmapped data\" } }\n```\n- **errorMap** - Transform the error result by applying a function to it\n```kotlin\nrepository.getData()\n    .errorMap { IllegalStateException(\"something went wrong\") }\n```\n- **recover** - Transform the error result by applying a function to it to another Reaction\n```kotlin\nrepository.getData()\n    .recover { \"New reaction, much better then old\" }\n```\n- **doOnSuccess** - Register an action to take when Reaction is Success\n```kotlin\nrepository.getData()\n    .doOnSuccess { Log.d(\"Success! Let's dance!\") }\n```\n- **doOnError** - Register an action to take when Reaction is Error\n```kotlin\nrepository.getData()\n    .doOnError { Log.d(\"Error! Let's dance but sadly =(\") }\n```\n- **doOnComplete** - Register an action to take when Reaction is nevermind\n```kotlin\nrepository.getData()\n    .doOnComplete { Log.d(\"Let's dance in any case!\") }\n```\n- **handle** - Handle the Reaction result with on success and on error actions\n```kotlin\nrepository.getData(data)\n    .handle(\n          success = { liveData.postValue(it) },\n          error = { Log.d(\"LOG\", \"Error. That's a shame\") }\n    )\n```\n- **flatHandle** - Handle the Reaction result with one action with success and error\n```kotlin\nrepository.getData(data)\n    .flatHandle { success, error -\u003e\n      Log.d(\"LOG\", \"Let's combine results ${success.toString() + error.toString()}\")\n    }\n```\n- **fold** - Handle the Reaction result with on success and on error actions and transform them to the new object\n```kotlin\nrepository.getData()\n    .fold(\n        success = { State.Success(it) },\n        error = { State.Error }\n    )\n```\n- **check** - Check the success result by a function\n```kotlin\nrepository.getData()\n    .check { it.isNotEmpty() }\n```\n- **takeOrReturn** - Unwrap and receive the success result data or do a function with *return*\n```kotlin\nval data = repository.getData()\n    .takeOrReturn {\n        Log.d(\"LOG\", \"it is an error again\")\n        return\n    }\n```\n- **takeOrDefault** - Unwrap and receive the success result data or receive the default value in error case\n```kotlin\nval data = repository.getData()\n    .takeOrDefault {\n        \"default data\"\n    }\n```\n- **takeOrThrow** - Unwrap and receive the success result data or throw exception from Error in error case\n```kotlin\nval data = repository.getData()\n    .takeOrThrow()\n```\n- **isSuccess** - Check is result success\n```kotlin\nval isSuccess = repository.getData()\n   .isSuccess()\n```\n- **isError** - Check is result error\n```kotlin\nval isError = repository.getData()\n   .isError()\n```\n- **resumeWithReactionSuccess** - Coroutine Continuation for success callback\n```kotlin\noverride fun success() {\n    continuation.resumeWithReactionSuccess { true }\n}\n```\n- **resumeWithReactionError** - Coroutine Continuation for failure callback\n```kotlin\noverride fun failure(error: Throwable?) {\n    continuation.resumeWithReactionError { error }\n}\n```\n# License\n\n   Copyright 2021 Aleksey Potapov\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaptappub%2Freaction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaptappub%2Freaction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaptappub%2Freaction/lists"}