Skip to content

Instantly share code, notes, and snippets.

@jcampbell05
Created April 22, 2020 22:30
Show Gist options
  • Save jcampbell05/a286bb49049b3feec245aac199b1e89e to your computer and use it in GitHub Desktop.
Save jcampbell05/a286bb49049b3feec245aac199b1e89e to your computer and use it in GitHub Desktop.
import Foundation
import Combine
import OpenCombine
enum CombineIdentifierWrapper {
@available(iOS 13.0, *)
case combine(Combine.CombineIdentifier)
case open(OpenCombine.CombineIdentifier)
init() {
if #available(iOS 13.0, *) {
self = .combine(Combine.CombineIdentifier())
} else {
self = .open(OpenCombine.CombineIdentifier())
}
}
func unwrap() -> Any {
switch self {
case .combine(let me):
return me
case .open(let me):
return me
}
}
}
@available(iOS 13.0, *)
struct DataSubscriptionCombine: Combine.Subscription {
var combineIdentifier: Combine.CombineIdentifier
func request(_ demand: Combine.Subscribers.Demand) {
}
func cancel() {
}
}
struct DataSubscriptionOpen: OpenCombine.Subscription {
var combineIdentifier: OpenCombine.CombineIdentifier
func request(_ demand: OpenCombine.Subscribers.Demand) {
}
func cancel() {
}
}
enum DataSubscriptionWrapper {
@available(iOS 13.0, *)
case combine(DataSubscriptionCombine)
case open(DataSubscriptionOpen)
init(combineIdentifier: CombineIdentifierWrapper) {
if #available(iOS 13.0, *) {
let identifier = combineIdentifier.unwrap() as! Combine.CombineIdentifier
self = .combine(DataSubscriptionCombine(combineIdentifier: identifier))
} else {
let identifier = combineIdentifier.unwrap() as! OpenCombine.CombineIdentifier
self = .open(DataSubscriptionOpen(combineIdentifier: identifier))
}
}
func unwrap() -> Any {
switch self {
case .combine(let me):
return me
case .open(let me):
return me
}
}
}
@available(iOS 13.0, *)
struct DataSubscriberCombine<O,F>: Combine.Subscriber where F:Error {
typealias Input = O
typealias Failure = F
var combineIdentifier: Combine.CombineIdentifier
func receive(subscription: Combine.Subscription) {
}
func receive(_ input: O) -> Combine.Subscribers.Demand {
return .none
}
func receive(completion: Combine.Subscribers.Completion<F>) {
}
}
struct DataSubscriberOpen<O,F>: OpenCombine.Subscriber where F:Error {
typealias Input = O
typealias Failure = F
var combineIdentifier: OpenCombine.CombineIdentifier
func receive(subscription: OpenCombine.Subscription) {
}
func receive(_ input: O) -> OpenCombine.Subscribers.Demand {
return .none
}
func receive(completion: OpenCombine.Subscribers.Completion<F>) {
}
}
enum DataSubscriberWrapper<O,F> where F:Error {
@available(iOS 13.0, *)
case combine(DataSubscriberCombine<O,F>)
case open(DataSubscriberOpen<O,F>)
init(combineIdentifier: CombineIdentifierWrapper) {
if #available(iOS 13.0, *) {
let identifier = combineIdentifier.unwrap() as! Combine.CombineIdentifier
self = .combine(DataSubscriberCombine(combineIdentifier: identifier))
} else {
let identifier = combineIdentifier.unwrap() as! OpenCombine.CombineIdentifier
self = .open(DataSubscriberOpen(combineIdentifier: identifier))
}
}
func unwrap() -> Any {
switch self {
case .combine(let me):
return me
case .open(let me):
return me
}
}
}
@available(iOS 13.0, *)
struct DataPublisherCombine<O,F>: Combine.Publisher where F:Error {
typealias Output = O
typealias Failure = F
func receive<S: Combine.Subscriber>(subscriber: S) where
DataPublisherCombine.Failure == S.Failure, DataPublisherCombine.Output == S.Input {
}
}
struct DataPublisherOpen<O,F>: OpenCombine.Publisher where F:Error {
typealias Output = O
typealias Failure = F
func receive<S: OpenCombine.Subscriber>(subscriber: S) where
DataPublisherOpen.Failure == S.Failure, DataPublisherOpen.Output == S.Input {
}
}
enum DataPublisherWrapper<O,F> where F:Error {
@available(iOS 13.0, *)
case combine(DataPublisherCombine<O,F>)
case open(DataPublisherOpen<O,F>)
init() {
if #available(iOS 13.0, *) {
self = .combine(DataPublisherCombine())
} else {
self = .open(DataPublisherOpen())
}
}
func receive(subscriber: DataSubscriberWrapper<O, F>) {
let subscripitonIdentifier = CombineIdentifierWrapper()
let subscription = DataSubscriptionWrapper(combineIdentifier: subscripitonIdentifier)
if #available(iOS 13.0, *) {
let identifier = subscriber.unwrap() as! DataSubscriberCombine<O,F>
let combineSub = subscription.unwrap() as! Combine.Subscription
identifier.receive(subscription: combineSub)
} else {
let identifier = subscriber.unwrap() as! DataSubscriberOpen<O,F>
let combineSub = subscription.unwrap() as! OpenCombine.Subscription
identifier.receive(subscription: combineSub)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment