Download this file
| 1 | import Foundation |
| 2 | import RastrilloApple |
| 3 | |
| 4 | /// A `NativePathSource` a test drives by hand: `NativeConnectivity(source:)` |
| 5 | /// then sees exactly the path changes the test sends, and none from the |
| 6 | /// machine it runs on. |
| 7 | public final class ScriptedPathSource: NativePathSource, @unchecked Sendable { |
| 8 | private let lock = NSLock() |
| 9 | private var update: (@Sendable (NativePath) -> Void)? |
| 10 | private var initial: NativePath? |
| 11 | |
| 12 | /// `initial` is delivered as soon as a connectivity starts, as NWPathMonitor does. |
| 13 | public init(_ initial: NativePath.Status? = nil) { self.initial = initial.map { NativePath(status: $0) } } |
| 14 | |
| 15 | public func start(_ update: @escaping @Sendable (NativePath) -> Void) { |
| 16 | let first: NativePath? = lock.withLock { self.update = update; return initial } |
| 17 | if let first { update(first) } |
| 18 | } |
| 19 | |
| 20 | public func cancel() { lock.withLock { update = nil } } |
| 21 | |
| 22 | public func send(_ status: NativePath.Status, isExpensive: Bool = false, isConstrained: Bool = false) { |
| 23 | let path = NativePath(status: status, isExpensive: isExpensive, isConstrained: isConstrained) |
| 24 | lock.withLock { update }?(path) |
| 25 | } |
| 26 | } |
| 27 | |