Skip to content

Commit

Permalink
Add synchronous interfaces to ReachabilityMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jun 14, 2022
1 parent 4cbd824 commit 08bb161
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ import SystemConfiguration
/// - ``reachabilityPublisher(forHost:)``
/// - ``reachabilityPublisher(forAddress:)``
/// - ``Publisher``
///
/// ### Synchronous
///
/// - ``currentReachability``
/// - ``reachability``
/// - ``reachability(forHost:)``
/// - ``reachability(forAddress:)``
@available(macOS 10.13, iOS 11, watchOS 4, tvOS 11, *)
public final class ReachabilityMonitor {

Expand Down Expand Up @@ -285,13 +292,53 @@ public final class ReachabilityMonitor {
/// // Do something with `reachability`
/// }
/// ```
///
/// - Throws: An error of type ``Error``
public static var reachability: Reachability {
get throws {
let monitor = try ReachabilityMonitor()
return try monitor.currentReachability
}
}

/// Retrieve the latest known reachability for a specific host
///
/// ```
/// func updateReachability() throws {
/// let reachability = try ReachabilityMonitor.reachability(forHost: "www.apple.com")
/// // Do something with `reachability`
/// }
/// ```
///
/// - Parameter host: The host you want to monitor
///
/// - Returns: The latest known reachability for the provided host
///
/// - Throws: An error of type ``Error``
public static func reachability(forHost host: String) throws -> Reachability {
let monitor = try ReachabilityMonitor(host: host)
return try monitor.currentReachability
}

/// Retrieve the latest known reachability for a specific socket address
///
/// ```
/// func updateReachability() throws {
/// let reachability = try ReachabilityMonitor.reachability(forAddress: myAddress)
/// // Do something with `reachability`
/// }
/// ```
///
/// - Parameter host: The socket address you want to monitor
///
/// - Returns: The latest known reachability for the provided host
///
/// - Throws: An error of type ``Error``
public static func reachability(forAddress address: sockaddr) throws -> Reachability {
let monitor = try ReachabilityMonitor(address: address)
return try monitor.currentReachability
}

// MARK: - Private

private init(ref: SCNetworkReachability,
Expand Down

0 comments on commit 08bb161

Please sign in to comment.