You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lightweight, modern Swift async/await timer implementation for Apple platforms. AsyncTimer provides a clean, actor-based API for scheduling one-time and repeating tasks with precise control over timing, priorities, and cancellation.
8
+
A lightweight, modern Swift async/await timer/debounce/throttle implementation for Apple platforms.
9
9
10
10
## Features
11
11
12
12
- ✅ **Swift Concurrency** - Built with Swift's modern concurrency model using async/await and actors
13
-
- ✅ **Flexible Timing** - Support for one-time and repeating timers with configurable intervals
14
-
- ✅ **Task Priorities** - Set execution priorities for your timer tasks
15
-
- ✅ **Immediate Firing Option** - Configure timers to fire immediately or after the first interval
- **interval**: The interval at which the timer fires (in seconds)
204
-
- **priority**: The priority of the task (default: `.medium`)
205
-
- **repeating**: Whether the timer should repeat (default: `false`)
206
-
- **firesImmediately**: Whether the timer should fire immediately upon starting (default: `true`, only effective when `repeating` is `true`)
207
-
- **handler**: The handler that is called when the timer fires
208
-
- **cancelHandler**: The handler that is called when the timer is cancelled (optional)
209
-
210
-
#### Properties
211
79
212
-
```swift
213
-
public var isRunning: Bool
214
-
```
215
-
216
-
Whether the timer is currently running.
217
-
218
-
#### Methods
219
-
220
-
```swift
221
-
public func start()
222
-
```
223
-
224
-
Starts the timer. If the timer is already running, it will be stopped and restarted.
225
-
226
-
```swift
227
-
public func stop()
228
-
```
229
-
230
-
Stops the timer.
231
-
232
-
```swift
233
-
public func restart()
80
+
// High-frequency calls → at most one execution per window
81
+
await throttler.call { print("work") }
234
82
```
235
83
236
-
Restarts the timer (equivalent to calling `stop()` followed by `start()`).
84
+
### AsyncDebouncer
237
85
238
86
```swift
239
-
public func setInterval(_ newInterval: TimeInterval)
240
-
```
241
-
242
-
Modifies the interval of the timer. This will also restart the timer if it's currently running.
243
-
244
-
```swift
245
-
public static func sleep(_ interval: TimeInterval) async throws
246
-
```
247
-
248
-
Utility method to sleep for the specified interval.
249
-
250
-
## Advanced Usage
251
-
252
-
### Handling Concurrency
253
-
254
-
Since `AsyncTimer` is implemented as an actor, all its methods are automatically thread-safe. You can safely call methods from different tasks without worrying about race conditions.
255
-
256
-
```swift
257
-
let timer = AsyncTimer(interval: 1.0, repeating: true) {
258
-
print("Timer fired!")
259
-
}
260
-
261
-
// These can be called from different tasks safely
262
-
Task {
263
-
await timer.start()
264
-
}
87
+
importAsyncTimer
265
88
266
-
Task {
267
-
tryawait Task.sleep(for: .seconds(5))
268
-
await timer.stop()
269
-
}
89
+
let debouncer =AsyncDebouncer(debounceTime: .seconds(0.2))
90
+
await debouncer.call { print("fire after quiet period") }
270
91
```
271
92
272
93
## Contributing
@@ -275,4 +96,4 @@ Contributions are welcome! If you find a bug or have a feature request, please o
275
96
276
97
## License
277
98
278
-
AsyncTimer is available under the MIT license. See the LICENSE file for more info.
99
+
AsyncTimer is available under the MIT license. See the LICENSE file for more info.
0 commit comments