https://github.com/GeekTree0101/Optical
Lightweight & Predictable state management framework for iOS
https://github.com/GeekTree0101/Optical
architecture ios state-management unidirectional
Last synced: about 1 year ago
JSON representation
Lightweight & Predictable state management framework for iOS
- Host: GitHub
- URL: https://github.com/GeekTree0101/Optical
- Owner: GeekTree0101
- License: mit
- Created: 2019-07-29T00:25:17.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-31T01:00:17.000Z (almost 7 years ago)
- Last Synced: 2025-02-24T04:18:18.633Z (over 1 year ago)
- Topics: architecture, ios, state-management, unidirectional
- Language: Swift
- Size: 670 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://travis-ci.org/Geektree0101/Optical)
[](https://cocoapods.org/pods/Optical)
[](https://cocoapods.org/pods/Optical)
[](https://cocoapods.org/pods/Optical)
## Intro
Optical is a lightweight and predictable state management pattern framework for iOS

- [X] Find and fix bugs faster and easier.
- [X] Change existing behaviors with confidence.
- [X] Add new features easily.
- [X] **Write shorter** methods with single responsibility.
- [X] Extract business logic from view controllers into **opticle**.
- [X] Build **reusable** components with network services and utilities objects.
- [X] Write factored code from the start.
- [X] Write fast and **maintainable unit tests** with state base.
- [X] Have **confidence in your tests** to catch regression.
## Structures
### Workflow

- Dispatch: You can request network(backend) service or API and commit response for mutating state
```swift
var service: NetworkService = .init()
func dispatch(_ request: Request) {
// do commit
service.request(onSuccess: { [weak self] response in
self?.commit(.success(response))
})
}
```
- Mutation: You can mutate currentState with utilities base on **previous state with response** from dispatcher
```swift
var utility: SomeUtil = .init()
func mutate(_ state: State, response: Response) {
var newState = state
newState.value = utility.makeValue(from: response)
return newState
}
```
- Watcher: You can **observe state** changing from opticle
```swift
let opticle = SomeOpticle()
opticle.watch.live { newState in
print(newSate.value)
}
// you can observe state duplicately!
opticle.watch.live({ newState in
print("listen one more \(newSate.value)")
})
// you can observe state on other dispatch qos!
opticle.watch.live(on: DispatchQueue.global(.background), { newState in
print("background \(newSate.value)")
})
// Map & Fillter
opticle.watch.map { $0.list }.filter { $0.count > 10 }.live({ list in
print("map & filter")
})
```
### Mutation & Recover

- Mutation: It will be called by success commit from dispatcher
- Recover: You can recover state base on error. it will be called by error commit from dispatcher
```swift
func dispatch(_ request: Request) {
// success
self.commit(.success)
// error
self.commit(.failed(error), from: request)
}
func mutate(_ state: State, response: Response) -> State {
// .success only
}
func recover(_ state: State, request: Request, error: Error?) -> State {
// .failed from request
}
```
## Installation
Optical is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'Optical'
```
## Author
Geektree0101, h2s1880@gmail.com
## License
Optical is available under the MIT license. See the LICENSE file for more info.