Files
T
4d4022e91b Add a whole bunch more to the guides (#64)
* Rebase guides WIP to avoid source changes

* Add prose for debounce and throttle

* Add an alterantive for debounce

* Starts to prose for reductions

* Add effect listings for all types

* Add some examples for reductions

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Channel.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Collections.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Throttle.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Throttle.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Guides/Throttle.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Update Sources/AsyncAlgorithms/AsyncInclusiveReductionsSequence.swift

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Adjust events to elements for channels

* Add a bit more detail to the README and a start to the guide for Task.select

* Chunked updates

* Add a getting started section to the README

* Add some more prose for Channel

* Update Guides/RemoveDuplicates.md

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Apply suggestions from code review

Co-authored-by: Chris Adamson <invalidname@gmail.com>

* Add some brief prose for Compacted

* reductions could be named scan

* spelling and correctness fixes

* Apply @kperryua's suggestion for changing the intro of Compacted

* Add some brief prose for Timer

* Apply suggestions from code review for README

Co-authored-by: Tony Parker <anthony.parker@apple.com>

* Add some brief prose for RemoveDuplicates

* Fleshing out of `Joined`

Co-authored-by: Chris Adamson <invalidname@gmail.com>
Co-authored-by: Kevin Perry <kperry@apple.com>
Co-authored-by: Tony Parker <anthony.parker@apple.com>
2022-03-17 18:40:14 -07:00

2.3 KiB

Timer

[ Source | Tests ]

Introduction

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.

Proposed Solution

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.

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().

public struct AsyncTimerSequence<C: Clock>: AsyncSequence {
  public typealias Element = C.Instant
  
  public struct Iterator: AsyncIteratorProtocol {
    public mutating func next() async -> C.Instant?
  }
  
  public init(
    interval: C.Instant.Duration, 
    tolerance: C.Instant.Duration? = nil, 
    clock: C
  )
  
  public func makeAsyncIterator() -> Iterator
}

extension AsyncTimerSequence where C == SuspendingClock {
  public static func repeating(every interval: Duration, tolerance: Duration? = nil) -> AsyncTimerSequence<SuspendingClock>
}

extension AsyncTimerSequence: Sendable { }
extension AsyncTimerSequence.Iterator: Sendable { }

Since all the types comprising AsyncTimerSequence and it's Iterator are Sendable these types are also `Sendable.

Credits/Inspiration

https://developer.apple.com/documentation/foundation/timer

https://developer.apple.com/documentation/foundation/timer/timerpublisher