Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A simple and predictable state management library inspired by Redux for Kotlin Multiplatform using the concepts of actors.

License

NotificationsYou must be signed in to change notification settings

abhimuktheeswarar/Flywheel

Repository files navigation

Maven CentralGitHub LicenseKotlinbadge-androidbadge-jvmbadge-applebadge-jsbadge-linuxbadge-windowsbadge-native

A simple and predictable state management library inspired byFlux +Elm +Redux. Flywheel is built on top ofCorotuines using the concepts ofstructured concurrency. At the core, lies theState Machine which is based onactor 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.5-RC")          }      }  }}

In Android / Gradle project:

dependencies {    implementation("com.msabhi:flywheel-android:1.1.5-RC")}

In Apple platforms

You can use theSwift Package Manager to installFlywheel by adding the proper description to yourPackage.swift file:

// swift-tools-version:5.3import PackageDescriptionletpackage=Package(    name:"YOUR_PROJECT_NAME",    dependencies:[.package(url:"https://github.com/abhimuktheeswarar/Flywheel.git", from:"1.1.5-RC"),])

Then runswift build whenever you get prepared.

Usage

This is how a simple counter example looks like.

  1. Define a state.

    data classCounterState(valcounter:Int =0) : State
  2. Define actions that can change the state.

    sealedinterfaceCounterAction :Action {object IncrementAction : CounterActionobject 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) {isCounterAction.IncrementAction-> copy(counter= counter+1)isCounterAction.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 ourwiki.

License

Copyright (C) 2021 Abhi MuktheeswararLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.

[8]ページ先頭

©2009-2025 Movatter.jp