Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 12, 2024
1 parent 174b791 commit 4c009d5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Sources/Defaults/Defaults+Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ extension Defaults {
}

extension Defaults {
public struct AnyBridge: Defaults.Bridge, Sendable {
public struct AnyBridge: Bridge, Sendable {
public typealias Value = Defaults.AnySerializable
public typealias Serializable = Any

Expand Down
4 changes: 2 additions & 2 deletions Sources/Defaults/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ extension Defaults.Key {

extension Defaults.Key where Value: Equatable {
/**
Check whether the stored value is the default value.
Indicates whether the value is the same as the default value.
*/
public var isDefaultValue: Bool { self._isDefaultValue }
public var isDefaultValue: Bool { suite[self] == defaultValue }
}

extension Defaults {
Expand Down
11 changes: 8 additions & 3 deletions Sources/Defaults/Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,16 @@ extension Defaults {
}

func start(options: ObservationOptions) {
observations.forEach { $0.start(options: options) }
for observation in observations {
observation.start(options: options)
}
}

func invalidate() {
observations.forEach { $0.invalidate() }
for observation in observations {
observation.invalidate()
}

lifetimeAssociation?.cancel()
}

Expand All @@ -305,7 +310,7 @@ extension Defaults {
}

func remove(key: Defaults._AnyKey) {
guard let observation = observations.remove(DefaultsObservation(key: key, self.callback)) else {
guard let observation = observations.remove(DefaultsObservation(key: key, callback)) else {
return
}

Expand Down
20 changes: 17 additions & 3 deletions Sources/Defaults/SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension Defaults {
task?.cancel()
}

func observe() {
private func observe() {
// We only use this on the latest OSes (as of adding this) since the backdeploy library has a lot of bugs.
if #available(macOS 13, iOS 16, tvOS 16, watchOS 9, visionOS 1.0, *) {
task?.cancel()
Expand All @@ -44,7 +44,7 @@ extension Defaults {
return
}

self.objectWillChange.send()
objectWillChange.send()
}
}
} else {
Expand Down Expand Up @@ -221,12 +221,26 @@ extension Defaults {
}

extension Defaults.Toggle<Text> {
public init(_ title: some StringProtocol, key: Defaults.Key<Bool>) {
public init(
_ title: some StringProtocol,
key: Defaults.Key<Bool>
) {
self.label = { Text(title) }
self.observable = .init(key)
}
}

extension Defaults.Toggle<Label<Text, Image>> {
public init(
_ title: some StringProtocol,
systemImage: String,
key: Defaults.Key<Bool>
) {
self.label = { Label(title, systemImage: systemImage) }
self.observable = .init(key)
}
}

extension Defaults.Toggle {
/**
Do something when the value changes to a different value.
Expand Down
10 changes: 2 additions & 8 deletions Tests/DefaultsTests/DefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ final class DefaultsTests: XCTestCase {

async let waiter = Defaults.updates(key, initial: false).first { $0 }

try? await Task.sleep(seconds: 0.1)
try? await Task.sleep(for: .seconds(0.1))

Defaults[key] = true

Expand All @@ -798,7 +798,7 @@ final class DefaultsTests: XCTestCase {
}
}()

try? await Task.sleep(seconds: 0.1)
try? await Task.sleep(for: .seconds(0.1))

Defaults[key1] = true
Defaults[key2] = true
Expand All @@ -820,10 +820,4 @@ actor Counter {
}
}

// TODO: Remove when testing on macOS 13.
extension Task<Never, Never> {
static func sleep(seconds: TimeInterval) async throws {
try await sleep(nanoseconds: UInt64(seconds * Double(NSEC_PER_SEC)))
}
}
// swiftlint:enable discouraged_optional_boolean
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Store key-value pairs persistently across launches of your app.

It uses `UserDefaults` underneath but exposes a type-safe facade with lots of nice conveniences.

It's used in production by [all my apps](https://sindresorhus.com/apps) (1 million+ users).
It's used in production by [all my apps](https://sindresorhus.com/apps) (4 million+ users).

## Highlights

Expand Down

0 comments on commit 4c009d5

Please sign in to comment.