@@ -16,7 +16,7 @@ import Network
1616
1717public enum NetworkPathMonitorInfo : Sendable {
1818 /// Current NetworkPathMonitor version.
19- public static let version = " 0.0.7 "
19+ public static let version = " 0.2.0 "
2020}
2121
2222/// A class that monitors network path changes using `NWPathMonitor`.
@@ -29,9 +29,6 @@ public actor NetworkPathMonitor {
2929 /// Debounce interval in seconds.
3030 private let debounceInterval : Interval
3131
32- /// Ignore first path update.
33- private let ignoreFirstPathUpdate : Bool
34-
3532 /// The network path update handler.
3633 private var networkPathUpdater : PathUpdateHandler ?
3734
@@ -45,27 +42,21 @@ public actor NetworkPathMonitor {
4542 private var debounceTask : Task < Void , Never > ?
4643
4744 /// Current network path.
48- public private( set) var currentPath : NWPath
49-
50- /// Flag to track if this is the first path update.
51- private var isFirstUpdate : Bool = true
45+ public private( set) var currentPath : NetworkPath
5246
5347 /// Network path status change notification.
5448 public static let networkStatusDidChangeNotification = Notification . Name ( " NetworkPathMonitor.NetworkPathStatusDidChange " )
5549
5650 /// Initializes a new instance of `NetworkPathMonitor`.
5751 /// - Parameter queue: The queue on which the network path monitor runs. Default is a serial queue with a unique label.
5852 /// - Parameter debounceInterval: Debounce interval. If set to 0, no debounce will be applied. Default is 0 seconds.
59- /// - Parameter ignoreFirstPathUpdate: Ignore first path update. Default is false.
6053 public init ( queue: DispatchQueue = . init( label: " com.networkPathMonitor. \( UUID ( ) ) " ) ,
61- debounceInterval: Interval = . seconds( 0 ) ,
62- ignoreFirstPathUpdate: Bool = false )
54+ debounceInterval: Interval = . seconds( 0 ) )
6355 {
6456 precondition ( debounceInterval. nanoseconds >= 0 , " debounceInterval must be greater than or equal to 0 " )
6557 monitorQueue = queue
66- currentPath = networkMonitor. currentPath
58+ currentPath = NetworkPath ( nwPath : networkMonitor. currentPath)
6759 self . debounceInterval = debounceInterval
68- self . ignoreFirstPathUpdate = ignoreFirstPathUpdate
6960 networkMonitor. pathUpdateHandler = { [ weak self] path in
7061 guard let self else { return }
7162 Task { await self . handlePathUpdate ( path) }
@@ -84,10 +75,10 @@ public actor NetworkPathMonitor {
8475 }
8576
8677 /// Updates the current network path and notifies the handler.
87- private var pathUpdateContinuation : AsyncStream < NWPath > . Continuation ?
78+ private var pathUpdateContinuation : AsyncStream < NetworkPath > . Continuation ?
8879
8980 /// An asynchronous stream of network path updates.
90- public var pathUpdates : AsyncStream < NWPath > {
81+ public var pathUpdates : AsyncStream < NetworkPath > {
9182 AsyncStream { continuation in
9283 self . pathUpdateContinuation = continuation
9384 // When the AsyncStream is cancelled, clean up the continuation
@@ -102,26 +93,22 @@ public actor NetworkPathMonitor {
10293 }
10394
10495 private func handlePathUpdate( _ path: NWPath ) async {
105- currentPath = path
106-
107- // Check if we should ignore the first path update
108- if isFirstUpdate, ignoreFirstPathUpdate {
109- isFirstUpdate = false
110- return
111- }
96+ currentPath. sequence. previousPath = nil // clear previous path avoid infinite reference
97+ let networkPath = NetworkPath ( nwPath: path, sequence: . update( currentPath. sequence. nextIndex, currentPath) )
98+ currentPath = networkPath
11299
113100 debounceTask? . cancel ( )
114101 guard debounceInterval. nanoseconds > 0 else {
115102 // No debounce, yield immediately
116103 debounceTask = nil
117- await yieldNetworkPath ( path )
104+ await yieldNetworkPath ( networkPath )
118105 return
119106 }
120107 // Debounce is active
121108 debounceTask = Task {
122109 do {
123110 try await Task . sleep ( nanoseconds: UInt64 ( self . debounceInterval. nanoseconds) )
124- await self . yieldNetworkPath ( path )
111+ await self . yieldNetworkPath ( networkPath )
125112 } catch is CancellationError {
126113 // Task was cancelled, do nothing
127114 } catch {
@@ -131,10 +118,7 @@ public actor NetworkPathMonitor {
131118 }
132119
133120 // Yield the path update handler
134- private func yieldNetworkPath( _ path: NWPath ) async {
135- // Mark first update as completed when actually notifying
136- isFirstUpdate = false
137-
121+ private func yieldNetworkPath( _ path: NetworkPath ) async {
138122 // Send updates via AsyncStream
139123 pathUpdateContinuation? . yield ( path)
140124
@@ -158,7 +142,7 @@ public actor NetworkPathMonitor {
158142
159143public extension NetworkPathMonitor {
160144 /// A type alias for the network path update handler.
161- typealias PathUpdateHandler = @Sendable ( Network . NWPath ) async -> Void
145+ typealias PathUpdateHandler = @Sendable ( NetworkPath ) async -> Void
162146
163147 /// Starts monitoring the network path.
164148 func fire( ) {
0 commit comments