punchnet-macos/Tun/Punchnet/SDLAsyncTimerStream.swift
2026-02-25 00:25:58 +08:00

33 lines
688 B
Swift

//
// SDLAsyncTimerStream.swift
// Tun
//
// Created by on 2026/2/3.
//
import Foundation
class SDLAsyncTimerStream {
let timer: DispatchSourceTimer
let stream: AsyncStream<Void>
private let cont: AsyncStream<Void>.Continuation
init() {
self.timer = DispatchSource.makeTimerSource(queue: .global())
(stream, cont) = AsyncStream.makeStream(of: Void.self)
}
func start(interval: DispatchTimeInterval) {
timer.schedule(deadline: .now(), repeating: interval)
timer.setEventHandler {
self.cont.yield()
}
timer.resume()
}
deinit {
self.timer.cancel()
}
}