Is there something like Timer (as per API) that's compatible with actors?
I cooked this one myself:
final class AsyncTimer: @unchecked Sendable {
var task: Task<Void, Never>!
func invalidate() {
task.cancel()
}
@discardableResult static func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @Sendable @isolated(any) @escaping (AsyncTimer) async -> Void) -> AsyncTimer {
let timer = AsyncTimer()
timer.task = Task {
if repeats {
while !Task.isCancelled {
try? await Task.sleep(for: .seconds(interval))
await block(timer)
}
} else {
try? await Task.sleep(for: .seconds(interval))
if !Task.isCancelled {
await block(timer)
}
}
}
return timer
}
}
which seems to be working alright (as in a "drop-in" replacement for the Timer API compatible with actors) however if there's something like this already in the standard library I would prefer using that instead.