Kotlin Redux

A simplified implementation of Redux as a Kotlin library.

License

License

Categories

Categories

Kotlin Languages
GroupId

GroupId

com.workday
ArtifactId

ArtifactId

kotlin-redux
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Kotlin Redux
A simplified implementation of Redux as a Kotlin library.
Project URL

Project URL

https://github.com/Workday/kotlin-redux
Source Code Management

Source Code Management

https://github.com/Workday/kotlin-redux

Download kotlin-redux

How to add to project

<!-- https://jarcasting.com/artifacts/com.workday/kotlin-redux/ -->
<dependency>
    <groupId>com.workday</groupId>
    <artifactId>kotlin-redux</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.workday/kotlin-redux/
implementation 'com.workday:kotlin-redux:1.0.0'
// https://jarcasting.com/artifacts/com.workday/kotlin-redux/
implementation ("com.workday:kotlin-redux:1.0.0")
'com.workday:kotlin-redux:jar:1.0.0'
<dependency org="com.workday" name="kotlin-redux" rev="1.0.0">
  <artifact name="kotlin-redux" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.workday', module='kotlin-redux', version='1.0.0')
)
libraryDependencies += "com.workday" % "kotlin-redux" % "1.0.0"
[com.workday/kotlin-redux "1.0.0"]

Dependencies

runtime (1)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.10

test (2)

Group / Artifact Type Version
junit : junit jar 4.13
com.nhaarman.mockitokotlin2 : mockito-kotlin jar 2.2.0

Project Modules

There are no modules declared in this project.

kotlin-redux

Build Status

Maven

This library is a simplified implementation of Redux written in Kotlin for Android.

Redux is an architecture that was originally created in JavaScript: https://redux.js.org/. It follows a strict, unidirectional flow of data, and makes state predictable. It achieves this by ensuring the state is the single source of truth and immutable, and that changes to the state are done through pure functions.

Diagram of Redux

Installation

Add the following lines to your build.gradle file, replacing $kotlin_redux_version with latest version from Maven Central.

repositories {
    mavenCentral()
}

dependencies {
    compile "com.workday:kotlin-redux:$kotlin_redux_version"
}

Usage

To create a state:

data class ExampleState(
    ...
) : State

You can have many reducers, but it is best practice to combine them into one. To create a reducer:

class ExampleReducer : Reducer<State, Action> {
    override fun invoke(currentState: State, newAction: Action): MainState {
        return currentState.copy(...)
    }
}

To create a single threaded store, use the following initialization but with your initial state, reducer, and middleware(s):

val store = SingleThreadedStore<MainState, Action>(
  state = ExampleState(...),
  reducer = ExampleReducer(),
  middleware = listOf(ExampleMiddleware(), AnotherMiddleware())
)

To subscribe to the store:

val storeUnsubscriber = store.subscribe { currentState, dispatch ->
  ...
}

Ensure that you also unsubscribe from the store by calling:

storeUnsubscriber.invoke()

Avoid dispatching multiple times in a row as per Redux best practices. To dispatch actions, use the following with an action that implements the Action interface:

dispatch(ExampleAction)

Sample

See the sample app in the sample module for additional reference. The sample app is a simple counter, which shows a count and allows the user to increment and decrement the count.

com.workday

Workday

Versions

Version
1.0.0