File tree Expand file tree Collapse file tree
Sources/SwiftRetrier/Util Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -68,6 +68,26 @@ let value = try await withExponentialBackoff()
6868Note that you can use ` cancellableValue ` instead of ` value ` . In this case, if the task wrapping the concurrency context
6969is 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
7393All retriers (including repeaters) expose Combine publishers that publish relevant events.
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments