Skip to content

Instantly share code, notes, and snippets.

@GuilhE
Last active April 14, 2022 12:12
Show Gist options
  • Save GuilhE/f5e09be147a218e641a790897d8c7261 to your computer and use it in GitHub Desktop.
Save GuilhE/f5e09be147a218e641a790897d8c7261 to your computer and use it in GitHub Desktop.
Medium articles - KMM with MVI+FSM
import Combine
import shared
struct FlowPublisher<T: Any>: Publisher {
public typealias Output = T
public typealias Failure = Never
private let flow: Kotlinx_coroutines_coreFlow
public init(flow: Kotlinx_coroutines_coreFlow) {
self.flow = flow
}
public func receive<S: Subscriber>(subscriber: S) where S.Input == T, S.Failure == Failure {
subscriber.receive(subscription: FlowSubscription(flow: flow, subscriber: subscriber))
}
final class FlowSubscription<S: Subscriber>: Subscription where S.Input == T, S.Failure == Failure {
private var subscriber: S?
private var job: Kotlinx_coroutines_coreJob?
private let flow: Kotlinx_coroutines_coreFlow
init(flow: Kotlinx_coroutines_coreFlow, subscriber: S) {
self.flow = flow
self.subscriber = subscriber
job = SubscribeKt.subscribe(
flow,
onEach: { item in if let item = item as? T { _ = subscriber.receive(item) }},
onComplete: { subscriber.receive(completion: .finished) },
onThrow: { error in debugPrint(error) }
)
}
func cancel() {
subscriber = nil
job?.cancel(cause: nil)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment