Skip to content

Commit e2d767f

Browse files
committed
onEach modifier (#37)
* Implement onEach modifier #36 * Update README.md
1 parent 3a884f7 commit e2d767f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ let value = try await withExponentialBackoff()
6868
Note that you can use `cancellableValue` instead of `value`. In this case, if the task wrapping the concurrency context
6969
is cancelled, the underlying retrier will be cancelled.
7070

71+
## Simple events handling
72+
73+
Retrier events can be handled simply.
74+
75+
```swift
76+
fetcher.onEach {
77+
switch $0 {
78+
case .attemptSuccess(let value):
79+
print("Fetched something: \(value)")
80+
case .attemptFailure(let failure):
81+
print("An attempt #\(failure.index) failed with \(failure.error)")
82+
case .completion(let error):
83+
print("Fetcher completed with \(error?.localizedDescription ?? "no error")")
84+
}
85+
}
86+
```
87+
88+
Keep in mind that the event handler will be retained until the retrier finishes (succeeding, failing or being
89+
cancelled).
90+
7191
## Combine publishers
7292

7393
All retriers (including repeaters) expose Combine publishers that publish relevant events.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Combine
2+
3+
public extension Retrier {
4+
5+
func onEach(handleEvent: @escaping (RetrierEvent<Output>) -> Void) -> Self {
6+
var subscription: AnyCancellable?
7+
subscription = publisher()
8+
.sink(receiveCompletion: { _ in
9+
subscription?.cancel()
10+
}, receiveValue: handleEvent)
11+
return self
12+
}
13+
}

0 commit comments

Comments
 (0)