-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Observation+Combine.swift
133 lines (111 loc) · 3.68 KB
/
Observation+Combine.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import Foundation
import Combine
extension Defaults {
/**
Custom `Subscription` for `UserDefaults` key observation.
*/
final class DefaultsSubscription<SubscriberType: Subscriber>: Subscription where SubscriberType.Input == BaseChange {
private var subscriber: SubscriberType?
private var observation: UserDefaultsKeyObservation?
private let options: ObservationOptions
init(subscriber: SubscriberType, suite: UserDefaults, key: String, options: ObservationOptions) {
self.subscriber = subscriber
self.options = options
self.observation = UserDefaultsKeyObservation(
object: suite,
key: key,
callback: observationCallback(_:)
)
}
func request(_ demand: Subscribers.Demand) {
// Nothing as we send events only when they occur.
}
func cancel() {
observation = nil
subscriber = nil
}
func start() {
observation?.start(options: options)
}
private func observationCallback(_ change: BaseChange) {
_ = subscriber?.receive(change)
}
}
/**
Custom Publisher, which is using DefaultsSubscription.
*/
struct DefaultsPublisher: Publisher {
typealias Output = BaseChange
typealias Failure = Never
private let suite: UserDefaults
private let key: String
private let options: ObservationOptions
init(suite: UserDefaults, key: String, options: ObservationOptions) {
self.suite = suite
self.key = key
self.options = options
}
func receive(subscriber: some Subscriber<Output, Failure>) {
let subscription = DefaultsSubscription(
subscriber: subscriber,
suite: suite,
key: key,
options: options
)
subscriber.receive(subscription: subscription)
subscription.start()
}
}
/**
Returns a type-erased `Publisher` that publishes changes related to the given key.
```swift
extension Defaults.Keys {
static let isUnicornMode = Key<Bool>("isUnicornMode", default: false)
}
let publisher = Defaults.publisher(.isUnicornMode).map(\.newValue)
let cancellable = publisher.sink { value in
print(value)
//=> false
}
```
- Warning: This method exists for backwards compatibility and will be deprecated sometime in the future. Use ``Defaults/updates(_:initial:)-9eh8`` instead.
*/
public static func publisher<Value: Serializable>(
_ key: Key<Value>,
options: ObservationOptions = [.initial]
) -> AnyPublisher<KeyChange<Value>, Never> {
let publisher = DefaultsPublisher(suite: key.suite, key: key.name, options: options)
.map { KeyChange<Value>(change: $0, defaultValue: key.defaultValue) }
return AnyPublisher(publisher)
}
/**
Publisher for multiple `Key<T>` observation, but without specific information about changes.
- Warning: This method exists for backwards compatibility and will be deprecated sometime in the future. Use ``Defaults/updates(_:initial:)-9eh8`` instead.
*/
public static func publisher(
keys: [_AnyKey],
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
let initial = Empty<Void, Never>(completeImmediately: false).eraseToAnyPublisher()
return
keys
.map { key in
DefaultsPublisher(suite: key.suite, key: key.name, options: options)
.map { _ in () }
.eraseToAnyPublisher()
}
.reduce(initial) { combined, keyPublisher in
combined.merge(with: keyPublisher).eraseToAnyPublisher()
}
}
/**
Publisher for multiple `Key<T>` observation, but without specific information about changes.
- Warning: This method exists for backwards compatibility and will be deprecated sometime in the future. Use ``Defaults/updates(_:initial:)-9eh8`` instead.
*/
public static func publisher(
keys: _AnyKey...,
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
publisher(keys: keys, options: options)
}
}