From 31b56ce018f03b07cc335960d8f7aa1d49d9b0f8 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 13 Apr 2020 12:31:54 +0800 Subject: [PATCH] Rename `DefaultsObservation` to `Defaults.Observation` --- Sources/Defaults/Observation.swift | 11 ++++++----- Tests/DefaultsTests/DefaultsTests.swift | 16 ++++++++-------- readme.md | 20 ++++++++++---------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Sources/Defaults/Observation.swift b/Sources/Defaults/Observation.swift index 38c1983..bc3fba4 100644 --- a/Sources/Defaults/Observation.swift +++ b/Sources/Defaults/Observation.swift @@ -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: 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, options: NSKeyValueObservingOptions = [.initial, .old, .new], handler: @escaping (KeyChange) -> Void - ) -> DefaultsObservation { + ) -> Defaults.Observation { let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in handler( KeyChange(change: change, defaultValue: key.defaultValue) @@ -230,7 +231,7 @@ extension Defaults { _ key: Defaults.NSSecureCodingKey, options: NSKeyValueObservingOptions = [.initial, .old, .new], handler: @escaping (NSSecureCodingKeyChange) -> Void - ) -> DefaultsObservation { + ) -> Defaults.Observation { let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in handler( NSSecureCodingKeyChange(change: change, defaultValue: key.defaultValue) @@ -248,7 +249,7 @@ extension Defaults { _ key: Defaults.NSSecureCodingOptionalKey, options: NSKeyValueObservingOptions = [.initial, .old, .new], handler: @escaping (NSSecureCodingOptionalKeyChange) -> Void - ) -> DefaultsObservation { + ) -> Defaults.Observation { let observation = UserDefaultsKeyObservation(object: key.suite, key: key.name) { change in handler( NSSecureCodingOptionalKeyChange(change: change) diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index 6cebcf4..9794e84 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -406,7 +406,7 @@ final class DefaultsTests: XCTestCase { let key = Defaults.Key("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("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("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("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("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("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("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("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 { diff --git a/readme.md b/readme.md index e158a8a..043c492 100644 --- a/readme.md +++ b/readme.md @@ -341,7 +341,7 @@ Defaults.observe( _ key: Defaults.Key, options: NSKeyValueObservingOptions = [.initial, .old, .new], handler: @escaping (KeyChange) -> Void -) -> DefaultsObservation +) -> Defaults.Observation ``` ```swift @@ -349,7 +349,7 @@ Defaults.observe( _ key: Defaults.NSSecureCodingKey, options: NSKeyValueObservingOptions = [.initial, .old, .new], handler: @escaping (NSSecureCodingKeyChange) -> Void -) -> DefaultsObservation +) -> Defaults.Observation ``` ```swift @@ -357,7 +357,7 @@ Defaults.observe( _ key: Defaults.NSSecureCodingOptionalKey, options: NSKeyValueObservingOptions = [.initial, .old, .new], handler: @escaping (NSSecureCodingOptionalKeyChange) -> 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`