-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathSet+AnyCancellable.swift
74 lines (71 loc) · 2.09 KB
/
Set+AnyCancellable.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Set+AnyCancellable.swift
// CombineExt
//
// Created by Jasdev Singh on 10/04/2020.
// Copyright © 2020 Combine Community. All rights reserved.
//
import Combine
public extension Set where Element == AnyCancellable {
/// Convenience storage method on `Set` for a veridic number of `AnyCancellable`s.
///
/// `Set.store(_:)` can save repeated [AnyCancellable.store(in:)](https://developer.apple.com/documentation/combine/anycancellable/3333294-store) calls, e.g.
///
/// ```
/// firstPublisher
/// .sink( /* … */ )
/// .store(in: &subscriptions)
///
/// secondPublisher
/// .sink( /* … */ )
/// .store(in: &subscriptions)
///
/// thirdPublisher
/// .sink( /* … */ )
/// .store(in: &subscriptions)
/// ```
///
/// can be rewritten as
///
/// ```
/// subscriptions.store(
/// firstPublisher
/// .sink( /* … */ ),
/// secondPublisher
/// .sink( /* … */ ),
/// thirdPublisher
/// .sink( /* … */ )
/// )
/// ```
///
/// - parameter cancellables: The cancellables to store in the `Set`.
mutating func store(_ cancellables: AnyCancellable...) {
store(cancellables)
}
/// Convenience storage method on `Set` for a `Sequence` of `AnyCancellable`s.
///
/// `Set.store(_:)` can help in situations where you want to store batches of cancellables in one go, e.g.
///
/// ```
/// let firstBatchOfCancellables = […]
///
/// /* … */
///
/// let secondBatchOfCancellables = […]
///
/// /* … */
///
/// let thirdBatchOfCancellables = […]
///
/// subscriptions.store(
/// firstBatchOfCancellables +
/// secondBatchOfCancellables +
/// thirdBatchOfCancellables
/// )
/// ```
///
/// - parameter cancellables: The cancellables to store in the `Set`.
mutating func store<Sequence: Swift.Sequence>(_ cancellables: Sequence) where Sequence.Element == AnyCancellable {
cancellables.forEach { insert($0) }
}
}