This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension View { | |
func streamChanges<Value: Equatable & Sendable>( | |
of value: Value, | |
provideStream: @escaping @Sendable (AsyncStream<Value>) async -> Void | |
) -> some View { | |
modifier(ChangeStream(value: value, provideStream: provideStream)) | |
} | |
} | |
private struct ChangeStream<Value: Equatable & Sendable>: ViewModifier { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct SelfSizingSheetPresentation: ViewModifier { | |
private enum BoundsPreference: PreferenceKey { | |
static let defaultValue: Anchor<CGRect>? = nil | |
static func reduce(value: inout Value, nextValue: () -> Value) { | |
value = nextValue() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// An AsyncSequence that iterates at most once every `interval` | |
public struct AsyncIntervalSequence<Base: AsyncSequence, C: Clock> { | |
public let base: Base | |
public let interval: C.Instant.Duration | |
public let clock: C | |
public init(_ base: Base, interval: C.Instant.Duration, clock: C) { | |
self.base = base | |
self.interval = interval | |
self.clock = clock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// An AsyncSequence that will terminate with a `Timeout` error if | |
/// iteration time exceeds the provided `timeout`. | |
struct AsyncTimeoutSequence<Base: AsyncSequence, C: Clock> { | |
struct Timeout: Error {} | |
let base: Base | |
let timeout: C.Instant.Duration | |
let clock: C | |
init(_ base: Base, timeout: C.Instant.Duration, clock: C) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct RecursiveModifier<Data: Collection, Transformed: View>: ViewModifier { | |
let data: Data | |
let transform: (Data.Element, AnyView) -> Transformed | |
func body(content: Content) -> some View { | |
var subsequence = data[...] | |
if let first = subsequence.popFirst() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct AsyncWrappedSequence<Base: Sequence>: AsyncSequence { | |
typealias Element = Base.Element | |
struct AsyncIterator<BaseIterator>: AsyncIteratorProtocol where BaseIterator == Base.Iterator { | |
var base: BaseIterator | |
mutating func next() async -> BaseIterator.Element? { | |
base.next() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct AnyAsyncSequence<Element>: AsyncSequence { | |
struct AnyAsyncIterator: AsyncIteratorProtocol { | |
private let _next: () async throws -> Element? | |
init<Base: AsyncSequence>(_ base: Base) where Base.Element == Element { | |
var baseIterator = base.makeAsyncIterator() | |
self._next = { try await baseIterator.next() } | |
} | |
func next() async throws -> Element? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct InterleavedSequence<First: Sequence, Second: Sequence>: Sequence where First.Element == Second.Element { | |
let first: First | |
let second: Second | |
init(first: First, second: Second) { | |
self.first = first | |
self.second = second | |
} | |
func makeIterator() -> Iterator { |