Git Product home page Git Product logo

kotlin-redux's Introduction

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.

kotlin-redux's People

Contributors

angien avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.