//
//  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()
    }
    
}
