Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Conversation

@reddavis
Copy link
Owner

extension AnyAsyncSequenceable {
    
    // MARK: Builders
    
    /// Create a simple effect that emits a single event.
    /// - Parameter closure: An async closure that returns an event.
    /// - Returns: A type erased async sequence.
    public static func effect(_ closure: @escaping () async -> Element) -> Self {
        AsyncStream {
            let event = await closure()
            $0.finish(with: event)
        }.eraseToAnyAsyncSequenceable()
    }
    
    /// Creates a fire and forget async sequence. This sequence will not emit any events
    /// and will finish as soon as the provided closure has been executed.
    /// - Parameter closure: An async closure.
    /// - Returns: A type erased async sequence.
    public static func fireAndForget(_ closure: @escaping () async -> Void) -> Self {
        AsyncStream {
            await closure()
            $0.finish()
        }.eraseToAnyAsyncSequenceable()
    }
}

@reddavis reddavis merged commit 9cdb5f7 into main Feb 27, 2022
@reddavis reddavis deleted the feature/effect-helpers branch February 27, 2022 18:37
reddavis added a commit that referenced this pull request Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants