@@ -16,7 +16,7 @@ import Network
1616
1717public enum NetworkPathMonitorInfo : Sendable {
1818 /// Current NetworkPathMonitor version.
19- public static let version = " 0.0.5 "
19+ public static let version = " 0.0.6 "
2020}
2121
2222/// A class that monitors network path changes using `NWPathMonitor`.
@@ -27,7 +27,7 @@ public actor NetworkPathMonitor {
2727 private let monitorQueue : DispatchQueue
2828
2929 /// Debounce interval in seconds.
30- private let debounceInterval : TimeInterval
30+ private let debounceInterval : Interval
3131
3232 /// Ignore first path update.
3333 private let ignoreFirstPathUpdate : Bool
@@ -55,13 +55,13 @@ public actor NetworkPathMonitor {
5555
5656 /// Initializes a new instance of `NetworkPathMonitor`.
5757 /// - Parameter queue: The queue on which the network path monitor runs. Default is a serial queue with a unique label.
58- /// - Parameter debounceInterval: Debounce interval in seconds . If set to 0, no debounce will be applied. Default is 0 seconds.
58+ /// - Parameter debounceInterval: Debounce interval. If set to 0, no debounce will be applied. Default is 0 seconds.
5959 /// - Parameter ignoreFirstPathUpdate: Ignore first path update. Default is false.
6060 public init ( queue: DispatchQueue = . init( label: " com.networkPathMonitor. \( UUID ( ) ) " ) ,
61- debounceInterval: TimeInterval = 0 ,
61+ debounceInterval: Interval = . seconds ( 0 ) ,
6262 ignoreFirstPathUpdate: Bool = false )
6363 {
64- precondition ( debounceInterval >= 0 , " debounceInterval must be greater than or equal to 0 " )
64+ precondition ( debounceInterval. nanoseconds >= 0 , " debounceInterval must be greater than or equal to 0 " )
6565 monitorQueue = queue
6666 currentPath = networkMonitor. currentPath
6767 self . debounceInterval = debounceInterval
@@ -111,7 +111,7 @@ public actor NetworkPathMonitor {
111111 }
112112
113113 debounceTask? . cancel ( )
114- guard debounceInterval > 0 else {
114+ guard debounceInterval. nanoseconds > 0 else {
115115 // No debounce, yield immediately
116116 debounceTask = nil
117117 await yieldNetworkPath ( path)
@@ -120,7 +120,7 @@ public actor NetworkPathMonitor {
120120 // Debounce is active
121121 debounceTask = Task {
122122 do {
123- try await Task . sleep ( nanoseconds: UInt64 ( self . debounceInterval * 1_000_000_000 ) )
123+ try await Task . sleep ( nanoseconds: UInt64 ( self . debounceInterval. nanoseconds ) )
124124 await self . yieldNetworkPath ( path)
125125 } catch is CancellationError {
126126 // Task was cancelled, do nothing
@@ -184,3 +184,30 @@ public extension NetworkPathMonitor {
184184 networkPathUpdater = handler
185185 }
186186}
187+
188+ // MARK: - Duration Convenience
189+
190+ public extension NetworkPathMonitor {
191+ enum Interval : Sendable {
192+ case nanoseconds( _: Int )
193+ case microseconds( _: Int )
194+ case milliseconds( _: Int )
195+ case seconds( _: Double )
196+ case minutes( _: Int )
197+
198+ var nanoseconds : Int {
199+ switch self {
200+ case let . nanoseconds( value) :
201+ return value
202+ case let . microseconds( value) :
203+ return value * 1000
204+ case let . milliseconds( value) :
205+ return value * 1_000_000
206+ case let . seconds( value) :
207+ return Int ( value * 1_000_000_000 )
208+ case let . minutes( value) :
209+ return value * 60 * 1_000_000_000
210+ }
211+ }
212+ }
213+ }
0 commit comments