Latest 0.0.2
Homepage https://github.com/giuseppesalvo/atlas
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0
Authors

Atlas Swift Store

Atlas is a redux store for your swift apps without the reducer layer

Version
License
Platform

Installation

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

pod 'AtlasSwift'

How it works

there are 3 main components

  • State
  • Actions
  • Guards

State


// The state should be always a struct, to ensure immutability
struct CountState {
    var count: Int
}

let store = Atlas(state: CountState(
    count: 0
))

Actions

Actions can be synchronous or asynchronous


struct Increment: AtlasAction {
    func handle(state: CountState, completition: @escaping AtlasActionCompletition<CountState>) {
        var newState   = state
        newState.count = result
        completition(newState)
    }
}

store.dispatch(Increment())

// With a completition callback
store.dispatch(Increment()) { state in
    print("done! ", state.count)
}

Action Group


struct CountOperation: AtlasActionGroup {
    func handle(store: Atlas<CountState>, completition: @escaping AtlasActionGroupCompletition) {
        store.dispatch(Increment())
        store.dispatch(Increment())
        store.dispatch(Increment())
        store.dispatch(Decrement()) { _ in
            completition()
        }
    }
}

store.dispatch(CountOperation())

Guards

Guards track the store lifecycle.
In future, they will include also a middleware-like function.


struct Logger: AtlasGuard {

    func willUpdate<A: AtlasAction>(state: State, action: A) {
        print("will update!", state.count)
    }

    func didUpdate<A: AtlasAction>(state: State, action: A) {
        print("update!", state.count)
    }
}

let store = Atlas(state: YourState(), guards: [ Logger() ])

Subscription


extension YourController: AtlasSubscriber {

    override func viewDidAppear() {
        super.viewDidAppear()
        store.subscribe(self)
    }

    // There is no real need to unsubscribe your objects. Subscribers are weak references
    override func viewWillDisappear() {
        super.viewWillDisappear()
        store.unsubscribe(self)
    }

    func newState(_ state: CountState) {
        print("count state changed!")
    }
}

Subscriber Should Update

Avoiding unneeded updates and subscribing to a specific part of the store.
By default, the shouldUpdate function returns always true.


extension YourController: AtlasSubscriber {

    // subscription code...

    func shouldUpdate(prevState: CountState?, newState: CountState) -> Bool {
        return prevState?.count != newState.count
    }
}

Notes

  • Atlas uses a serial queue to dispatch every action, so you can be sure that your actions will be executed in the invokation order
  • The dispatch function is async, to avoid deadlocks. To track its end, you can use the completition argument
  • The subcribe function also have a second argument "queue", to subscribe a class on a specific queue

That’s all!

Inspired by ReSwift and Redux

License

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

Latest podspec

{
    "name": "AtlasSwift",
    "version": "0.0.2",
    "summary": "Atlas is a redux store for your swift apps without the reducer layer",
    "description": "Atlas is a redux like store for your swift iOS/macOS/tvOS apps without the reducer layer",
    "homepage": "https://github.com/giuseppesalvo/atlas",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Giuseppe": "[email protected]"
    },
    "source": {
        "git": "https://github.com/giuseppesalvo/Atlas.git",
        "tag": "0.0.2"
    },
    "social_media_url": "https://github.com/giuseppesalvo",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source_files": "AtlasSwift/Classes/**/*",
    "swift_version": "4.2",
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "4.1"
    }
}

Pin It on Pinterest

Share This