Code Monkey home page Code Monkey logo

optical's Introduction

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, [email protected]

License

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

optical's People

Contributors

geektree0101 avatar

Stargazers

MohsinAli avatar

Watchers

MohsinAli avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.