Skip to content

Commit fd2812d

Browse files
committed
Add some brief prose for Timer
1 parent 14b0f82 commit fd2812d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Guides/Timer.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99

1010
## Introduction
1111

12+
Producing elements at regular intervals can be useful for composing with other algorithms. These can range from invoking code at specific times to using those regular intervals as a delimter of events. There are other cases this exists in APIs however those do not currently interact with Swift concurrency. These existing APIs are ones like `Timer` or `DispatchTimer` but are bound to internal clocks that are not extensible.
13+
1214
## Proposed Solution
1315

16+
We propose to add a new type; `AsyncTimerSequence` which utilizes the new `Clock`, `Instant` and `Duration` types. This allows the interaction of the timer to custom implementations of types adopting `Clock`.
17+
18+
This asynchronous sequence will produce elements of the clock's `Instant` type after the interval has elapsed. That instant will be the `now` at the time that the sleep has resumed. For each invocation to `next()` the `AsyncTimerSequence.Iterator` will calculate the next deadline to resume and pass that and the tolerance to the clock. If at any point in time the task executing that iteration is cancelled the iteration will return `nil` from the call to `next()`.
19+
1420
```swift
1521
public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
1622
public typealias Element = C.Instant
@@ -27,10 +33,19 @@ public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
2733

2834
public func makeAsyncIterator() -> Iterator
2935
}
30-
```
3136

32-
## Detailed Design
37+
extension AsyncTimerSequence where C == SuspendingClock {
38+
public static func repeating(every interval: Duration, tolerance: Duration? = nil) -> AsyncTimerSequence<SuspendingClock>
39+
}
3340

34-
## Alternatives Considered
41+
extension AsyncTimerSequence: Sendable { }
42+
extension AsyncTimerSequence.Iterator: Sendable { }
43+
```
44+
45+
Since all the types comprising `AsyncTimerSequence` and it's `Iterator` are `Sendable` these types are also `Sendable.
3546

3647
## Credits/Inspiration
48+
49+
https://developer.apple.com/documentation/foundation/timer
50+
51+
https://developer.apple.com/documentation/foundation/timer/timerpublisher

0 commit comments

Comments
 (0)