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

View File

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

View File

@ -341,7 +341,7 @@ Defaults.observe<T: Codable>(
_ key: Defaults.Key<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (KeyChange<T>) -> Void
) -> DefaultsObservation
) -> Defaults.Observation
```
```swift
@ -349,7 +349,7 @@ Defaults.observe<T: NSSecureCoding>(
_ key: Defaults.NSSecureCodingKey<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (NSSecureCodingKeyChange<T>) -> Void
) -> DefaultsObservation
) -> Defaults.Observation
```
```swift
@ -357,7 +357,7 @@ Defaults.observe<T: NSSecureCoding>(
_ key: Defaults.NSSecureCodingOptionalKey<T>,
options: NSKeyValueObservingOptions = [.initial, .old, .new],
handler: @escaping (NSSecureCodingOptionalKeyChange<T>) -> Void
) -> DefaultsObservation
) -> Defaults.Observation
```
Type: `func`
@ -446,27 +446,27 @@ Type: `func`
Remove all entries from the `UserDefaults` suite.
### `DefaultsObservation`
### `Defaults.Observation`
Type: `protocol`
Represents an observation of a defaults key.
#### `DefaultsObservation.invalidate`
#### `Defaults.Observation#invalidate`
```swift
DefaultsObservation.invalidate()
Defaults.Observation#invalidate()
```
Type: `func`
Invalidate the observation.
#### `DefaultsObservation.tieToLifetime`
#### `Defaults.Observation#tieToLifetime`
```swift
@discardableResult
DefaultsObservation.tieToLifetime(of weaklyHeldObject: AnyObject) -> Self
Defaults.Observation#tieToLifetime(of weaklyHeldObject: AnyObject) -> Self
```
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.
#### `DefaultsObservation.removeLifetimeTie`
#### `Defaults.Observation.removeLifetimeTie`
```swift
DefaultsObservation.removeLifetimeTie()
Defaults.Observation#removeLifetimeTie()
```
Type: `func`