Latest 1.2
Homepage https://github.com/aronse/RdxSwift
License MIT
Platforms ios 9.0
Dependencies RxCocoa, RxSwift
Authors

CI Status
Version
License
Platform

RdxSwift is a redux-inspired, RxSwift-backed state manager.

This library relies heavily on RxSwift

It provides a set of classes, protocols and enums to easily manage your state by dispatching actions and async actions.


RdxStateType protocol

The protocol RdxStateType is used to define any state. All your states must conform to this protocol. It is empty by default and you may extend it at your convenience.

Example

struct MyState: RdxStateType {
  var someStringProperty: String = ""
  var someIntProperty: Int = 0
  var apiData: Dictionary<String, Any> = [ : ]
}

RdxActionType protocol

The protocol RdxActionType is used to define any action. All your actions must conform to this protocol. It is empty by default and you may extend it at your convenience.

Example

enum MyAction: RdxActionType {
  case changeStringProperty(String)
  case changeIntProperty(Int)
  case doSomethingAsync
}

RdxStore<S: RdxStateType, A: RdxActionType> class

The RdxStore class handles the internal state manager’s logic.

  • init

import RxSwift
import RxCocoa

typealias MyStore = RdxStore<MyState, MyAction>

let defaultState = MyState()

func reducer(oldState: MyState, action: MyAction) -> MyState {
var state = oldState
switch action {
case .changeStringProperty(let val):
state.someStringProperty = val
case .changeIntProperty(let val):
state.someIntProperty = val
default:
break
}
return state
}

let store = MyStore(
state: defaultState,
reducer: reducer,
middlewares: [ ],
asyncReducer: nil
)


- dispatch actions
```swift
store.dispatch(action: .changeStringProperty("foo"))
store.dispatchAsync(action: .doSomethingAsync)
  • middlewares
    
    func logger(action: MyAction, method: RdxActionMethod) {
    print(method, action)
    }

func trackMetrics(action: MyAction, method: RdxActionMethod) {
metrics.track("(currentUser.uid) dispatched (action)")
}

// …

let store = MyStore(
state: defaultState,
reducer: reducer,
middlewares: [ logger, trackMetrics ],
asyncReducer: nil
)


- Async reducer
```swift

func asyncReducer(store: MyStore, action: MyAction) -> Observable<MyState> {
  switch action {
    case .doSomethingAsync:
      let response$: Observable<Dictionary<String, Any>> = api.get("/something")
      return response$
        .map({ dictionary -> MyState in
          var state = store.state
          state.apiData = dictionary
          return state
        })
     default:
      return .just(store.state)
  }
}

let store = MyStore(
  ...,
  asyncReducer: asyncReducer
)
  • subscribe to state / observe state
    
    // listen to state
    store.subscribe()
    .bind { (state: MyState) -> Void in
    // do something
    }

// listen to one state property
store.subscribe()
.map { $0.someStringProperty }
.bind { (prop: String) -> Void in
// do something
}


### RdxActionMethod enum
```swift
public enum RdxActionMethod {
  case sync
  case async
}

Notes

  • All reducers are triggered on a background queue.
  • store.subscribe() returns an Observable that triggers on the main thread.

References

Installation

RdxSwift is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'RdxSwift'

Author

Alex Ronse, [email protected]

License

RdxSwift is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "RdxSwift",
    "version": "1.2",
    "summary": "redux-inspired, rxswift-backed swift state manager",
    "description": "RdxSwift is a RxSwift state manager inspired by Redux.",
    "homepage": "https://github.com/aronse/RdxSwift",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "aronse": "[email protected]"
    },
    "source": {
        "git": "https://github.com/aronse/RdxSwift",
        "tag": "1.2"
    },
    "social_media_url": "https://twitter.com/ronse_a",
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "RdxSwift/Classes/**/*",
    "dependencies": {
        "RxCocoa": [],
        "RxSwift": []
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This