GithubHelp home page GithubHelp logo

mercari / rxreduxk Goto Github PK

View Code? Open in Web Editor NEW
66.0 5.0 3.0 164 KB

Micro-framework for Redux implemented in Kotlin

License: MIT License

Kotlin 100.00%
redux kotlin android kotlin-android kotlin-library flux rx rxredux micro-framework reducer

rxreduxk's Introduction

RxRedux for Kotlin

jcenter Build Status

Micro-framework for Redux implemented in Kotlin

Installation

dependencies {
  repositories {
    jcenter()
  }
}

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

Usage

This framework is composed of several types/abstractions inspired by Redux that help to implement the reactive behavior of application components. It is based on RxJava for reactivity and works well with RemoteDataK.

State

State represents the model or state of your component or UI. A State is recommended to be immutable, however it can be allowed to be mutable.

This can typically be implemented by a data class

For example:

data class CounterState(
    val counter: Int
) : State

Action

An Action represents the desired modifications on a State, for example

class Increment : Action
class Decrement : Action

Although not required, it is recommended to model Actions as class hierarchy with a sealed class.

sealed class CounterAction : Action

class Increment : CounterAction()
class Decrement : CounterAction()

An Action can contain parameters that make them more useful depending on the desired behaviour. For example:

class Increment(val by: Int) : CounterAction

Actions are to be dispatched through the Store's dispatch method to perform State mutations.

For example:

store.dispatch(Increment(2))

Reducer

A Reducer is where the State is mutated or modified, depending on which Action is applied. It is basically a map of the desired modifications and their effects.

For example:

class CounterReducer: Reducer<CounterState, CounterAction> {
    override fun reduce(currentState: CounterState, action: CounterAction) : CounterState =
        when(action) {
          is Increment -> CounterState(counter: currenState.counter + action.by)
          is Decrement -> CounterState(counter: currenState.counter - action.by)
        }
}

Middleware

Middleware allows for a variety of behaviours that are not directly related to the component's State. This is useful for the implementation of so called cross-cutting concerns such as Logging, by hooking into the sequence of Action events.

Middleware can run before reducing the state or after depending on the need, this can be achieved by overriding the provided methods.

Store

The Store "stores" the State, and exposes it for observation as an Observable. It also connects all the other abstractions together.

To create a Store, simply instantiate it with an initial State and its related Reducer:

val counterStore = Store(initialState, reducer)

Several middleware can also be added to the Store through the Store's addMiddleware method.

Examples

Examples of usage can be seen in the tests

rxreduxk's People

Contributors

beylerian avatar keithyokoma avatar kittinunf avatar pgreze avatar tanakaworld avatar yhanada avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

cfirmo33 worker8

rxreduxk's Issues

Republish the library to maven central

As JCenter will close its distribution service and stop the delivery in February 2022, we need to republish this library including the past versions on Maven Central.

Sink/NoOp entities

It could be useful to have some "no-op" entities that act as loop/sinks in some situations, where we need to define them but have no state mutation.

For example

object SinkState : State

object SinkReducer : Reducer<State> {
    override fun reduce(currentState: State, action: Action): State = SinkState
}

object NoOpReducer : Reducer<State> {
    override fun reduce(currentState: State, action: Action): State = currentState
}

Deploy artifact to SNAPSHOT repository

Currently, builds fail for uploading duplicate artifacts on master branch workflow.
It seems to be better to upload them as a SNAPSHOT, and upload the finalized artifact to the release repository when we create a new tag.

Restrict the Action type for the reducers.

Maybe define the Action type for reducers.

Instead of

interface Reducer<S : State> {
    fun reduce(currentState: S, action: Action): S
}

Set the Action type

interface Reducer<S : State, A: Action> {
    fun reduce(currentState: S, action: A): S
}

This implies that invalid actions will not be allowed by design, since they will be type checked.

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.