Flywheel

1.3.0

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

What's New

v1.3.0

2026-08-06T08:23:15Z

Added

ReduceAction — typed, self-reducing actions that fix the lost-update problem.

When multiple SideEffects read a state snapshot, compute a new collection from it,
and dispatch the whole collection as a replacement, the last writer silently
overwrites the others' changes. awaitState() cannot prevent this — it guarantees
a fresh read, not a safe write.

A ReduceAction is a normal, named action that carries only the delta in its
properties and its own reduce(state) merge. It is dispatched like any other
action; the state machine applies reduce against the current state — not the
snapshot the SideEffect saw — so concurrent updates compose instead of overwriting
each other:

data class MergeItemsAction(val entries: Map<String, Int>) : ReduceAction<ItemsState> {
    override fun reduce(state: ItemsState) = state.copy(items = state.items + entries)
}

// In a SideEffect: heavy computation stays here, off the state machine
val delta = heavyProcessing(response)
// Dispatch the operation like any other action; reduce() merges into the CURRENT state
dispatch(MergeItemsAction(delta))

Because it is a regular Action, it stays part of the flow: middleware sees it,
it appears in actions/actionStates with its payload visible in logs, and other
SideEffects can react to it. Keep reduce() fast and pure — carry precomputed
deltas, and combine them with the current state. Works uniformly for maps, lists,
sets, and scalar fields.

See the Concurrency and Collections
wiki page for the full guidance, including list-specific rules (operate by stable
IDs or anchors computed inside reduce(), never by snapshot-derived indexes).

Removed

setState and SetStateAction (introduced in 1.2.0) are removed.

setState solved the same lost-update problem but exposed an anonymous write
path that let code bypass typed actions entirely — against Flywheel's design,
where every state change is a named action in the flow. ReduceAction provides
the same atomicity as part of the action vocabulary.

Migrating from 1.2.0 (if you adopted setState):

// 1.2.0
setState("mergeItems") { copy(items = items + delta) }

// 1.3.0
data class MergeItemsAction(val delta: Map<String, Int>) : ReduceAction<ItemsState> {
    override fun reduce(state: ItemsState) = state.copy(items = state.items + delta)
}
dispatch(MergeItemsAction(delta))

Code coming from 1.1.7 or earlier needs no changes.

Install

Gradle:

implementation("com.msabhi:flywheel:1.3.0")          // Kotlin Multiplatform
implementation("com.msabhi:flywheel-android:1.3.0")  // Android

Swift Package Manager — add https://github.com/abhimuktheeswarar/Flywheel.git
and select 1.3.0.

Full changelog: v1.2.0...v1.3.0

Flywheel

Maven Central GitHub License Kotlin Coroutines 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.3.0")
          }
      }
  }
}

In Android / Gradle project:

dependencies {

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

In Apple platforms (Swift Package Manager)

In Xcode, go to File → Add Package Dependencies and enter the repository URL:

https://github.com/abhimuktheeswarar/Flywheel.git

Or add it to your Package.swift:

// swift-tools-version:5.9
import PackageDescription

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

Each GitHub release includes a Flywheel.xcframework.zip artifact with a matching Package.swift manifest containing the URL and checksum.

Building the XCFramework locally

The XCFramework is not checked into the repository. To build it from source:

# Build and copy into flywheel/xcframework/ for local SPM use
./gradlew :flywheel:copyXCFrameworkToRepo

# Build, zip and generate a release-ready Package.swift with checksum
./gradlew :flywheel:generatePackageSwift

# Full release prep: build XCFramework, generate Package.swift, copy to repo root
./gradlew prepareSpmRelease

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(
        config = getDefaultStateReserveConfig(),
        initialState = InitialState.set(CounterState()),
        reduce = reduce,
        middlewares = null)
  5. Listen for state changes

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

    stateReserve.dispatch(IncrementAction)
  7. To update the state based on its current value (e.g. from concurrent SideEffects), dispatch a ReduceAction — an action that carries a delta and its own merge. Its reduce is applied atomically against the current state on the state machine, so concurrent updates compose instead of overwriting each other. Do the heavy computation first, then dispatch only the precomputed delta.

    data class AddToCountAction(val amount: Int) : ReduceAction<CounterState> {
        override fun reduce(state: CounterState) = state.copy(counter = state.counter + amount)
    }
    
    stateReserve.dispatch(AddToCountAction(5))

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.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Aug 09 2026 07:17:57 GMT-0900 (Hawaii-Aleutian Daylight Time)