Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds publisher function, accepting array of keys #148

Merged
merged 9 commits into from
Sep 3, 2023
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> {
self.publisher(keys: keys, options: options)
leoMehlig marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
hank121314 marked this conversation as resolved.
Show resolved Hide resolved

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
}
leoMehlig marked this conversation as resolved.
Show resolved Hide resolved

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