Skip to content

Latest commit

 

History

History
114 lines (86 loc) · 3.41 KB

README.md

File metadata and controls

114 lines (86 loc) · 3.41 KB

CI Status Version License Platform

Intro

Optical is a lightweight and predictable state management pattern framework for iOS

  • Find and fix bugs faster and easier.
  • Change existing behaviors with confidence.
  • Add new features easily.
  • Write shorter methods with single responsibility.
  • Extract business logic from view controllers into opticle.
  • Build reusable components with network services and utilities objects.
  • Write factored code from the start.
  • Write fast and maintainable unit tests with state base.
  • 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
  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
  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
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
  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. To install it, simply add the following line to your Podfile:

pod 'Optical'

Author

Geektree0101, h2s1880@gmail.com

License

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