Created
January 21, 2025 19:10
-
-
Save cobbal/bbf69018506a6ec89ddc4e52c5a25ec9 to your computer and use it in GitHub Desktop.
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
public struct SwiftConcurrencyPublisher<Output: Sendable, Failure: Sendable & Error> : Publisher { | |
private let subject: Publishers.HandleEvents<PassthroughSubject<Output, Failure>> | |
public init(body: @Sendable @escaping () async -> Result<Output, Failure>) { | |
let subject = PassthroughSubject<Output, Failure>() | |
let task = Task { | |
switch await body() { | |
case .success(let success): | |
subject.send(success) | |
subject.send(completion: .finished) | |
case .failure(let failure): | |
subject.send(completion: .failure(failure)) | |
} | |
} | |
self.subject = subject.handleEvents(receiveCancel: { task.cancel() }) | |
} | |
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input { | |
subject.receive(subscriber: subscriber) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment