Skip to content

Commit 18f564f

Browse files
committed
Duration Convenience
1 parent 4738163 commit 18f564f

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

Sources/NetworkPathMonitor/NetworkPathMonitor.swift

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Network
1616

1717
public 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+
}

Tests/NetworkPathMonitorTests/NetworkPathMonitorTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class NetworkPathMonitorTests: XCTestCase, @unchecked Sendable {
2929
XCTAssertNotNil(currentPath)
3030

3131
// Test with custom debounce interval
32-
let debounceMonitor = NetworkPathMonitor(debounceInterval: 1.0)
32+
let debounceMonitor = NetworkPathMonitor(debounceInterval: .seconds(1.0))
3333
let debounceIsActive = await debounceMonitor.isActive
3434
XCTAssertFalse(debounceIsActive)
3535
}
@@ -129,7 +129,7 @@ class NetworkPathMonitorTests: XCTestCase, @unchecked Sendable {
129129
// MARK: - Debounce Tests
130130

131131
func testDebounceInterval() async throws {
132-
let debounceMonitor = NetworkPathMonitor(debounceInterval: 0.5)
132+
let debounceMonitor = NetworkPathMonitor(debounceInterval: .seconds(0.5))
133133
let expectation = XCTestExpectation(description: "Debounced path update")
134134
var updateCount = 0
135135

@@ -247,7 +247,7 @@ class NetworkPathMonitorTests: XCTestCase, @unchecked Sendable {
247247

248248
func testIgnoreFirstPathUpdateWithDebounce() async {
249249
let debounceIgnoreMonitor = NetworkPathMonitor(
250-
debounceInterval: 0.5,
250+
debounceInterval: .seconds(0.5),
251251
ignoreFirstPathUpdate: true
252252
)
253253
let expectation = XCTestExpectation(description: "First debounced update should be ignored")

0 commit comments

Comments
 (0)