Code Monkey home page Code Monkey logo

flywheel's Introduction

Flywheel

Maven Central GitHub License Kotlin badge-android badge-jvm badge-apple badge-js badge-linux badge-windows badge-native

A simple and predictable state management library inspired by Flux + Elm + Redux. Flywheel is built on top of Corotuines using the concepts of structured concurrency. At the core, lies the State Machine which is based on actor model.

Why Flywheel?

The goal was to make the state management concept of Redux simple, understandable & easy to use in Kotlin based projects. To achieve that, we adapted only the core concepts from Redux and slightly modified them. We excluded Android, Apple or any platform-specific dependencies. It is just pure Kotlin. By doing so, you are free to choose your architecture that best suits your codebase, no need to make any big refactor to fit in Flywheel. Don't be fooled by its simplicity, Flywheel got you covered for all practical use-cases. Even if we missed anything, it can be easily extended to support your use cases.

Getting started

In Kotlin Multiplatfrom project:

kotlin {
  sourceSets {
      val commonMain by getting {
          dependencies {
              implementation("com.msabhi:flywheel:1.1.4-RC")
          }
      }
  }
}

In Android / Gradle project:

dependencies {

    implementation("com.msabhi:flywheel-android:1.1.4-RC")
}

In Apple platforms

You can use CocoaPods to install Flywheel by adding it to your Podfile:

use_frameworks!

target 'MyApp' do

    pod 'Flywheel', '~> 1.1.4-RC'

end

You can use the Swift Package Manager to install Flywheel by adding the proper description to your Package.swift file:

// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/abhimuktheeswarar/Flywheel.git", from: "1.1.4-RC"),
    ]
)

Then run swift build whenever you get prepared.

Usage

This is how a simple counter example looks like.

  1. Define a state.

    data class CounterState(val counter: Int = 0) : State
  2. Define actions that can change the state.

    sealed interface CounterAction : Action {
    
        object IncrementAction : CounterAction
    
        object DecrementAction : CounterAction
    }
  3. Define a reducer that updates the state based on the action & current state.

    val reduce = reducerForAction<CounterAction, CounterState> { action, state ->
        with(state) {
            when (action) {
                is CounterAction.IncrementAction -> copy(counter = counter + 1)
                is CounterAction.DecrementAction -> copy(counter = counter - 1)
            }
        }
    }
  4. Create a StateReserve.

    val stateReserve = StateReserve(
        initialState = InitialState.set(CounterState()),
        reduce = reduce)
  5. Listen for state changes

    stateReserve.states.collect { state -> println(state.counter) }
  6. Send actions to StateReserve to update the state.

    stateReserve.dispatch(IncrementAction)

To learn more about Flywheel, head on over to our wiki.

License

Copyright (C) 2021 Abhi Muktheeswarar

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

flywheel's People

Contributors

abhimuktheeswarar 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.