Skip to content

First pass at some documentation for debounce #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Sources/AsyncAlgorithms/AsyncDebounceSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@
//===----------------------------------------------------------------------===//

extension AsyncSequence {
/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
/// has elapsed by using a spectified Clock.
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
public func debounce<C: Clock>(for interval: C.Instant.Duration, tolerance: C.Instant.Duration? = nil, clock: C) -> AsyncDebounceSequence<Self, C> {
AsyncDebounceSequence(self, interval: interval, tolerance: tolerance, clock: clock)
}

/// Creates an asynchronous sequence that emits the latest element after a given quiescence period
/// has elapsed.
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
public func debounce(for interval: Duration, tolerance: Duration? = nil) -> AsyncDebounceSequence<Self, ContinuousClock> {
debounce(for: interval, tolerance: tolerance, clock: .continuous)
}
}

/// An `AsyncSequence` that emits the latest element after a given quiescence period
/// has elapsed.
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
public struct AsyncDebounceSequence<Base: AsyncSequence, C: Clock>: Sendable
where Base.AsyncIterator: Sendable, Base.Element: Sendable, Base: Sendable {
Expand All @@ -41,6 +47,7 @@ public struct AsyncDebounceSequence<Base: AsyncSequence, C: Clock>: Sendable
extension AsyncDebounceSequence: AsyncSequence {
public typealias Element = Base.Element

/// The iterator for a `AsyncDebounceSequence` instance.
public struct Iterator: AsyncIteratorProtocol, Sendable {
enum Partial: Sendable {
case sleep
Expand Down