Fix `Defaults.publisher(keys:)`

This commit is contained in:
Sindre Sorhus 2020-04-18 14:19:38 +08:00
parent 15c096d7fd
commit c20b7d820c
3 changed files with 10 additions and 76 deletions

View File

@ -132,59 +132,15 @@ extension Defaults {
Publisher for multiple `Key<T>` observation, but without specific information about changes.
*/
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
public static func publisher<Value: Codable>(
keys: Key<Value>...,
public static func publisher(
keys: _DefaultsBaseKey...,
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
let initial = Empty<Void, Never>(completeImmediately: false).eraseToAnyPublisher()
let combinedPublisher =
keys.map { key in
Defaults.publisher(key, options: options)
.map { _ in () }
.eraseToAnyPublisher()
}.reduce(initial) { (combined, keyPublisher) in
combined.merge(with: keyPublisher).eraseToAnyPublisher()
}
return combinedPublisher
}
/**
Publisher for multiple `NSSecureCodingKey<T>` observation, but without specific information about changes.
*/
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
public static func publisher<Value: NSSecureCoding>(
keys: NSSecureCodingKey<Value>...,
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
let initial = Empty<Void, Never>(completeImmediately: false).eraseToAnyPublisher()
let combinedPublisher =
keys.map { key in
Defaults.publisher(key, options: options)
.map { _ in () }
.eraseToAnyPublisher()
}.reduce(initial) { (combined, keyPublisher) in
combined.merge(with: keyPublisher).eraseToAnyPublisher()
}
return combinedPublisher
}
/**
Publisher for multiple `NSSecureCodingOptionalKey<T>` observation, but without specific information about changes.
*/
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
public static func publisher<Value: NSSecureCoding>(
keys: NSSecureCodingOptionalKey<Value>...,
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
let initial = Empty<Void, Never>(completeImmediately: false).eraseToAnyPublisher()
let combinedPublisher =
keys.map { key in
Defaults.publisher(key, options: options)
DefaultsPublisher(suite: key.suite, key: key.name, options: options)
.map { _ in () }
.eraseToAnyPublisher()
}.reduce(initial) { (combined, keyPublisher) in

View File

@ -305,7 +305,7 @@ final class DefaultsTests: XCTestCase {
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
func testObserveMultipleKeysCombine() {
let key1 = Defaults.Key<Bool>("observeKey1", default: false)
let key1 = Defaults.Key<String>("observeKey1", default: "x")
let key2 = Defaults.Key<Bool>("observeKey2", default: true)
let expect = expectation(description: "Observation closure being called")
@ -315,14 +315,13 @@ final class DefaultsTests: XCTestCase {
expect.fulfill()
}
Defaults[key1] = true
Defaults[key1] = "y"
Defaults[key2] = false
cancellable.cancel()
waitForExpectations(timeout: 10)
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
func testObserveMultipleNSSecureKeysCombine() {
let key1 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey1", default: ExamplePersistentHistory(value: "TestValue"))
@ -345,7 +344,7 @@ final class DefaultsTests: XCTestCase {
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
func testObserveMultipleOptionalKeysCombine() {
let key1 = Defaults.Key<Bool?>("observeOptionalKey1")
let key1 = Defaults.Key<String?>("observeOptionalKey1")
let key2 = Defaults.Key<Bool?>("observeOptionalKey2")
let expect = expectation(description: "Observation closure being called")
@ -355,7 +354,7 @@ final class DefaultsTests: XCTestCase {
expect.fulfill()
}
Defaults[key1] = true
Defaults[key1] = "x"
Defaults[key2] = false
cancellable.cancel()

View File

@ -318,7 +318,7 @@ Type: `class`
Create a NSSecureCoding key with an optional value.
#### `Defaults.reset(key, …)`
#### `Defaults.reset(keys…)`
Type: `func`
@ -360,7 +360,7 @@ Observe changes to a key or an optional key.
By default, it will also trigger an initial event on creation. This can be useful for setting default values on controls. You can override this behavior with the `options` argument.
#### `Defaults.publisher`
#### `Defaults.publisher(_ key:, options:)`
```swift
Defaults.publisher<T: Codable>(
@ -389,28 +389,7 @@ Observation API using [Publisher](https://developer.apple.com/documentation/comb
Available on macOS 10.15+, iOS 13.0+, tvOS 13.0+, and watchOS 6.0+.
#### `Defaults.publisher(keys:)`
```swift
Defaults.publisher<T: Codable>(
keys: Defaults.Key<T>...,
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
```
```swift
Defaults.publisher<T: NSSecureCoding>(
keys: Defaults.NSSecureCodingKey<T>...,
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
```
```swift
Defaults.publisher<T: NSSecureCoding>(
keys: Defaults.NSSecureCodingOptionalKey<T>...,
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
```
#### `Defaults.publisher(keys: keys…, options:)`
Type: `func`