All Projects → mercari → RemoteDataK

mercari / RemoteDataK

Licence: MIT license
Algebraic data type (ADT) to represent the state of data that is loading from/to remote sources/destinations

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to RemoteDataK

remotedata-re
Tools for fetching data from remote sources in Reason
Stars: ✭ 32 (-27.27%)
Mutual labels:  adt, remotedata
Remote Data Ts
RemoteData type
Stars: ✭ 185 (+320.45%)
Mutual labels:  algebraic-data-types
Rings
Rings: efficient JVM library for polynomial rings
Stars: ✭ 50 (+13.64%)
Mutual labels:  algebraic-data-types
Adt
Algebraic data types for Python (experimental, not actively maintained)
Stars: ✭ 120 (+172.73%)
Mutual labels:  algebraic-data-types
Mach7
Functional programming style pattern-matching library for C++
Stars: ✭ 1,151 (+2515.91%)
Mutual labels:  algebraic-data-types
Plate
Principled schema system for JSON. Work in progress.
Stars: ✭ 130 (+195.45%)
Mutual labels:  algebraic-data-types
Witchcraft
Monads and other dark magic for Elixir
Stars: ✭ 864 (+1863.64%)
Mutual labels:  algebraic-data-types
Poica
🧮 A research programming language on top of C macros
Stars: ✭ 231 (+425%)
Mutual labels:  algebraic-data-types
Fluture
🦋 Fantasy Land compliant (monadic) alternative to Promises
Stars: ✭ 2,249 (+5011.36%)
Mutual labels:  algebraic-data-types
Datum
pure functional and generic programming for Erlang
Stars: ✭ 111 (+152.27%)
Mutual labels:  algebraic-data-types
Functionaljava
Functional programming in Java
Stars: ✭ 1,472 (+3245.45%)
Mutual labels:  algebraic-data-types
Ganja.js
📐 Javascript Geometric Algebra Generator for Javascript, c++, c#, rust, python. (with operator overloading and algebraic literals) -
Stars: ✭ 1,179 (+2579.55%)
Mutual labels:  algebraic-data-types
Expat
Reusable, composable patterns across Elixir libraries
Stars: ✭ 157 (+256.82%)
Mutual labels:  algebraic-data-types
Momi
Monadic middleware
Stars: ✭ 57 (+29.55%)
Mutual labels:  algebraic-data-types
Swift Enum Properties
🤝 Struct and enum data access in harmony.
Stars: ✭ 191 (+334.09%)
Mutual labels:  algebraic-data-types
Enum Fp
Functional Enum type / Sum type for javascript with simple pattern matching
Stars: ✭ 27 (-38.64%)
Mutual labels:  algebraic-data-types
Dataenum
Algebraic data types in Java.
Stars: ✭ 128 (+190.91%)
Mutual labels:  algebraic-data-types
Cl Algebraic Data Type
Algebraic data types in Common Lisp
Stars: ✭ 86 (+95.45%)
Mutual labels:  algebraic-data-types
Mlstyle.jl
Julia functional programming infrastructures and metaprogramming facilities
Stars: ✭ 223 (+406.82%)
Mutual labels:  algebraic-data-types
Folktale
[not actively maintained!] A standard library for functional programming in JavaScript
Stars: ✭ 1,995 (+4434.09%)
Mutual labels:  algebraic-data-types

RemoteData for Kotlin

jcenter Build Status

Algebraic data type (ADT) to represent the state of data that is loading from/to remote sources/destinations

Setup

dependencies {
  repositories {
    jcenter()
  }
}

implementation("com.mercari.remotedata:remotedata:<latest-version>")

About

RemoteData is useful to represent the state of data, when it is loading from/to a remote source/destination.

Using RemoteData is pretty straightforward, it is however meant to be used in a functional style.

RemoteData works nicely with RxJava and RxRedux, however it can be used independently.

This is done through 4 types: Initial, Loading, Success and Failure.

  • Initial represents the initial state of the data before any progress has been made.

  • Loading represents the intermediary loading state of data.

  • Success represents the state where the loading is completed, and holds the resulting value of the data.

  • Failure represents the state where the loading failed, and holds information about the reason of failure with an Exception.

In cases where the data size is known, you may find the properties progress and totalUnits of the Loading type useful. The value of progress is always between 0 and totalUnits (default of 100 for percentages).

Initial and Loading are Incomplete, whereas Success and Failure are Complete.

Usage

A common use case for RemoteData would be mapping it into a UI transition or component state.

For example, when making a network request to fetch data, most UIs have a progress indicator and finally display the result.

  • Declare your data where you need it, where V is the type of data value and E the type of Exception/Error.
var data: RemoteData<V, E> = Initial
  • Once the data starts to load, transition to Loading with the optional setting of a progress and totalUnits.
data = Loading()

If progress is used, simply update it when required.

data.progress = data.progress?.let { it + delta }
  • Once the loading is complete successfully, transition to Success along with the actual data.
data = Success(value)
  • Otherwise, transition to Failure with a proper Exception.
data = Failure(Exception("error"))
  • Behaviour based on the data state can then be mapped neatly, wherever the data is processed.
when (data) {
    is Initial -> initialize()
    is Loading -> progress(data.progress)
    is Success -> success(data.value) 
    is Failure -> failure(data.error)
 }

Or with the other conveniences:

when {
    data.isIncomplete -> showProgress()
    data.isComplete -> hideProgress()
 }

Higher oder functions

A few higher order functions are also provided for convenience such as:

  • map
  • mapError
  • mapBoth
  • getOrElse
  • flatMap
  • flatMapError
  • fanout

For examples, take a look at some of the comprehensive tests.

Contribution

Please read the CLA below carefully before submitting your contribution.

https://www.mercari.com/cla/

License

Copyright 2018-2019 Mercari, Inc.

Licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].