Skip to content

Instantly share code, notes, and snippets.

@hermanbanken
Created June 5, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hermanbanken/775a9ab30642078cbc7bbc1ada9a20d1 to your computer and use it in GitHub Desktop.
Save hermanbanken/775a9ab30642078cbc7bbc1ada9a20d1 to your computer and use it in GitHub Desktop.
//
// Test.swift
// Q42
//
// Created by Herman Banken on 6/4/19.
//
import Foundation
import Combine
import RxSwift
@available(iOS 13.0, *)
let p = Publishers.Future { (v: @escaping (Result<Int,Error>) -> Void) in
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
v(.success(42))
})
}
func run () {
if #available(iOS 13.0, *) {
let p2 = Publishers.Empty<Int, Error>()
let p4 = Publishers.Sequence<[Int], Error>(sequence: [1,2,3,4])
let p3 = Publishers.Just(42)
p3.sink { (value) in
debugPrint(value)
}
let p5 = p4.merge(with: p2)
let p6 = p5.append(5).allSatisfy { $0 >= 1 }.count()
p6.sink { (value) in
debugPrint(value)
}
.cancel()
}
}
struct Observa : Publisher {
public typealias Failure = Error
public typealias Output = Int
@available(iOS 13.0, *)
public func receive<S>(subscriber: S) where S : Subscriber, Observa.Failure == S.Failure, Observa.Output == S.Input {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
_ = subscriber.receive(42)
subscriber.receive(completion: .finished)
})
}
}
struct MyPublisher : Publisher {
public typealias Failure = Error
public typealias Output = Int
@available(iOS 13.0, *)
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
_ = subscriber.receive(42)
subscriber.receive(completion: .finished)
})
}
}
struct Observ<Element> : Publisher {
@available(iOS 13.0, *)
public func receive<S>(subscriber: S) where S : Subscriber, Observ.Failure == S.Failure, Observ.Output == S.Input {
// self.subs .
}
public typealias Failure = Never
public typealias Output = Element
}
extension Observable : Publisher {
@available(iOS 13.0, *)
public func receive<S>(subscriber: S) where S : Subscriber, Observable.Failure == S.Failure, Observable.Output == S.Input {
_ = self.subscribe(onNext: { (next) in
let demand = subscriber.receive(next)
}, onError: { (err) in
subscriber.receive(completion: .failure(err))
}, onCompleted: {
subscriber.receive(completion: .finished)
})
}
public typealias Failure = Error
public typealias Output = Element
}
// Source: https://developer.apple.com/documentation/swift/cocoa_design_patterns/using_key-value_observing_in_swift
class MyObserver: NSObject {
@objc var objectToObserve: MyObjectToObserve
var observation: NSKeyValueObservation?
init(object: MyObjectToObserve) {
objectToObserve = object
super.init()
observation = observe(
\.objectToObserve.myDate,
options: [.old, .new]
) { object, change in
print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
}
}
}
class MyObjectToObserve: NSObject {
@objc dynamic var myDate = NSDate(timeIntervalSince1970: 0) // 1970
func updateDate() {
myDate = myDate.addingTimeInterval(Double(2 << 30)) // Adds about 68 years.
}
}
class Article {
let id: UUID
let source: URL
var title: String
var body: String
init(title: String, body: String) {
let uuid = UUID()
self.id = uuid
self.source = URL(string: "http://localhost/\(uuid)")!
self.title = title
self.body = body
}
}
@available(iOS 13.0, *)
func run2 () {
// See https://www.swiftbysundell.com/posts/the-power-of-key-paths-in-swift
let article = Article(title: "Test", body: "Lorum ipsum")
let keypath: ReferenceWritableKeyPath<Article,String> = \.title
// The new Combine stuff:
let sink = Subscribers.Assign(object: article, keyPath: keypath)
//
let source = Publishers.Future<String, Never> { subscriber in
DispatchQueue.main.async {
subscriber(.success("42"))
}
}
source.subscribe(sink)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment