Skip to content

Commit

Permalink
Add .publisher() overload that accepts an array of keys (#148)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
leoMehlig and sindresorhus authored Sep 3, 2023
1 parent 957d807 commit 11b6adb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Sources/Defaults/Observation+Combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extension Defaults {
- 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...,
keys: [_AnyKey],
options: ObservationOptions = [.initial]
) -> AnyPublisher<Void, Never> {
let initial = Empty<Void, Never>(completeImmediately: false).eraseToAnyPublisher()
Expand All @@ -118,4 +118,16 @@ extension Defaults {
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)
}
}
8 changes: 7 additions & 1 deletion Sources/Defaults/Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ extension Defaults {
private weak var object: UserDefaults?
private let key: String
private let callback: Callback
private var isObserving = false

init(object: UserDefaults, key: String, callback: @escaping Callback) {
self.object = object
Expand All @@ -138,10 +139,15 @@ extension Defaults {

func start(options: ObservationOptions) {
object?.addObserver(self, forKeyPath: key, options: options.toNSKeyValueObservingOptions, context: nil)
isObserving = true
}

func invalidate() {
object?.removeObserver(self, forKeyPath: key, context: nil)
if isObserving {
object?.removeObserver(self, forKeyPath: key, context: nil)
isObserving = false
}

object = nil
lifetimeAssociation?.cancel()
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/DefaultsTests/DefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,23 @@ final class DefaultsTests: XCTestCase {
waitForExpectations(timeout: 10)
}

func testImmediatelyFinishingMultiplePublisherCombine() {
let key1 = Defaults.Key<Bool>("observeKey1", default: false)
let key2 = Defaults.Key<String>("observeKey2", default: "🦄")
let expect = expectation(description: "Observation closure being called without crashing")

let cancellable = Defaults
.publisher(keys: [key1, key2], options: [.initial])
.first()
.sink { _ in
expect.fulfill()
}

cancellable.cancel()

waitForExpectations(timeout: 10)
}

func testKeyEquatable() {
XCTAssertEqual(Defaults.Key<Bool>("equatableKeyTest", default: false), Defaults.Key<Bool>("equatableKeyTest", default: false))
}
Expand Down

0 comments on commit 11b6adb

Please sign in to comment.