iOS/Sources/Shared/API/Webhook/Networking/Promise+RetryNetworking.swift
Zac West b80c874917
Add code coverage reporting to tests (#1412)
Adds code coverage reports to pull requests. Enabling code coverage gathering appears to be about a 25% increase in build time, unfortunately, so just enabling it for the App/Shared targets and doing it in a separate scheme so it doesn't impact local build times.
2021-01-24 10:53:43 -08:00

25 lines
733 B
Swift

import Foundation
import PromiseKit
// https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md
func attemptNetworking<T>(
maximumRetryCount: Int = 3,
delayBeforeRetry: DispatchTimeInterval = .seconds(2),
_ body: @escaping () -> Promise<T>
) -> Promise<T> {
var attempts = 0
func attempt() -> Promise<T> {
attempts += 1
return body().recover { error -> Promise<T> in
guard attempts < maximumRetryCount, error is URLError else { throw error }
if Current.isRunningTests {
return attempt()
} else {
return after(delayBeforeRetry).then(on: .main, attempt)
}
}
}
return attempt()
}