/
Contents.swift
39 lines (31 loc) · 982 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Combine
import Foundation
func example(title: String, action: () -> Void) {
print("// \(title) 예제 코드")
action()
print("\n")
}
example(title: "merge(with:)") {
let firstPublisher = PassthroughSubject<Int, Never>()
let secondPublisher = PassthroughSubject<Int, Never>()
firstPublisher
.merge(with: secondPublisher)
.sink(receiveCompletion: { print($0) },
receiveValue: { print($0) })
firstPublisher.send(1)
secondPublisher.send(11)
firstPublisher.send(2)
secondPublisher.send(12)
}
example(title: "MergeMany") {
var publishers: [PassthroughSubject<Int, Never>] = []
for _ in 0...20 {
let publisher = PassthroughSubject<Int, Never>()
publishers.append(publisher)
}
Publishers.MergeMany(publishers)
.sink(receiveValue: { print($0, terminator: " ") })
for index in 0..<publishers.count {
publishers[index].send(index)
}
}