Skip to content

Commit

Permalink
Add support for syncing using NSUbiquitousKeyValueStore (#136)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
hank121314 and sindresorhus committed Apr 20, 2024
1 parent 38925e3 commit e837052
Show file tree
Hide file tree
Showing 12 changed files with 1,189 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Sources/Defaults/Defaults+Bridge.swift
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
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
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
}

0 comments on commit e837052

Please sign in to comment.