A lightweight, modern Swift async/await timer/debounce/throttle implementation for Apple platforms.
- ✅ Swift Concurrency - Built with Swift's modern concurrency model using async/await and actors
- ✅ Thread Safety - Actor-based design ensures thread-safe operation
- ✅ Lightweight - Zero dependencies, minimal footprint
- ✅ Timer Utilities - Built-in
AsyncTimer
with support for one-time and repeating timers with configurable intervals - ✅ Debounce Utilities - Built-in
AsyncDebouncer
with configurable debounce time - ✅ Throttle Utilities - Built-in
AsyncThrottler
with leading/trailing and drift/anti-drift cadence
- Swift 5.10+ / Swift 6.0
- iOS 13.0+
- macOS 10.15+
- macCatalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- visionOS 1.0+
Add AsyncTimer to your project using Swift Package Manager by adding it to your Package.swift
file's dependencies:
dependencies: [
.package(url: "https://github.com/codingiran/AsyncTimer.git", from: "0.0.7")
]
Or add it directly in Xcode:
- Go to File > Add Packages...
- Enter the repository URL:
https://github.com/codingiran/AsyncTimer.git
- Select the version or branch you want to use
import AsyncTimer
// One-time
let once = AsyncTimer(interval: .seconds(1), repeating: false) {
print("fire once")
}
await once.start()
// Repeating (fire immediately on start)
let repeating = AsyncTimer(interval: .seconds(0.5), repeating: true, firesImmediately: true) {
print("tick")
}
await repeating.start()
// ... later
await repeating.stop()
import AsyncTimer
let throttler = AsyncThrottler(
throttleTime: .seconds(0.2),
behavior: .trailingOnly, // .leadingOnly / .leadingAndTrailing
cadence: .antiDrift // .drift for rolling windows
)
// High-frequency calls → at most one execution per window
await throttler.call { print("work") }
import AsyncTimer
let debouncer = AsyncDebouncer(debounceTime: .seconds(0.2))
await debouncer.call { print("fire after quiet period") }
Contributions are welcome! If you find a bug or have a feature request, please open an issue. If you'd like to contribute code, please fork the repository and submit a pull request.
AsyncTimer is available under the MIT license. See the LICENSE file for more info.