Skip to content

Commit cb0d364

Browse files
committed
Make State public, keep backwards compatible
1 parent ad74820 commit cb0d364

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

Sources/Retry/Retry.swift

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public enum Retry {
2525
(pow(2, max(0, attempt - 1)) * Decimal(baseDelay) as NSDecimalNumber).uint32Value
2626
}
2727

28-
struct State {
29-
var retriesLeft: Int
30-
var currentTry = 0
31-
var lastError: Swift.Error?
28+
public struct State {
29+
public var retriesLeft: Int
30+
public var currentTry = 0
31+
public var lastError: Swift.Error?
3232

33-
var isFirstIteration: Bool { currentTry == 0 }
34-
var hasRetriesLeft: Bool { retriesLeft > 0 }
33+
public var isFirstIteration: Bool { currentTry == 0 }
34+
public var hasRetriesLeft: Bool { retriesLeft > 0 }
3535
mutating func advance() {
3636
currentTry += 1
3737
retriesLeft -= 1
@@ -43,14 +43,14 @@ public enum Retry {
4343
delay: Double = 5,
4444
retries: Int = 5,
4545
logger: RetryLogging = DefaultLogger(),
46-
_ block: (Int) throws -> T) throws -> T {
46+
_ block: () throws -> T) throws -> T {
4747
var state = State(retriesLeft: retries)
4848
while true {
4949
if !state.isFirstIteration {
5050
logger.onStartOfRetry(label: label, attempt: state.currentTry)
5151
}
5252
do {
53-
return try block(state.currentTry)
53+
return try block()
5454
} catch let Error.abort(with: error) {
5555
throw Error.abort(with: error)
5656
} catch {
@@ -67,14 +67,62 @@ public enum Retry {
6767
delay: Double = 5,
6868
retries: Int = 5,
6969
logger: RetryLogging = DefaultLogger(),
70-
_ block: (Int) async throws -> T) async throws -> T {
70+
_ block: () async throws -> T) async throws -> T {
7171
var state = State(retriesLeft: retries)
7272
while true {
7373
if !state.isFirstIteration {
7474
logger.onStartOfRetry(label: label, attempt: state.currentTry)
7575
}
7676
do {
77-
return try await block(state.currentTry)
77+
return try await block()
78+
} catch let Error.abort(with: error) {
79+
throw Error.abort(with: error)
80+
} catch {
81+
if catchHandler(label, delay: delay, logger: logger, error: error, state: &state) {
82+
break
83+
}
84+
}
85+
}
86+
throw Error.retryLimitExceeded(lastError: state.lastError)
87+
}
88+
89+
@discardableResult
90+
public static func attempt<T>(_ label: String,
91+
delay: Double = 5,
92+
retries: Int = 5,
93+
logger: RetryLogging = DefaultLogger(),
94+
_ block: (State) throws -> T) throws -> T {
95+
var state = State(retriesLeft: retries)
96+
while true {
97+
if !state.isFirstIteration {
98+
logger.onStartOfRetry(label: label, attempt: state.currentTry)
99+
}
100+
do {
101+
return try block(state)
102+
} catch let Error.abort(with: error) {
103+
throw Error.abort(with: error)
104+
} catch {
105+
if catchHandler(label, delay: delay, logger: logger, error: error, state: &state) {
106+
break
107+
}
108+
}
109+
}
110+
throw Error.retryLimitExceeded(lastError: state.lastError)
111+
}
112+
113+
@discardableResult
114+
public static func attempt<T>(_ label: String,
115+
delay: Double = 5,
116+
retries: Int = 5,
117+
logger: RetryLogging = DefaultLogger(),
118+
_ block: (State) async throws -> T) async throws -> T {
119+
var state = State(retriesLeft: retries)
120+
while true {
121+
if !state.isFirstIteration {
122+
logger.onStartOfRetry(label: label, attempt: state.currentTry)
123+
}
124+
do {
125+
return try await block(state)
78126
} catch let Error.abort(with: error) {
79127
throw Error.abort(with: error)
80128
} catch {

Tests/RetryTests/RetryTests.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,24 @@ final class RetryTests: XCTestCase {
4242
struct Error: Swift.Error { }
4343

4444
// MUT
45-
try Retry.attempt("", delay: 0, retries: 3) { currentTry in
46-
XCTAssertEqual(currentTry, called)
45+
try Retry.attempt("", delay: 0, retries: 3) {
46+
called += 1
47+
if called < 3 {
48+
throw Error()
49+
}
50+
}
51+
52+
// validation
53+
XCTAssertEqual(called, 3)
54+
}
55+
56+
func test_attempt_state() throws {
57+
var called = 0
58+
struct Error: Swift.Error { }
59+
60+
// MUT
61+
try Retry.attempt("", delay: 0, retries: 3) { state in
62+
XCTAssertEqual(state.currentTry, called)
4763
called += 1
4864
if called < 3 {
4965
throw Error()

0 commit comments

Comments
 (0)