-
-
Notifications
You must be signed in to change notification settings - Fork 4
Update to use newer Cache version #144
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,11 @@ import PackageDescription | |
| let package = Package( | ||
| name: "AppState", | ||
| platforms: [ | ||
| .iOS(.v15), | ||
| .watchOS(.v8), | ||
| .macOS(.v11), | ||
| .tvOS(.v15), | ||
| .visionOS(.v1) | ||
| .iOS(.v18), | ||
| .watchOS(.v11), | ||
| .macOS(.v15), | ||
| .tvOS(.v18), | ||
| .visionOS(.v2) | ||
| ], | ||
| products: [ | ||
| .library( | ||
|
|
@@ -18,19 +18,27 @@ let package = Package( | |
| ) | ||
| ], | ||
| dependencies: [ | ||
| .package(url: "https://github.com/0xLeif/Cache", from: "2.0.0"), | ||
| .package(url: "https://github.com/0xLeif/Cache", branch: "leif/mutex"), | ||
|
||
| .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.4.0") | ||
| ], | ||
| targets: [ | ||
| .target( | ||
| name: "AppState", | ||
| dependencies: [ | ||
| "Cache" | ||
| ], | ||
| swiftSettings: [ | ||
| .swiftLanguageMode(.v6), | ||
| .enableExperimentalFeature("StrictConcurrency") | ||
| ] | ||
| ), | ||
| .testTarget( | ||
| name: "AppStateTests", | ||
| dependencies: ["AppState"] | ||
| dependencies: ["AppState"], | ||
| swiftSettings: [ | ||
| .swiftLanguageMode(.v6), | ||
| .enableExperimentalFeature("StrictConcurrency") | ||
| ] | ||
| ) | ||
| ] | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #if !os(Linux) && !os(Windows) | ||
| import Cache | ||
| import Foundation | ||
| import Synchronization | ||
|
|
||
| /** | ||
| A `Keychain` class that adopts the `Cacheable` protocol. | ||
|
|
@@ -25,23 +26,19 @@ public final class Keychain: Sendable { | |
| public typealias Key = String | ||
| public typealias Value = String | ||
|
|
||
| private let lock: NSLock | ||
| @MainActor | ||
| private var keys: Set<Key> | ||
|
|
||
| private let keys: Mutex<Set<Key>> | ||
|
|
||
| /// Default initializer | ||
| public init() { | ||
| self.lock = NSLock() | ||
| self.keys = [] | ||
| self.keys = Mutex([]) | ||
| } | ||
|
|
||
| /** | ||
| Initialize with a predefined set of keys. | ||
| - Parameter keys: The predefined set of keys. | ||
| */ | ||
| public init(keys: Set<Key>) { | ||
| self.lock = NSLock() | ||
| self.keys = keys | ||
| self.keys = Mutex(keys) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -57,19 +54,16 @@ public final class Keychain: Sendable { | |
| kSecReturnData: true, | ||
| kSecMatchLimit: kSecMatchLimitOne | ||
| ] | ||
|
|
||
| var dataTypeRef: AnyObject? | ||
|
|
||
| lock.lock() | ||
| let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef) | ||
| lock.unlock() | ||
|
|
||
|
|
||
| guard | ||
| status == noErr, | ||
| let data = dataTypeRef as? Data, | ||
| let output = String(data: data, encoding: .utf8) as? Output | ||
| else { return nil } | ||
|
|
||
| return output | ||
| } | ||
|
|
||
|
|
@@ -113,9 +107,7 @@ public final class Keychain: Sendable { | |
| SecItemAdd(addQuery as CFDictionary, nil) | ||
| } | ||
|
|
||
| Task { @MainActor in | ||
| keys.insert(key) | ||
| } | ||
| _ = keys.withLock { $0.insert(key) } | ||
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -127,10 +119,9 @@ public final class Keychain: Sendable { | |
| kSecClass: kSecClassGenericPassword, | ||
| kSecAttrAccount: key | ||
| ] | ||
|
|
||
| lock.lock() | ||
|
|
||
| SecItemDelete(query as CFDictionary) | ||
| lock.unlock() | ||
| _ = keys.withLock { $0.remove(key) } | ||
|
Comment on lines
123
to
+124
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -174,21 +165,16 @@ public final class Keychain: Sendable { | |
| - Parameter ofType: The type of the values expected. | ||
| - Returns: A dictionary with keys and their associated values. | ||
| */ | ||
| @MainActor | ||
| public func values<Output>(ofType: Output.Type) -> [Key: Output] { | ||
| let storedKeys: [Key] | ||
| let storedKeys = keys.withLock { Array($0) } | ||
| var values: [Key: Output] = [:] | ||
|
|
||
| lock.lock() | ||
| storedKeys = Array(keys) | ||
| lock.unlock() | ||
|
|
||
|
|
||
| for key in storedKeys { | ||
| if let value = get(key, as: Output.self) { | ||
| values[key] = value | ||
| } | ||
| } | ||
|
|
||
| return values | ||
| } | ||
| } | ||
|
|
@@ -217,7 +203,6 @@ public extension Keychain { | |
| Returns all keys and their string values currently in the keychain. | ||
| - Returns: A dictionary with keys and their corresponding string values. | ||
| */ | ||
| @MainActor | ||
| func values() -> [Key: String] { | ||
| values(ofType: String.self) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a git branch for a dependency is generally discouraged for shared or production code as it can lead to build instability. Branches can be force-pushed, rebased, or deleted, which would break builds for anyone using this package. For better stability and reproducible builds, it's recommended to depend on a specific version tag or a commit hash. Please consider updating this to a stable release version once the
leif/mutexbranch is merged and a new version ofCacheis tagged.