Skip to content

stanwood/StanwoodGroupChain

Repository files navigation

StanwoodGroupChain

Swift Version Build Status

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

pod "StanwoodGroupChain"

Usage

Add handlers

import StanwoodGroupChain

class ExampleHandler: AbstractHandler {
    
    override func execute(_ element: ChainElement) {
        
        /// Make a networking call, load from file, run animation and return a result
        
        /// Access the target
        let target = element.target
        
        /// Use custom userInfo
        let userInfo = element.userInfo
        
        /// Return a response if reqruied on success
        let someItem = ModelItem()
        let response = ChainResponse(object: someItem)
        let successResult: ChainResult = .success(response)
        element.resultComplition?(successResult)
        
        /// Return an error if requried
        let error: ChainError = ChainError(message: "Something went wrong...")
        let failureResult: ChainResult = .failure(error)
        element.resultComplition?(failureResult)
    }
}

Declare a chain

var chain: Chain?

let handlers = [ExampleHandler(), HandlerOne(), HandlerTwo(), HandlerThree(), HandlerFour(), HandlerFive(), HandlerSix()]
chain = Chain(handlers: handlers)  

Instantiate handler element and handle

let handlerFour = ChainElement(type: .type(ExampleHandler.self), target: self) { (result) in
            
   switch result {
      case .failure(let error): print("Handle error, \(error.description)")
      case .success(let item): print("Handle item, \(item)")
                
         /// Call the next handler
      }
  }
        
let handlerOne = ChainElement(type: .type(HandlerSix.self), target: self) { [weak self, handlerFour = handlerFour] (result) in
            
    guard let `self` = self else { return }
            
    switch result {
      case .failure(let error): print("Handle error, \(error.description)")
      case .success(let item): print("Handle item, \(item)")
                
         /// Call the next handler
                
         self.chain?.handel(handlerFour)
      }
 }
        
chain?.handel(handlerOne)

License

StanwoodCore is under MIT licence. See the LICENSE file for more info.