Skip to content

Commit

Permalink
Rethrows & refactoring (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
reddavis authored Jun 26, 2022
1 parent a6e759c commit aaf2e9f
Show file tree
Hide file tree
Showing 25 changed files with 38 additions and 97 deletions.
26 changes: 26 additions & 0 deletions Asynchrone/Source/Common/RethrowingAccessor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation

@rethrows
protocol RethrowingAccessor {
associatedtype T
func _value() throws -> T
}

extension RethrowingAccessor {
func _forceRethrowError() rethrows {
_ = try _retrowValue()
fatalError("No error")
}

func _retrowValue() rethrows -> T {
try self._value()
}
}

// MARK: Result

extension Result: RethrowingAccessor {
func _value() throws -> Success {
try self.get()
}
}
7 changes: 0 additions & 7 deletions Asynchrone/Source/Sequences/AnyAsyncSequenceable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
///
/// If the async sequence that you wish to type erase can throw, then use `AnyThrowingAsyncSequenceable`.
public struct AnyAsyncSequenceable<Element>: AsyncSequence {

// Private
private var _next: () async -> Element?
private var _makeAsyncIterator: () -> Self

Expand Down Expand Up @@ -38,21 +36,16 @@ public struct AnyAsyncSequenceable<Element>: AsyncSequence {
// MARK: AsyncIteratorProtocol

extension AnyAsyncSequenceable: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
/// - Returns: The next element or `nil` if the end of the sequence is reached.
public mutating func next() async -> Element? {
await self._next()
}
}




// MARK: Erasure

extension AsyncSequence {

/// Creates a type erasing async sequence.
///
/// If the async sequence that you wish to type erase can throw,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
///
/// If the async sequence that you wish to type erase doesn't throw, then use `AnyAsyncSequenceable`.
public struct AnyThrowingAsyncSequenceable<Element>: AsyncSequence {

// Private
private var _next: () async throws -> Element?
private var _makeAsyncIterator: () -> Self

Expand Down Expand Up @@ -38,17 +36,13 @@ public struct AnyThrowingAsyncSequenceable<Element>: AsyncSequence {
// MARK: AsyncIteratorProtocol

extension AnyThrowingAsyncSequenceable: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
/// - Returns: The next element or `nil` if the end of the sequence is reached.
public mutating func next() async throws -> Element? {
try await self._next()
}
}




// MARK: Erasure

extension AsyncSequence {
Expand Down
1 change: 0 additions & 1 deletion Asynchrone/Source/Sequences/AsyncSequenceCompletion.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// Describes how an async sequence has completed.
public enum AsyncSequenceCompletion<Failure: Error> {

/// The async sequence finished normally.
case finished

Expand Down
7 changes: 0 additions & 7 deletions Asynchrone/Source/Sequences/ChainAsyncSequenceable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
/// // 9
/// ```
public struct ChainAsyncSequence<P: AsyncSequence, Q: AsyncSequence>: AsyncSequence where P.Element == Q.Element {

/// The kind of elements streamed.
public typealias Element = P.Element

Expand Down Expand Up @@ -81,9 +80,6 @@ public struct ChainAsyncSequence<P: AsyncSequence, Q: AsyncSequence>: AsyncSeque
// MARK: AsyncIteratorProtocol

extension ChainAsyncSequence: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
/// - Returns: The next element or `nil` if the end of the sequence is reached.
public mutating func next() async rethrows -> Element? {
if let element = try await self.iteratorP.next() {
return element
Expand All @@ -93,12 +89,9 @@ extension ChainAsyncSequence: AsyncIteratorProtocol {
}
}



// MARK: Chain

extension AsyncSequence {

/// An asynchronous sequence that chains two async sequences.
///
/// The combined sequence first emits the all the values from the first sequence
Expand Down
5 changes: 0 additions & 5 deletions Asynchrone/Source/Sequences/CombineLatest3AsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
/// // (4, 9, 11)
/// ```
public struct CombineLatest3AsyncSequence<P: AsyncSequence, Q: AsyncSequence, R: AsyncSequence>: AsyncSequence {

/// The kind of elements streamed.
public typealias Element = (P.Element, Q.Element, R.Element)

Expand Down Expand Up @@ -93,7 +92,6 @@ public struct CombineLatest3AsyncSequence<P: AsyncSequence, Q: AsyncSequence, R:
// MARK: AsyncIteratorProtocol

extension CombineLatest3AsyncSequence: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
///
/// Continues to call `next()` on it's base iterator and iterator of
Expand Down Expand Up @@ -134,12 +132,9 @@ extension CombineLatest3AsyncSequence: AsyncIteratorProtocol {
}
}



// MARK: Combine latest

extension AsyncSequence {

/// Combine three async sequences.
///
/// The combined sequence emits a tuple of the most-recent elements from each sequence
Expand Down
5 changes: 0 additions & 5 deletions Asynchrone/Source/Sequences/CombineLatestAsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
/// // (4, 9)
/// ```
public struct CombineLatestAsyncSequence<P: AsyncSequence, Q: AsyncSequence>: AsyncSequence {

/// The kind of elements streamed.
public typealias Element = (P.Element, Q.Element)

Expand Down Expand Up @@ -79,7 +78,6 @@ public struct CombineLatestAsyncSequence<P: AsyncSequence, Q: AsyncSequence>: As
// MARK: AsyncIteratorProtocol

extension CombineLatestAsyncSequence: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
///
/// Continues to call `next()` on it's base iterator and iterator of
Expand Down Expand Up @@ -116,12 +114,9 @@ extension CombineLatestAsyncSequence: AsyncIteratorProtocol {
}
}



// MARK: Combine latest

extension AsyncSequence {

/// Combine with an additional async sequence to produce a `AsyncCombineLatest2Sequence`.
///
/// The combined sequence emits a tuple of the most-recent elements from each sequence
Expand Down
3 changes: 0 additions & 3 deletions Asynchrone/Source/Sequences/CurrentElementAsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
/// // 4
/// ```
public actor CurrentElementAsyncSequence<Element>: AsyncSequence {

/// The element wrapped by this async sequence, emitted as a new element whenever it changes.
public private(set) var element: Element

Expand Down Expand Up @@ -72,8 +71,6 @@ public actor CurrentElementAsyncSequence<Element>: AsyncSequence {
}
}



// MARK: Stream

fileprivate struct _Stream<Element>: AsyncSequence {
Expand Down
2 changes: 0 additions & 2 deletions Asynchrone/Source/Sequences/DebounceAsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ extension DebounceAsyncSequence {
}
case .sleep:
self.resultTask = resultTask

//
if let result = lastResult {
return try result._rethrowGet()
}
Expand Down
11 changes: 2 additions & 9 deletions Asynchrone/Source/Sequences/DelayAsyncSequence.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Foundation


/// Delays emission of all elements by the provided interval.
///
/// ```swift
Expand All @@ -22,7 +21,6 @@ import Foundation
/// // 2 - 1.5
/// ```
public struct DelayAsyncSequence<T: AsyncSequence>: AsyncSequence {

/// The kind of elements streamed.
public typealias Element = T.Element

Expand Down Expand Up @@ -59,24 +57,19 @@ public struct DelayAsyncSequence<T: AsyncSequence>: AsyncSequence {
// MARK: AsyncIteratorProtocol

extension DelayAsyncSequence: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
/// - Returns: The next element or `nil` if the end of the sequence is reached.
public mutating func next() async throws -> Element? {
public mutating func next() async rethrows -> Element? {
defer { self.lastEmission = Date() }

let lastEmission = self.lastEmission ?? Date()
let delay = self.interval - Date().timeIntervalSince(lastEmission)
if delay > 0 {
try await Task.sleep(seconds: delay)
try? await Task.sleep(seconds: delay)
}

return try await self.iterator.next()
}
}



// MARK: Delay

extension AsyncSequence {
Expand Down
3 changes: 0 additions & 3 deletions Asynchrone/Source/Sequences/Empty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
/// // Finished
/// ```
public struct Empty<Element>: AsyncSequence {

// Private
private let completeImmediately: Bool

// MARK: Initialization
Expand All @@ -43,7 +41,6 @@ public struct Empty<Element>: AsyncSequence {
// MARK: AsyncIteratorProtocol

extension Empty: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
///
/// Because this is an empty sequence, this will always be nil.
Expand Down
7 changes: 2 additions & 5 deletions Asynchrone/Source/Sequences/Fail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
/// // Error!
/// ```
public struct Fail<Element, Failure>: AsyncSequence where Failure: Error {

// Private
let error: Failure
var hasThownError = false
private let error: Failure
private var hasThownError = false

// MARK: Initialization

Expand All @@ -43,7 +41,6 @@ public struct Fail<Element, Failure>: AsyncSequence where Failure: Error {
// MARK: AsyncIteratorProtocol

extension Fail: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
/// - Returns: The next element or `nil` if the end of the sequence is reached.
public mutating func next() async throws -> Element? {
Expand Down
7 changes: 2 additions & 5 deletions Asynchrone/Source/Sequences/Just.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
/// // 1
/// ```
public struct Just<Element>: AsyncSequence {

// Private
let element: Element
var emittedElement = false
private let element: Element
private var emittedElement = false

// MARK: Initialization

Expand All @@ -37,7 +35,6 @@ public struct Just<Element>: AsyncSequence {
// MARK: AsyncIteratorProtocol

extension Just: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
/// - Returns: The next element or `nil` if the end of the sequence is reached.
public mutating func next() async -> Element? {
Expand Down
1 change: 0 additions & 1 deletion Asynchrone/Source/Sequences/Merge3AsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
/// // 4
/// ```
public struct Merge3AsyncSequence<T: AsyncSequence>: AsyncSequence {

/// The kind of elements streamed.
public typealias Element = T.Element

Expand Down
1 change: 0 additions & 1 deletion Asynchrone/Source/Sequences/MergeAsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
/// // 9
/// ```
public struct MergeAsyncSequence<T: AsyncSequence>: AsyncSequence {

/// The kind of elements streamed.
public typealias Element = T.Element

Expand Down
2 changes: 0 additions & 2 deletions Asynchrone/Source/Sequences/PassthroughAsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
/// // 2
/// ```
public struct PassthroughAsyncSequence<Element>: AsyncSequence {

// Private
private var stream: AsyncStream<Element>!
private var continuation: AsyncStream<Element>.Continuation!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
/// // 3
/// ```
public struct RemoveDuplicatesAsyncSequence<Base: AsyncSequence>: AsyncSequence where Base.Element: Equatable {

/// The kind of elements streamed.
public typealias Element = Base.Element

Expand Down Expand Up @@ -61,7 +60,6 @@ public struct RemoveDuplicatesAsyncSequence<Base: AsyncSequence>: AsyncSequence
// MARK: AsyncIteratorProtocol

extension RemoveDuplicatesAsyncSequence: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
///
/// Continues to call `next()` on it's base iterator and discard the
Expand Down Expand Up @@ -90,12 +88,9 @@ extension RemoveDuplicatesAsyncSequence: AsyncIteratorProtocol {
}
}



// MARK: Remove duplicates

extension AsyncSequence where Element: Equatable {

/// Emits only elements that don't match the previous element.
/// - Returns: A `AsyncRemoveDuplicatesSequence` instance.
public func removeDuplicates() -> RemoveDuplicatesAsyncSequence<Self> {
Expand Down
5 changes: 0 additions & 5 deletions Asynchrone/Source/Sequences/ReplaceErrorAsyncSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
/// // 0
/// ```
public struct ReplaceErrorAsyncSequence<Base: AsyncSequence>: AsyncSequence {

/// The kind of elements streamed.
public typealias Element = Base.Element

Expand Down Expand Up @@ -47,7 +46,6 @@ public struct ReplaceErrorAsyncSequence<Base: AsyncSequence>: AsyncSequence {
// MARK: AsyncIteratorProtocol

extension ReplaceErrorAsyncSequence: AsyncIteratorProtocol {

/// Produces the next element in the sequence.
/// - Returns: The next element or `nil` if the end of the sequence is reached.
public mutating func next() async -> Element? {
Expand All @@ -59,12 +57,9 @@ extension ReplaceErrorAsyncSequence: AsyncIteratorProtocol {
}
}



// MARK: Replace error

extension AsyncSequence {

/// Replaces any errors in the async sequence with the provided element.
///
/// ```swift
Expand Down
Loading

0 comments on commit aaf2e9f

Please sign in to comment.