Swiftpack.co - Package - clmntcrl/swidux

Swidux

Swift unidirectional data flow inspired by redux.

Stability

This library should be considered alpha, and not stable. Breaking changes will happen often.

Usage

Define your application state,

struct AppState {
    var count = 0
}

the list of actions that could be done,

enum CounterAction: Action {
    case increment
    case decrement
}

and how they impact the application state.

let counterReducer = Reducer<Int> { state, action in
    switch action {
    case CounterAction.increment: state += 1
    case CounterAction.decrement: state -= 1
    default: break
    }
}

Then initialize the application store,

let store = Store<AppState>(
    initialState: AppState(),
    reducer: counterReducer.lift(\.count)
)

subscribe any application state key path to be notified of state changes (make sure to keep reference to the token because its deallocation stop subscription).

let token = store.subscribe(\.count) { print($0) }

Now dispatch actions to mutate application state and use your subscription to state changes to update your UI, ...

store.dispatch(CounterAction.increment)
store.dispatch(CounterAction.decrement)

Discussions

AppState

Application state must be a value type because Swidux store rely on didSet observer to notify subscribers of state changes.

Reducers

Reducers use (inout State, Action) -> Void signature (instead of (State, Action) -> State) to improve performances and avoid lot of State copies.

Installation

Carthage

Add the following dependency to your Cartfile:

github "clmntcrl/swidux" ~> 0.1.1
$ carthage update

SwiftPM

Add package as dependency:

import PackageDescription

let package = Package(
    name: "AwesomeProjectName",
    dependencies: [
        .package(url: "https://github.com/clmntcrl/swidux.git", from: "0.1.1"),
    ],
    targets: [
        .target(name: "AwesomeProjectName", dependencies: ["Swidux"])
    ]
)
$ swift build

License

Swidux is released under the MIT license. See LICENSE for details.

Github

link
Stars: 0
Help us keep the lights on

Dependencies

Releases

0.1.1 - Oct 31, 2018

0.1.0 - Oct 18, 2018