Created
June 27, 2020 11:55
-
-
Save zafarivaev/64f48ec746996a0e76fe3b4a69a7e696 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
import Foundation | |
import Combine | |
var subscriptions = Set<AnyCancellable>() | |
// MARK: - prepend | |
func prependOutputExample() { | |
let stringPublisher = ["World!"].publisher | |
stringPublisher | |
.prepend("Hello") | |
.sink(receiveValue: { print($0) }) | |
.store(in: &subscriptions) | |
} | |
func prependPublisherExample() { | |
let subject = PassthroughSubject<String, Never>() | |
let stringPublisher = ["Break things!"].publisher | |
stringPublisher | |
.prepend(subject) | |
.sink(receiveValue: { print($0) }) | |
.store(in: &subscriptions) | |
subject.send("Run code") | |
subject.send(completion: .finished) | |
} | |
// MARK: - append | |
func appendOutputExample() { | |
let stringPublisher = ["Hello"].publisher | |
stringPublisher | |
.append("World!") | |
.sink(receiveValue: { print($0) }) | |
.store(in: &subscriptions) | |
} | |
func appendPublisherExample() { | |
let subject = PassthroughSubject<String, Never>() | |
let stringPublisher = ["Run code!"].publisher | |
stringPublisher | |
.append(subject) | |
.sink(receiveValue: { print($0) }) | |
.store(in: &subscriptions) | |
subject.send("Break things!") | |
subject.send(completion: .finished) | |
} | |
// MARK: - switchToLatest | |
func switchToLatestExample() { | |
let stringSubject1 = PassthroughSubject<String, Never>() | |
let stringSubject2 = PassthroughSubject<String, Never>() | |
let stringSubject3 = PassthroughSubject<String, Never>() | |
let subjects = PassthroughSubject<PassthroughSubject<String, Never>, Never>() | |
subjects | |
.switchToLatest() | |
.sink(receiveValue: { print($0) }) | |
.store(in: &subscriptions) | |
subjects.send(stringSubject1) | |
stringSubject1.send("A") | |
subjects.send(stringSubject2) | |
stringSubject1.send("B") // Omitted | |
stringSubject2.send("C") | |
stringSubject2.send("D") | |
subjects.send(stringSubject3) | |
stringSubject2.send("E") // Omitted | |
stringSubject2.send("F") // Omitted | |
stringSubject3.send("G") | |
stringSubject3.send(completion: .finished) | |
} | |
func switchToLatestExample2() { | |
func isAvailable(query: String) -> Future<Bool, Never> { | |
return Future { promise in | |
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { | |
promise(.success(Bool.random())) | |
} | |
} | |
} | |
let searchSubject = PassthroughSubject<String, Never>() | |
searchSubject | |
.print("subject") | |
.map { isAvailable(query: $0) } | |
.print("search") | |
.switchToLatest() | |
.sink(receiveValue: { print($0) }) | |
.store(in: &subscriptions) | |
searchSubject.send("Query 1") | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { | |
searchSubject.send( "Query 2") | |
} | |
} | |
// MARK: - merge(with:) | |
func mergeWithExample() { | |
let stringSubject1 = PassthroughSubject<String, Never>() | |
let stringSubject2 = PassthroughSubject<String, Never>() | |
stringSubject1 | |
.merge(with: stringSubject2) | |
.sink(receiveValue: { print($0) }) | |
.store(in: &subscriptions) | |
stringSubject1.send("A") | |
stringSubject2.send("B") | |
stringSubject2.send("C") | |
stringSubject1.send("D") | |
} | |
// MARK: - combineLatest | |
func combineLatestExample() { | |
let usernameTextField = CurrentValueSubject<String, Never>("") | |
let passwordTextField = CurrentValueSubject<String, Never>("") | |
let isButtonEnabled = CurrentValueSubject<Bool, Never>(false) | |
usernameTextField | |
.combineLatest(passwordTextField) | |
.handleEvents(receiveOutput: { (username, password) in | |
print("Username: \(username), password: \(password)") | |
let isSatisfied = username.count >= 5 && password.count >= 8 | |
isButtonEnabled.send(isSatisfied) | |
}) | |
.sink(receiveValue: { _ in }) | |
.store(in: &subscriptions) | |
isButtonEnabled | |
.sink { print("isButtonEnabled: \($0)") } | |
.store(in: &subscriptions) | |
usernameTextField.send("user") | |
usernameTextField.send("user12") | |
passwordTextField.send("12") | |
passwordTextField.send("12345678") | |
} | |
// MARK: - zip | |
func zipExample() { | |
let intSubject1 = PassthroughSubject<Int, Never>() | |
let intSubject2 = PassthroughSubject<Int, Never>() | |
let foundIdenticalPairSubject = PassthroughSubject<Bool, Never>() | |
intSubject1 | |
.zip(intSubject2) | |
.handleEvents(receiveOutput: { (value1, value2) in | |
print("value1: \(value1), value2: \(value2)") | |
let isIdentical = value1 == value2 | |
foundIdenticalPairSubject.send(isIdentical) | |
}) | |
.sink(receiveValue: { _ in }) | |
.store(in: &subscriptions) | |
foundIdenticalPairSubject | |
.sink(receiveValue: { print("is identical: \($0)") }) | |
.store(in: &subscriptions) | |
intSubject1.send(0) | |
intSubject1.send(1) | |
intSubject2.send(4) | |
intSubject1.send(6) | |
intSubject2.send(1) | |
intSubject2.send(7) | |
intSubject2.send(9) // Not displayed, as its pair is not yet emitted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment