Rename `DefaultsObservation` to `Defaults.Observation`

This commit is contained in:
Sindre Sorhus 2020-04-13 12:31:54 +08:00
parent b2fdee2055
commit 31b56ce018
3 changed files with 24 additions and 23 deletions

View File

@ -1,6 +1,5 @@
import Foundation import Foundation
/// TODO: Nest this inside `Defaults` if Swift ever supported nested protocols.
public protocol DefaultsObservation: AnyObject { public protocol DefaultsObservation: AnyObject {
func invalidate() func invalidate()
@ -26,6 +25,8 @@ public protocol DefaultsObservation: AnyObject {
} }
extension Defaults { extension Defaults {
public typealias Observation = DefaultsObservation
private static func deserialize<Value: Decodable>(_ value: Any?, to type: Value.Type) -> Value? { private static func deserialize<Value: Decodable>(_ value: Any?, to type: Value.Type) -> Value? {
guard guard
let value = value, let value = value,
@ -130,7 +131,7 @@ extension Defaults {
} }
} }
final class UserDefaultsKeyObservation: NSObject, DefaultsObservation { final class UserDefaultsKeyObservation: NSObject, Defaults.Observation {
typealias Callback = (BaseChange) -> Void typealias Callback = (BaseChange) -> Void
private weak var object: UserDefaults? private weak var object: UserDefaults?
@ -212,7 +213,7 @@ extension Defaults {
_ key: Defaults.Key<Value>, _ key: Defaults.Key<Value>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (KeyChange<Value>) -> Void handler: @escaping (KeyChange<Value>) -> Void
) -> DefaultsObservation { ) -> Defaults.Observation {
let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in
handler( handler(
KeyChange<Value>(change: change, defaultValue: key.defaultValue) KeyChange<Value>(change: change, defaultValue: key.defaultValue)
@ -230,7 +231,7 @@ extension Defaults {
_ key: Defaults.NSSecureCodingKey<Value>, _ key: Defaults.NSSecureCodingKey<Value>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (NSSecureCodingKeyChange<Value>) -> Void handler: @escaping (NSSecureCodingKeyChange<Value>) -> Void
) -> DefaultsObservation { ) -> Defaults.Observation {
let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in
handler( handler(
NSSecureCodingKeyChange<Value>(change: change, defaultValue: key.defaultValue) NSSecureCodingKeyChange<Value>(change: change, defaultValue: key.defaultValue)
@ -248,7 +249,7 @@ extension Defaults {
_ key: Defaults.NSSecureCodingOptionalKey<Value>, _ key: Defaults.NSSecureCodingOptionalKey<Value>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (NSSecureCodingOptionalKeyChange<Value>) -> Void handler: @escaping (NSSecureCodingOptionalKeyChange<Value>) -> Void
) -> DefaultsObservation { ) -> Defaults.Observation {
let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in
handler( handler(
NSSecureCodingOptionalKeyChange<Value>(change: change) NSSecureCodingOptionalKeyChange<Value>(change: change)

View File

@ -406,7 +406,7 @@ final class DefaultsTests: XCTestCase {
let key = Defaults.Key<Bool>("observeKey", default: false) let key = Defaults.Key<Bool>("observeKey", default: false)
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
var observation: DefaultsObservation! var observation: Defaults.Observation!
observation = Defaults.observe(key, options: [.old, .new]) { change in observation = Defaults.observe(key, options: [.old, .new]) { change in
XCTAssertFalse(change.oldValue) XCTAssertFalse(change.oldValue)
XCTAssertTrue(change.newValue) XCTAssertTrue(change.newValue)
@ -424,7 +424,7 @@ final class DefaultsTests: XCTestCase {
let key = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey", default: ExamplePersistentHistory(value: "TestValue")) let key = Defaults.NSSecureCodingKey<ExamplePersistentHistory>("observeNSSecureCodingKey", default: ExamplePersistentHistory(value: "TestValue"))
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
var observation: DefaultsObservation! var observation: Defaults.Observation!
observation = Defaults.observe(key, options: [.old, .new]) { change in observation = Defaults.observe(key, options: [.old, .new]) { change in
XCTAssertEqual(change.oldValue.value, "TestValue") XCTAssertEqual(change.oldValue.value, "TestValue")
XCTAssertEqual(change.newValue.value, "NewTestValue") XCTAssertEqual(change.newValue.value, "NewTestValue")
@ -441,7 +441,7 @@ final class DefaultsTests: XCTestCase {
let key = Defaults.Key<Bool?>("observeOptionalKey") let key = Defaults.Key<Bool?>("observeOptionalKey")
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
var observation: DefaultsObservation! var observation: Defaults.Observation!
observation = Defaults.observe(key, options: [.old, .new]) { change in observation = Defaults.observe(key, options: [.old, .new]) { change in
XCTAssertNil(change.oldValue) XCTAssertNil(change.oldValue)
XCTAssertTrue(change.newValue!) XCTAssertTrue(change.newValue!)
@ -459,7 +459,7 @@ final class DefaultsTests: XCTestCase {
let key = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingOptionalKey") let key = Defaults.NSSecureCodingOptionalKey<ExamplePersistentHistory>("observeNSSecureCodingOptionalKey")
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
var observation: DefaultsObservation! var observation: Defaults.Observation!
observation = Defaults.observe(key, options: [.old, .new]) { change in observation = Defaults.observe(key, options: [.old, .new]) { change in
XCTAssertNil(change.oldValue) XCTAssertNil(change.oldValue)
XCTAssertEqual(change.newValue?.value, "NewOptionalValue") XCTAssertEqual(change.newValue?.value, "NewOptionalValue")
@ -478,7 +478,7 @@ final class DefaultsTests: XCTestCase {
let key = Defaults.Key<URL>("observeKeyURL", default: fixtureURL) let key = Defaults.Key<URL>("observeKeyURL", default: fixtureURL)
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
var observation: DefaultsObservation! var observation: Defaults.Observation!
observation = Defaults.observe(key, options: [.old, .new]) { change in observation = Defaults.observe(key, options: [.old, .new]) { change in
XCTAssertEqual(change.oldValue, fixtureURL) XCTAssertEqual(change.oldValue, fixtureURL)
XCTAssertEqual(change.newValue, fixtureURL2) XCTAssertEqual(change.newValue, fixtureURL2)
@ -495,7 +495,7 @@ final class DefaultsTests: XCTestCase {
let key = Defaults.Key<FixtureEnum>("observeKeyEnum", default: .oneHour) let key = Defaults.Key<FixtureEnum>("observeKeyEnum", default: .oneHour)
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
var observation: DefaultsObservation! var observation: Defaults.Observation!
observation = Defaults.observe(key, options: [.old, .new]) { change in observation = Defaults.observe(key, options: [.old, .new]) { change in
XCTAssertEqual(change.oldValue, .oneHour) XCTAssertEqual(change.oldValue, .oneHour)
XCTAssertEqual(change.newValue, .tenMinutes) XCTAssertEqual(change.newValue, .tenMinutes)
@ -591,7 +591,7 @@ final class DefaultsTests: XCTestCase {
let key = Defaults.Key<Bool>("lifetimeTie", default: false) let key = Defaults.Key<Bool>("lifetimeTie", default: false)
let expect = expectation(description: "Observation closure being called") let expect = expectation(description: "Observation closure being called")
weak var observation: DefaultsObservation! weak var observation: Defaults.Observation!
observation = Defaults.observe(key, options: []) { change in observation = Defaults.observe(key, options: []) { change in
observation.invalidate() observation.invalidate()
expect.fulfill() expect.fulfill()
@ -605,7 +605,7 @@ final class DefaultsTests: XCTestCase {
func testObserveWithLifetimeTieManualBreak() { func testObserveWithLifetimeTieManualBreak() {
let key = Defaults.Key<Bool>("lifetimeTieManualBreak", default: false) let key = Defaults.Key<Bool>("lifetimeTieManualBreak", default: false)
weak var observation: DefaultsObservation? = Defaults.observe(key, options: []) { _ in }.tieToLifetime(of: self) weak var observation: Defaults.Observation? = Defaults.observe(key, options: []) { _ in }.tieToLifetime(of: self)
observation!.removeLifetimeTie() observation!.removeLifetimeTie()
for i in 1...10 { for i in 1...10 {

View File

@ -341,7 +341,7 @@ Defaults.observe<T: Codable>(
_ key: Defaults.Key<T>, _ key: Defaults.Key<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (KeyChange<T>) -> Void handler: @escaping (KeyChange<T>) -> Void
) -> DefaultsObservation ) -> Defaults.Observation
``` ```
```swift ```swift
@ -349,7 +349,7 @@ Defaults.observe<T: NSSecureCoding>(
_ key: Defaults.NSSecureCodingKey<T>, _ key: Defaults.NSSecureCodingKey<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (NSSecureCodingKeyChange<T>) -> Void handler: @escaping (NSSecureCodingKeyChange<T>) -> Void
) -> DefaultsObservation ) -> Defaults.Observation
``` ```
```swift ```swift
@ -357,7 +357,7 @@ Defaults.observe<T: NSSecureCoding>(
_ key: Defaults.NSSecureCodingOptionalKey<T>, _ key: Defaults.NSSecureCodingOptionalKey<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new], options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (NSSecureCodingOptionalKeyChange<T>) -> Void handler: @escaping (NSSecureCodingOptionalKeyChange<T>) -> Void
) -> DefaultsObservation ) -> Defaults.Observation
``` ```
Type: `func` Type: `func`
@ -446,27 +446,27 @@ Type: `func`
Remove all entries from the `UserDefaults` suite. Remove all entries from the `UserDefaults` suite.
### `DefaultsObservation` ### `Defaults.Observation`
Type: `protocol` Type: `protocol`
Represents an observation of a defaults key. Represents an observation of a defaults key.
#### `DefaultsObservation.invalidate` #### `Defaults.Observation#invalidate`
```swift ```swift
DefaultsObservation.invalidate() Defaults.Observation#invalidate()
``` ```
Type: `func` Type: `func`
Invalidate the observation. Invalidate the observation.
#### `DefaultsObservation.tieToLifetime` #### `Defaults.Observation#tieToLifetime`
```swift ```swift
@discardableResult @discardableResult
DefaultsObservation.tieToLifetime(of weaklyHeldObject: AnyObject) -> Self Defaults.Observation#tieToLifetime(of weaklyHeldObject: AnyObject) -> Self
``` ```
Type: `func` Type: `func`
@ -475,10 +475,10 @@ Keep the observation alive for as long as, and no longer than, another object ex
When `weaklyHeldObject` is deinitialized, the observation is invalidated automatically. When `weaklyHeldObject` is deinitialized, the observation is invalidated automatically.
#### `DefaultsObservation.removeLifetimeTie` #### `Defaults.Observation.removeLifetimeTie`
```swift ```swift
DefaultsObservation.removeLifetimeTie() Defaults.Observation#removeLifetimeTie()
``` ```
Type: `func` Type: `func`