Minor tweaks
This commit is contained in:
parent
ed390e9ac7
commit
c9033c70cf
|
@ -131,8 +131,8 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults.Key where Value: _DefaultsOptionalType {
|
extension Defaults.Key {
|
||||||
public convenience init(_ key: String, suite: UserDefaults = .standard) {
|
public convenience init<T>(_ key: String, suite: UserDefaults = .standard) where Value == T? {
|
||||||
self.init(key, default: nil, suite: suite)
|
self.init(key, default: nil, suite: suite)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,7 +143,7 @@ extension Defaults {
|
||||||
DefaultsPublisher(suite: key.suite, key: key.name, options: options)
|
DefaultsPublisher(suite: key.suite, key: key.name, options: options)
|
||||||
.map { _ in () }
|
.map { _ in () }
|
||||||
.eraseToAnyPublisher()
|
.eraseToAnyPublisher()
|
||||||
}.reduce(initial) { (combined, keyPublisher) in
|
}.reduce(initial) { combined, keyPublisher in
|
||||||
combined.merge(with: keyPublisher).eraseToAnyPublisher()
|
combined.merge(with: keyPublisher).eraseToAnyPublisher()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,11 +83,11 @@ extension Defaults {
|
||||||
let oldValue: Any?
|
let oldValue: Any?
|
||||||
|
|
||||||
init(change: [NSKeyValueChangeKey: Any]) {
|
init(change: [NSKeyValueChangeKey: Any]) {
|
||||||
kind = NSKeyValueChange(rawValue: change[.kindKey] as! UInt)!
|
self.kind = NSKeyValueChange(rawValue: change[.kindKey] as! UInt)!
|
||||||
indexes = change[.indexesKey] as? IndexSet
|
self.indexes = change[.indexesKey] as? IndexSet
|
||||||
isPrior = change[.notificationIsPriorKey] as? Bool ?? false
|
self.isPrior = change[.notificationIsPriorKey] as? Bool ?? false
|
||||||
oldValue = change[.oldKey]
|
self.oldValue = change[.oldKey]
|
||||||
newValue = change[.newKey]
|
self.newValue = change[.newKey]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +168,7 @@ extension Defaults {
|
||||||
lifetimeAssociation?.cancel()
|
lifetimeAssociation?.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private var lifetimeAssociation: LifetimeAssociation? = nil
|
private var lifetimeAssociation: LifetimeAssociation?
|
||||||
|
|
||||||
public func tieToLifetime(of weaklyHeldObject: AnyObject) -> Self {
|
public func tieToLifetime(of weaklyHeldObject: AnyObject) -> Self {
|
||||||
lifetimeAssociation = LifetimeAssociation(of: self, with: weaklyHeldObject, deinitHandler: { [weak self] in
|
lifetimeAssociation = LifetimeAssociation(of: self, with: weaklyHeldObject, deinitHandler: { [weak self] in
|
||||||
|
|
|
@ -79,12 +79,12 @@ final class LifetimeAssociation {
|
||||||
- Parameter owner: The object whose lifetime extends the target object's lifetime.
|
- Parameter owner: The object whose lifetime extends the target object's lifetime.
|
||||||
- Parameter deinitHandler: An optional closure to call when either `owner` or the resulting `LifetimeAssociation` is deallocated.
|
- Parameter deinitHandler: An optional closure to call when either `owner` or the resulting `LifetimeAssociation` is deallocated.
|
||||||
*/
|
*/
|
||||||
init(of target: AnyObject, with owner: AnyObject, deinitHandler: @escaping () -> Void = { }) {
|
init(of target: AnyObject, with owner: AnyObject, deinitHandler: @escaping () -> Void = {}) {
|
||||||
let wrappedObject = ObjectLifetimeTracker(for: target, deinitHandler: deinitHandler)
|
let wrappedObject = ObjectLifetimeTracker(for: target, deinitHandler: deinitHandler)
|
||||||
|
|
||||||
let associatedObjects = LifetimeAssociation.associatedObjects[owner] ?? []
|
let associatedObjects = LifetimeAssociation.associatedObjects[owner] ?? []
|
||||||
LifetimeAssociation.associatedObjects[owner] = associatedObjects + [wrappedObject]
|
LifetimeAssociation.associatedObjects[owner] = associatedObjects + [wrappedObject]
|
||||||
|
|
||||||
self.wrappedObject = wrappedObject
|
self.wrappedObject = wrappedObject
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
}
|
}
|
||||||
|
|
|
@ -366,7 +366,7 @@ final class DefaultsTests: XCTestCase {
|
||||||
func testReceiveValueBeforeSubscriptionCombine() {
|
func testReceiveValueBeforeSubscriptionCombine() {
|
||||||
let key = Defaults.Key<String>("receiveValueBeforeSubscription", default: "hello")
|
let key = Defaults.Key<String>("receiveValueBeforeSubscription", default: "hello")
|
||||||
let expect = expectation(description: "Observation closure being called")
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
let publisher = Defaults
|
let publisher = Defaults
|
||||||
.publisher(key)
|
.publisher(key)
|
||||||
.compactMap { $0.newValue }
|
.compactMap { $0.newValue }
|
||||||
|
@ -378,7 +378,7 @@ final class DefaultsTests: XCTestCase {
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}
|
}
|
||||||
|
|
||||||
Defaults[key] = "world";
|
Defaults[key] = "world"
|
||||||
cancellable.cancel()
|
cancellable.cancel()
|
||||||
waitForExpectations(timeout: 10)
|
waitForExpectations(timeout: 10)
|
||||||
}
|
}
|
||||||
|
@ -573,7 +573,7 @@ final class DefaultsTests: XCTestCase {
|
||||||
let expect = expectation(description: "Observation closure being called")
|
let expect = expectation(description: "Observation closure being called")
|
||||||
|
|
||||||
weak var observation: Defaults.Observation!
|
weak var observation: Defaults.Observation!
|
||||||
observation = Defaults.observe(key, options: []) { change in
|
observation = Defaults.observe(key, options: []) { _ in
|
||||||
observation.invalidate()
|
observation.invalidate()
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
}.tieToLifetime(of: self)
|
}.tieToLifetime(of: self)
|
||||||
|
|
Loading…
Reference in New Issue