Minor tweaks

This commit is contained in:
Sindre Sorhus 2020-06-07 02:14:12 +08:00
parent ed390e9ac7
commit c9033c70cf
5 changed files with 15 additions and 15 deletions

View File

@ -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)
} }
} }

View File

@ -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()
} }

View File

@ -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

View File

@ -79,7 +79,7 @@ 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] ?? []

View File

@ -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)