Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
This closes #5 implementing retry policy.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Rupérez committed Jan 18, 2018
1 parent 383fb06 commit 56014a4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Intelygenz <[email protected]>
Copyright (c) 2018 Intelygenz <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,39 @@ Kommander().make {
```swift
Kommander().make {
throw CocoaError(.featureUnsupported)
}.error({ error in
}.error { error in
print(String(describing: error!))
}).execute()
}.execute()
```

##### Retry after cancellation:

```swift
let kommand = Kommander().make { () -> Any? in
// Your code here
}.success { result in
// Your success handling here
}.error({ error in
}.error { error in
// Your error handling here
}).execute()
}.execute()

kommand.cancel()

kommand.retry()
```

##### Retry after failure:

```swift
let kommand = Kommander().make { () -> Any? in
// Your code here
}.error { error in
// Your error handling here
}.retry { error, executionCount in
return executionCount < 2
}.execute()
```

#### Creating Kommanders:

```swift
Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2017 Intelygenz.</string>
<string>Copyright © 2018 Intelygenz.</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand Down
20 changes: 20 additions & 0 deletions Source/Kommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ open class Kommand<Result> {
public typealias SuccessClosure = (_ result: Result) -> Void
/// Error closure type
public typealias ErrorClosure = (_ error: Error?) -> Void
/// Retry closure type
public typealias RetryClosure = (_ error: Error?, _ executionCount: UInt) -> Bool

/// Kommand<Result> state
internal(set) public final var state = State.uninitialized
Expand All @@ -45,6 +47,10 @@ open class Kommand<Result> {
private(set) final var successClosure: SuccessClosure?
/// Error closure
private(set) final var errorClosure: ErrorClosure?
/// Retry closure
private(set) final var retryClosure: RetryClosure?
/// Retry count
internal(set) final var executionCount: UInt
/// Operation to cancel
internal(set) final weak var operation: Operation?

Expand All @@ -53,6 +59,7 @@ open class Kommand<Result> {
self.deliverer = deliverer
self.executor = executor
self.actionClosure = actionClosure
executionCount = 0
state = .ready
}

Expand All @@ -78,6 +85,12 @@ open class Kommand<Result> {
return self
}

/// Specify Kommand<Result> retry closure
@discardableResult open func retry(_ retry: @escaping RetryClosure) -> Self {
self.retryClosure = retry
return self
}

/// Execute Kommand<Result> after delay
@discardableResult open func execute(after delay: DispatchTimeInterval) -> Self {
executor?.execute(after: delay, closure: {
Expand All @@ -96,6 +109,7 @@ open class Kommand<Result> {
if let actionClosure = self.actionClosure {
self.state = .running
let result = try actionClosure()
self.executionCount += 1
guard self.state == .running else {
return
}
Expand All @@ -111,6 +125,12 @@ open class Kommand<Result> {
self.deliverer?.execute {
self.state = .finished
self.errorClosure?(error)
self.executor?.execute {
if self.retryClosure?(error, self.executionCount) == true {
self.state = .ready
self.execute()
}
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions Source/Kommander.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private extension Kommander {
if let actionClosure = kommand.actionClosure {
kommand.state = .running
let result = try actionClosure()
kommand.executionCount += 1
guard kommand.state == .running else {
return
}
Expand All @@ -131,6 +132,12 @@ private extension Kommander {
self.deliverer.execute {
kommand.state = .finished
kommand.errorClosure?(error)
self.executor.execute {
if kommand.retryClosure?(error, kommand.executionCount) == true {
kommand.state = .ready
kommand.execute()
}
}
}
}
}
Expand Down

0 comments on commit 56014a4

Please sign in to comment.