Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NSUbiquitousKeyValueStore #136

Merged
merged 13 commits into from
Apr 20, 2024
2 changes: 1 addition & 1 deletion Sources/Defaults/Defaults+Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ extension Defaults {
return nil
}

if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, iOSApplicationExtension 15.0, macOSApplicationExtension 12.0, tvOSApplicationExtension 15.0, watchOSApplicationExtension 8.0, *) {
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, iOSApplicationExtension 15.0, macOSApplicationExtension 12.0, tvOSApplicationExtension 15.0, watchOSApplicationExtension 8.0, visionOSApplicationExtension 1.0, *) {
return Value(cgColor: cgColor)
}

Expand Down
14 changes: 14 additions & 0 deletions Sources/Defaults/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,17 @@ extension NSColor: Defaults.Serializable {}
*/
extension UIColor: Defaults.Serializable {}
#endif

extension NSUbiquitousKeyValueStore: DefaultsKeyValueStore {}
extension UserDefaults: DefaultsKeyValueStore {}

extension DefaultsLockProtocol {
@discardableResult
func with<R>(_ body: @Sendable () throws -> R) rethrows -> R where R: Sendable {
self.lock()
defer {
self.unlock()
}
return try body()
}
}
24 changes: 24 additions & 0 deletions Sources/Defaults/Defaults+Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,27 @@ public protocol _DefaultsRange {

init(uncheckedBounds: (lower: Bound, upper: Bound))
}

/**
Essential properties for synchronizing a key value store.
*/
protocol DefaultsKeyValueStore {
func object(forKey aKey: String) -> Any?

func set(_ anObject: Any?, forKey aKey: String)

func removeObject(forKey aKey: String)

@discardableResult
func synchronize() -> Bool
}

protocol DefaultsLockProtocol {
static func make() -> Self

func lock()

func unlock()

func with<R>(_ body: @Sendable () throws -> R) rethrows -> R where R: Sendable
}