Fix `Defaults.publisher(keys:)`
This commit is contained in:
parent
15c096d7fd
commit
c20b7d820c
|
@ -132,59 +132,15 @@ extension Defaults {
|
||||||
Publisher for multiple `Key<T>` observation, but without specific information about changes.
|
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, *)
|
@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>(
|
public static func publisher(
|
||||||
keys: Key<Value>...,
|
keys: _DefaultsBaseKey...,
|
||||||
options: ObservationOptions = [.initial]
|
options: ObservationOptions = [.initial]
|
||||||
) -> AnyPublisher<Void, Never> {
|
) -> AnyPublisher<Void, Never> {
|
||||||
let initial = Empty<Void, Never>(completeImmediately: false).eraseToAnyPublisher()
|
let initial = Empty<Void, Never>(completeImmediately: false).eraseToAnyPublisher()
|
||||||
|
|
||||||
let combinedPublisher =
|
let combinedPublisher =
|
||||||
keys.map { key in
|
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
|
|
||||||
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)
|
|
||||||
.map { _ in () }
|
.map { _ in () }
|
||||||
.eraseToAnyPublisher()
|
.eraseToAnyPublisher()
|
||||||
}.reduce(initial) { (combined, keyPublisher) in
|
}.reduce(initial) { (combined, keyPublisher) in
|
||||||
|
|
|
@ -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, *)
|
@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() {
|
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 key2 = Defaults.Key<Bool>("observeKey2", default: true)
|
||||||
let expect = expectation(description: "Observation closure being called")
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
|
@ -315,14 +315,13 @@ final class DefaultsTests: XCTestCase {
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
Defaults[key1] = true
|
Defaults[key1] = "y"
|
||||||
Defaults[key2] = false
|
Defaults[key2] = false
|
||||||
cancellable.cancel()
|
cancellable.cancel()
|
||||||
|
|
||||||
waitForExpectations(timeout: 10)
|
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, *)
|
@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() {
|
func testObserveMultipleNSSecureKeysCombine() {
|
||||||
let key1 = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey1", default: ExamplePersistentHistory(value: "TestValue"))
|
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, *)
|
@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() {
|
func testObserveMultipleOptionalKeysCombine() {
|
||||||
let key1 = Defaults.Key<Bool?>("observeOptionalKey1")
|
let key1 = Defaults.Key<String?>("observeOptionalKey1")
|
||||||
let key2 = Defaults.Key<Bool?>("observeOptionalKey2")
|
let key2 = Defaults.Key<Bool?>("observeOptionalKey2")
|
||||||
let expect = expectation(description: "Observation closure being called")
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
|
@ -355,7 +354,7 @@ final class DefaultsTests: XCTestCase {
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
Defaults[key1] = true
|
Defaults[key1] = "x"
|
||||||
Defaults[key2] = false
|
Defaults[key2] = false
|
||||||
cancellable.cancel()
|
cancellable.cancel()
|
||||||
|
|
||||||
|
|
27
readme.md
27
readme.md
|
@ -318,7 +318,7 @@ Type: `class`
|
||||||
|
|
||||||
Create a NSSecureCoding key with an optional value.
|
Create a NSSecureCoding key with an optional value.
|
||||||
|
|
||||||
#### `Defaults.reset(key, …)`
|
#### `Defaults.reset(keys…)`
|
||||||
|
|
||||||
Type: `func`
|
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.
|
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
|
```swift
|
||||||
Defaults.publisher<T: Codable>(
|
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+.
|
Available on macOS 10.15+, iOS 13.0+, tvOS 13.0+, and watchOS 6.0+.
|
||||||
|
|
||||||
#### `Defaults.publisher(keys:)`
|
#### `Defaults.publisher(keys: keys…, options:)`
|
||||||
|
|
||||||
```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> {
|
|
||||||
```
|
|
||||||
|
|
||||||
Type: `func`
|
Type: `func`
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue