Skip to content

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

StateManagement

Experimental. v0.9.4. The public API will break until 1.0.0.

StateManagement is a state library for Swift and SwiftUI. One Environment owns all State. State is sliced into Containers. You read a Value with @Watch. You change it only through an Operation. Observation is per Value, and per key in a dictionary. The Environment is not SwiftUI’s @Environment. It resolves Containers for you, in views and outside them, with no DI container.

import SwiftUI
import StateManagement

final class CounterContainer: StateContainer {
    var count: Int = 0
}

struct Increment: SyncOperation {
    func perform(in env: SyncOperationEnvironment) {
        let count = env.read(\CounterContainer.count)
        env.write(\CounterContainer.count, value: count + 1)
    }
}

struct CounterView: View {
    @Watch(\CounterContainer.count) var count: Int
    @Perform var perform

    var body: some View {
        Button("\(count)") { perform(Increment()) }
    }
}

let root = CounterView()
    .sharedEnvironment(SharedEnvironment())

A Computed derives a Value, a Service reacts, and a Satellite ships the AsyncStrategy for persistence or HTTP while the Environment owns the seam and holds the Value.

Replace @Published with @SMPublished to keep leftover Combine call sites while the Environment owns the Value.

import Combine
import StateManagement

final class SettingsController: StateContainer, ObservableObject {
    @SMPublished var theme = "system"
}

let leftover = SettingsController()
leftover.theme = "dark"                       // always SharedEnvironment.shared
let sub = leftover.$theme.sink { print($0) }  // Publisher<Value, Never>, not Published.Publisher

@Watch and Operations use the Environment they were given. Leftover Combine cannot. Tests that go through instance.theme use .shared plus reset(). $ does not support assign(to:).

If you know TCA or Redux

  • Isolated Operations, not one reducer tree.
  • Flat Containers, not nested feature state.
  • async/await in an Async operation, not an effect layer.
  • @Watch / @Perform, not a ViewStore.

Tests perform Operations on a fresh Environment; link StateManagementTestingSupport from the test target.

Requirements

  • Swift 6.2+
  • macOS 12+, iOS 17+

Installation

Add the package with Swift Package Manager, then depend on the products you need:

  • StateManagement: the library.
  • StateManagementTestingSupport: testing helpers. Test target only.
.target(name: "MyFeature", dependencies: ["StateManagement"]),
.testTarget(name: "MyFeatureTests", dependencies: ["MyFeature", "StateManagementTestingSupport"]),

The QuickStart and leftover-Combine examples live in Snippets/; swift build compiles them and CI checks they still match this file.

See PHILOSOPHY.md for what it will and will not do, and CONTRIBUTING.md to work on it.

License

MIT. See LICENSE.txt.

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages