https://github.com/stanwood/stanwoodgroupchain
https://github.com/stanwood/stanwoodgroupchain
chain-of-responsibility ios swift
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/stanwood/stanwoodgroupchain
- Owner: stanwood
- License: mit
- Created: 2017-07-02T06:42:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-04-02T10:31:14.000Z (over 5 years ago)
- Last Synced: 2025-02-09T22:33:28.626Z (9 months ago)
- Topics: chain-of-responsibility, ios, swift
- Language: Swift
- Size: 106 KB
- Stars: 1
- Watchers: 6
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StanwoodGroupChain
[]()
[](https://travis-ci.org/stanwood/StanwoodGroupChain)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Installation
```ruby
pod "StanwoodGroupChain"
```
## Usage
### Add handlers
```swift
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
```swift
var chain: Chain?
let handlers = [ExampleHandler(), HandlerOne(), HandlerTwo(), HandlerThree(), HandlerFour(), HandlerFive(), HandlerSix()]
chain = Chain(handlers: handlers)
```
### Instantiate handler element and handle
```swift
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.