46 lines
987 B
Swift
46 lines
987 B
Swift
//
|
|
// SDLPathMonitor.swift
|
|
// punchnet
|
|
//
|
|
// Created by 安礼成 on 2026/4/29.
|
|
//
|
|
import Foundation
|
|
import Network
|
|
|
|
final class SDLPathMonitor {
|
|
private let monitor = NWPathMonitor()
|
|
private let queue = DispatchQueue(label: "com.sdlan.path-monitor")
|
|
private let debouncer = SDLPathDebounceActor(delay: .seconds(2))
|
|
|
|
private var isStarted: Bool = false
|
|
|
|
func start() {
|
|
guard !isStarted else {
|
|
return
|
|
}
|
|
|
|
self.isStarted = true
|
|
monitor.pathUpdateHandler = { path in
|
|
let snapshot = SDLPathSnapshot(path)
|
|
|
|
Task {
|
|
await self.debouncer.submit(snapshot)
|
|
}
|
|
}
|
|
|
|
monitor.start(queue: queue)
|
|
}
|
|
|
|
func statusStream() -> AsyncStream<NWPath.Status> {
|
|
return self.debouncer.statusStream
|
|
}
|
|
|
|
func stop() {
|
|
Task {
|
|
await debouncer.stop()
|
|
}
|
|
monitor.cancel()
|
|
}
|
|
|
|
}
|