Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Couple of methods to bridge ReactiveSwift with SwiftUI
import Combine
import ReactiveSwift
import SwiftUI
class AnySubscription: Subscription {
private let cancelable: Cancellable
init(cancelable: Cancellable) {
self.cancelable = cancelable
}
init(_ cancel: @escaping () -> Void) {
self.cancelable = AnyCancellable(cancel)
}
func request(_ demand: Subscribers.Demand) {}
func cancel() {
self.cancelable.cancel()
}
}
extension Property: BindingConvertible {
public var binding: Binding<Value> {
Binding<Value>(getValue: { self.value }, setValue: {_ in })
}
}
extension Property: Publisher {
public typealias Output = Value
public typealias Failure = Never
public func receive<S>(subscriber: S) where S : Subscriber, Property.Failure == S.Failure, Property.Output == S.Input {
self.signal.observe { event in
switch event {
case .completed, .interrupted:
subscriber.receive(completion: .finished)
case let .value(value):
_ = subscriber.receive(value)
}
}
}
}
extension Property: BindableObject {
public typealias PublisherType = Property
public var didChange: Property {
self
}
}
extension MutableProperty: BindingConvertible {
public var binding: Binding<Value> {
Binding<Value>(getValue: { self.value }, setValue: { self.value = $0 })
}
}
extension MutableProperty: Publisher {
public typealias Output = Value
public typealias Failure = Never
public func receive<S>(subscriber: S) where S : Subscriber, MutableProperty.Failure == S.Failure, MutableProperty.Output == S.Input {
self.signal.observe { event in
switch event {
case .completed, .interrupted:
subscriber.receive(completion: .finished)
case let .value(value):
_ = subscriber.receive(value)
}
}
}
}
extension MutableProperty: BindableObject {
public typealias PublisherType = MutableProperty
public var didChange: MutableProperty {
self
}
}
extension SignalProducer {
var publisher: some Publisher {
return AnyPublisher<Value, Error> { subscriber in
let serial = SerialDisposable()
subscriber.receive(subscription: AnySubscription(serial.dispose))
serial.inner = self.start { event in
switch event {
case .completed, .interrupted:
subscriber.receive(completion: .finished)
case let .value(value):
_ = subscriber.receive(value)
case let .failed(error):
subscriber.receive(completion: .failure(error))
}
}
}
}
}
extension Signal {
var publisher: some Publisher {
return AnyPublisher<Value, Error> { subscriber in
let serial = SerialDisposable()
subscriber.receive(subscription: AnySubscription(serial.dispose))
serial.inner = self.observe { event in
switch event {
case .completed, .interrupted:
subscriber.receive(completion: .finished)
case let .value(value):
_ = subscriber.receive(value)
case let .failed(error):
subscriber.receive(completion: .failure(error))
}
}
}
}
}
@NicholasTD07

This comment has been minimized.

Copy link

NicholasTD07 commented Dec 20, 2019

Nice work! Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.